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:
For easier navigation, pipe the output to the less command:
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:
If you know the name of the target process, then we can use the pidof command to directly get the PID.
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.
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:
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.
- SIGUP (1) – The controlling terminal is hung up or the controlling process is dead. In such a situation, SIGUP will reload the configuration files and open/close log files.
- SIGKILL (9) – It’s a direct kill signal to the target process. It should be used as the last resort to terminate a process. If a process is terminated using SIGKILL, then it won’t save data or cleaning upon the termination of the process.
- SIGTERM (15) – It sends a termination signal to the target process. SIGTERM is the default signal to send. It’s also considered the safest method of terminating a process.
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:
For example, to send SIGTERM to a process, the command would look like this:
Similarly, if you want to send SIGKILL, then use the following command:
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:
For example, the following killall command will send SIGTERM to all the running Firefox processes:
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:
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;
For example, use the following pkill command to terminate the Firefox process with SIGTERM:
As always, check the man page of pkill for in-depth explanations with all the nuances:
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.