Killing a process means terminating the process and stopping its execution. You must have definitely encountered an unresponsive or misbehaving process/application at some point. An application like a web browser is just a set of processes running on your computer. If you open too many tabs, there is a high chance that your web browser will become unresponsive. If the web browser becomes unresponsive and you are not able to close it from the GUI, the best way to stop it is to kill all of its processes.
The easiest way to kill a process is through the command line. There are different commands to kill a process in Linux. We will cover the kill and pkill commands in this topic. We will go through their syntax, examples, the difference between both the commands and some guidelines on when to use which command. But before we go to the commands, let's understand the mechanism of stopping processes!
Signals
Killing a process is not like turning off the power of your computer, but it is similar to telling the process "I'm sending you a message to tell you to finish your work as soon as possible". And there are popular types of these messages called signals. A signal is a message to a process from the kernel. There are different types of signals you can send to a process. To view a list of all the available signals, run the kill -l command. Each signal has a name and a number, both of which can be used to refer to the same signal.
The most commonly used signals while killing processes are:
|
Signal |
Function |
|
SIGTERM (15) |
Terminate signal. The default signal sent by the kernel to a process requesting it to terminate. It is a safe method of terminating processes since it allows the process to clean up. |
|
SIGINT (2) |
Interrupt signal. This signal performs the same function as pressing Ctrl+C on a terminal. It usually terminates the program. |
|
SIGKILL (9) |
Kill signal. This is the most brutal way of killing a process. Actually, the SIGKILL signal is not sent to the target process. Whenever SIGKILL is used, the kernel directly terminates the process and so this signal cannot be ignored by the process. Since the kernel directly terminates the process, the process does not get time to save data and clean up. That's why SIGKILL should be used only when other signals are not able to terminate the process. |
|
SIGSTOP (19) |
Stop signal. It causes a process to pause without terminating it. Similar to SIGKILL, this signal isn't directly sent to the target process and hence cannot be ignored. |
|
SIGHUP (1) |
Hang up signal. It's used to reload a process. |
The kill command
kill is the most frequently used command for killing a process. It is used to kill a process with known PID.
The syntax of the command is:
kill [options] [PIDs]
PIDs are just space-separated process IDs for every process you want to kill. The most commonly used options are:
-<signal name/number>: specifies the signal to be sent to the process;-l: lists all the available signals (Discussed in the previous section);-l <signal name/number>: converts signal number to signal name, or vice-versa.
Let's look at some examples of how the kill command is used. To send the interrupt signal you can use one of these three variants:
# Using the full name of the signal
$ kill -SIGINT 5642
# Using the short name of the signal
$ kill -INT 5642
# Using the signal number
$ kill -2 5642
To kill multiple processes at once you can run the command with several process IDs:
# Sends the default kill signal (SIGTERM) to processes with PIDs 5642, 5789 and 6754
$ kill 5642 5789 6754
And if you don't know either the name or the number of a signal and want to find it out, try these commands:
$ kill -l 19
SIGSTOP
$ kill -l SIGINT
2
Now we know how to send a signal to a process when the PID of the process is known to us. But there are often times when we don't know the process ID. So, what can we do in such a situation?
The pkill command
The pkill command is used to kill a process by its name.
The syntax of the command is:
pkill [options] pattern
pattern specifies the regular expression for matching the process names. If the pattern matches multiple processes, all those processes are killed. Commonly used options for the pkill command are:
-<signal>: specifies the signal to be sent to the process;-n: selects only the newest of the matching processes;-o: selects only the oldest of the matching processes;-u <user_id>: only matches processes owned by the specified user.
If you want to see the complete list of all the options which could be used with pkill, refer to its manual page by using the man pkill command.
Let's go through a few examples to see how the pkill command is used:
# Sends SIGTERM signal to all the Firefox processes:
pkill firefox
#Sends SIGKILL (9) signal to all the Firefox processes:
pkill -9 firefox
# Sends SIGKILL signal to all the Chrome processes belonging to the user "jane":
pkill -SIGKILL -u jane chrome
Conclusion
Killing processes using the command line is very simple and efficient. In this topic, we saw two commands which can be used to kill processes: kill and pkill. Just to recap, use the kill command when you know the PID of the process you want to kill and use the pkill command when you know the name of the process you wish to kill.
We also learned about the various signals we can send to a process using kill and pkill.
Are you excited to practice killing some processes? Head on over to the exercise section!