Sometimes we end up with lots of instances of one program running. Maybe backups are scheduled from
cron but don’t use file locking. If one backup doesn’t finish before it’s time for the next one, you have two backup processes competing for the same resources, taking longer than expected…and before you know it, you have yet another backup process running. Of course, there are other causes of multiple similar processes running.
Sometimes we just want to stop them all. There are a number of way to kill multiple processes on a Linux server when normal daemon shutdown procedures don’t work, and we consider a few here.
The kill Command
This command is a misnomer: it actually sends a signal to a process, which may or may not kill that process. There are many types of signal, but the two that concern us here are
SIGKILL. The first (the default) instructs a process to terminate in a controlled way, and the second forces the process to end immediately.
Selecting Processes To Kill: The Hard Way
One selection technique is to use the output of the
ps command to generate a list of processes to kill. For example:
ps -o pid= -u freddy | xargs kill
That command will send a
SIGTERM signal to every process run by user freddy. If
-9 is appended to the above line, a
SIGKILL signal will be sent instead.
How It Works
pscommand lists processes running on the system.
-o pid=option specifies that only the process ID (pid) should be output. The equals sign redefines the header for the column, in this case to nothing, so no header is output.
-u freddyrestricts the listing to processes with an effective user ID of freddy
xargs killcommand will send a kill command to each PID passed to it.
Selecting Processes To Kill: The Easy Way
The above commands strings stemming from the output of
ps work, and it can be instructive to understand how they work, but there are easier ways.
This command sends a signal to all processes running any of the commands specified. Like
kill, by default a
SIGTERM is sent.
killall has a number of options, amongst them:
-I– ignore case
-i– ask for confirmation before killing each process
-o– only kill processes older than the time specified
-y– only kill processes younger than the time specified
-r– use a regex to select the processes
For example, the command below will send a signal (
SIGTERM) to all processes running
apache2 that have been running more than a week, and seeks confirmation before actually killing the process (note that there are better ways of simply stopping Apache under normal circumstances):
killall -io 1w apache2
If that fails – and only if you must kill Apache now – you can kill the processes with:
killall -9 -io 1w apache2
That will send a
SIGKILL signal to all processes running
apache2, forcing them to terminate immediately and abruptly.
Do not use this command on an HP-UX system because it will “do what it says on the tin” and kill all processes.
pgrep will list the process IDs (“PID”) of all processes matching certain criteria. To list the PIDs of all
To kill those processes:
To kill all processes run by user
pkill -u fredddy
This avoids the long, convoluted command lines that usually start with a
The above techniques should be considered for emergency use only. It is far better to shut down runaway processes using the appropriate
As always, full details of the commands used above are documented in the
Could This Linux Tip Be Improved?
Let us know in the comments below.