In this guide, we will demonstrate how to kill a Linux process by its PID.
PID in Linux
The term PID is an acronym for “process identification number”. Each process is assigned a unique PID the moment they’re created on the system. The PID number 1 is assigned to systemd (init, for older systems). Essentially, systemd or init is always the first process to start on Linux and is parent to all other processes.
There are a couple of tools we can use to get the PID of a target process. The most popular (and recommended) method of getting the PID is using the ps command. It’s a built-in tool available on all Linux systems. Learn more in-depth about the ps command in Linux.
To print all the running processes with their PID, run the following ps command:
$ ps -ef
For easier navigation, pipe the output to the less command:
$ ps -ef | less
Notice that the PID column lists all processes sorted by PID. It’s the value that we are looking for.
Looking for a particular process with a specific name? Then the grep command is your friend. It can search for a particular pattern in the given file (STDOUT, in this case). For example, the following command will search for any running Firefox process:
$ ps -ef | grep firefox
If you know the name of the target process, then we can use the pidof command to directly get the PID.
$ pidof firefox
Another interesting command to do the job is pgrep. It directly operates on the list of currently running processes. The pgrep command will take the process name as the parameter and print the PID of any matching entry.
$ pgrep firefox
Note the PID of the target process. The next section of this article will showcase terminating the process by its PID.
Killing a Process
Killing a process requires sending a terminal signal to the process. However, there isn’t a single termination signal. There are several of them, each acting slightly differently than the others. So, it’s crucial to understand them first.
Linux Kill Signals
The kill command in Linux is responsible for sending the termination signals. For a detailed explanation, check out this guide on the Linux kill command. In short, you tell the kill command what signal to send to which process (using PID).
To get the complete list of all the termination signals, run the following command:
$ kill -l
For the most part, however, we will need only a handful of them. Here are detailed explanations of the most common termination signals you should know about.
Killing a Process Using Kill
This is the default way of terminating a target process on Linux. The kill command follows the following command structure:
$ kill -<signal_number> <PID>
For example, to send SIGTERM to a process, the command would look like this:
$ kill -15 <PID>
Similarly, if you want to send SIGKILL, then use the following command:
$ kill -9 <PID >
Learn more about the Linux kill command with in-depth explanations and examples.
Killing Multiple Processes Using Killall
The killall command acts similar to kill. However, instead of defining a specific PID, it uses the process name and sends the specified termination signal to all processes that match the name.
The command structure of killall looks like this:
$ killall -<signal_number> <process_name>
For example, the following killall command will send SIGTERM to all the running Firefox processes:
$ killall -15 firefox
Check out this guide on the Linux killall command for detailed explanations with examples. It’s also recommended to check out the man page of killall:
$ man killall
Killing Multiple Processes Using pkill
Similar to the killall command, pkill can also lookup processes based on the name and send the specified termination signal. The command structure is almost similar as well;
$ pkill -<ignal_number> <process_name>
For example, use the following pkill command to terminate the Firefox process with SIGTERM:
$ pkill -15 firefox
As always, check the man page of pkill for in-depth explanations with all the nuances:
$ man pkill
This guide showcases various ways of killing a process by its PID. It also demonstrates alternative methods using the process name instead of PID. It’s always better to have multiple tools at your disposal. Here, we used ps and pgrep commands to find the PID and kill, pkill, and killall commands for killing processes.
In many situations, you may want to get rid of pesky background processes that are malfunctioning. Learn more about identifying background processes in Linux. Then, following the methods in this guide, you can easily get rid of unwanted ones.