24

I am working on a multi-user Ubuntu server and need to run multiprocessing python scripts. Sometimes I need to kill some of those processes. For example,

$ ps -eo pid,comm,cmd,start,etime | grep .py
3457 python          python process_to_kill.py - 20:57:28    01:44:09
3458 python          python process_to_kill.py - 20:57:28    01:44:09
3459 python          python process_to_kill.py - 20:57:28    01:44:09
3460 python          python process_to_kill.py - 20:57:28    01:44:09
3461 python          python process_to_kill.py - 20:57:28    01:44:09
3462 python          python process_to_kill.py - 20:57:28    01:44:09
3463 python          python process_to_kill.py - 20:57:28    01:44:09
3464 python          python process_to_kill.py - 20:57:28    01:44:09
13465 python         python process_not_to_kill.py - 08:57:28    13:44:09
13466 python         python process_not_to_kill.py - 08:57:28    13:44:09

processes 3457-3464 are to be killed. So far I can only do

$ kill 3457 3458 3459 3460 3461 3462 3463 3464

Is there a command like $ kill 3457-3464 so I can specify the starting and ending processes and kill all of those within the range?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Xiaofeng Fan
  • 437
  • 1
  • 4
  • 9
  • 2
    `ps -H` will tell you whether they are related; you might prefer to kill the process group in that cales (e.g. `kill -3457`). – Toby Speight Apr 10 '18 at 15:02

1 Answers1

51

Use the shell's brace expansion syntax:

$ kill {3457..3464}

which expands to:

$ kill 3457 3458 3459 3460 3461 3462 3463 3464

Or you can kill processes by name with pkill. For example:

$ pkill -f process_to_kill.py
John Kugelman
  • 349,597
  • 67
  • 533
  • 578