Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill a range of consecutive processes in Linux?

Tags:

linux

shell

kill

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?

like image 492
Xiaofeng Fan Avatar asked Apr 10 '18 14:04

Xiaofeng Fan


People also ask

How kill all processes with same name in Linux?

You can use the pkill command.

How do I kill a sleeping process in Linux?

Terminating a Process using kill Command You can use either the ps or pgrep command to locate the PID of the process. Also, you can terminate several processes at the same time by entering multiple PIDs on a single command line. Lets see an example of kill command. We would kill the process 'sleep 400' as shown below.

How do I Kill a process in Linux?

These commands can be used with any type of process, graphical or command line, foreground or background. To use kill, you must know the process ID (PID) of the process you wish to terminate. The ps command can be used to find the PID of a process. To have ps search through all of the processes use the -e (all processes) option.

How to kill SSH processes in Linux?

For example, here’s how to kill SSH: The pkill command is capable of sending different signals, just like the regular kill command: Don’t worry about getting the exact name of the process, either. This command killed a process named ssh-agent that was running on our system.

What is a process in Linux?

A process can be an application or script running on your Linux machine. Sometimes a process can crash or become a memory hog and this is when we need to step in and “kill” the process. As ever, there are a multitude of tools that we can use to do this. We will use a variety of approaches and tools to identify and kill the processes.

How do I see what processes are running in Linux?

To view a list of all currently running processes, use the command: The top command will reveal process IDs and users, in addition to the amount of memory and CPU power each process is using. To kill processes directly from the top interface, press k and enter the process ID.


1 Answers

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
like image 166
John Kugelman Avatar answered Sep 29 '22 11:09

John Kugelman