How to kill a range of consecutive processes in Linux?

Xiaofeng Fan picture Xiaofeng Fan · Apr 10, 2018 · Viewed 8.8k times · Source

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?

Answer

John Kugelman picture John Kugelman · Apr 10, 2018

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