Have you ever faced the situation where you launched an application, and suddenly while you are using the app, it becomes unresponsive and unexpectedly crashes? You try to start the application again, but nothing happens because the original application process never truly shuts down completely.
Well, it has happened to all of us at some point, hasn’t it? The solution is to terminate or kill the application process. But how?
Luckily, there are several utilities in Linux that allows you to kill errant processes.
In this article, we will show you how to use kill
, killall
, and pkill
commands to terminate a process in Linux.
The main difference between these tools is that kill
terminates processes based on Process ID number (PID), while the killall
and pkill
commands terminate running processes based on their names and other attributes.
Regular users can kill their own processes, but not those that belong to other users, while the root user can kill all processes.
System Kill Signals
kill
, killall
, and pkill
send a given signal to specified processes or process groups. When no signal is specified, each tool sends 15
(TERM
).
The most commonly used signals are:
1
(-HUP): to reload a process.9
(-KILL): to kill a process.15
(-TERM): to gracefully stop a process.
Signals can be specified in three different ways:
- using a number (e.g., -1)
- with the “SIG” prefix (e.g., -SIGHUP)
- without the “SIG” prefix (e.g., -HUP).
Use the -l
option to list all available signals:
kill -l # or killall -l
The steps outlined below will work on all Linux distributions.
Terminating Processes Using the kill
Command
To terminate a process with the kill
command first, you need to find the process PID. You can do this using different commands such as top
, ps
, pidof
, and pgrep
.
Let’s say the Firefox browser has become unresponsive, and you need to kill the browser process. To find the process ID, use the pidof
command:
pidof firefox
The command will print all Firefox processes:
2551 2514 1963 1856 1771
Once you know the Firefox processes PIDs to terminate all of them send the TERM
signal:
kill -9 2551 2514 1963 1856 1771
Terminating Processes Using the killall
Command
The killall
command terminates all programs that match a specified name.
Using the same scenario as before, you can kill the Firefox process by typing:
killall -9 firefox
killall
accepts several options such as sending signals to processes owned by a given user, matching processes names against regular expressions, and the creation time. You can get a list of all options by typing killall
(without any arguments) on your terminal.
For example, to terminate all processes running as a user “testuser”, you would run the following command:
sudo killall -u testuser
Terminating Processes Using the pkill
Command
pkill
terminates processes that match the pattern given on the command line:
pkill -9 firefox
The name of the process doesn’t have to be an exact match.
With pkill
you can also send a signal to processes that are owned by a given user. To kill only the firefox processes owned by the user “testuser”, you would type:
pkill -9 -u testuser firefox