How to Kill a Process on Linux
On Linux, the kill command is used with the process ID to kill an unresponsive process. Commands like pkill, xkill, and top can also be used to kill a Linux process.
The process on Linux is running an instance of an application. For example, whenever you launch an application, a process is created with a unique 5-digit ID called PID which manages the program in the background. If a process becomes unresponsive, it may start consuming system resources, consequently making the system unresponsive. In most cases, the processes manage themselves to recover from malfunctioning, however, many times it becomes necessary to give the manual command to close the process.
In this guide, I will be going through methods of finding process IDs on Linux and how to kill processes using sigkill and sigterm commands.
Finding Process IDs on Linux
Before killing a process, it is crucial to know the process ID of the unresponsive process. There are multiple command line utilities to find PIDs on Linux, such as:
- ps
- pgrep
- pidof
- top
- htop
Using ps Command
To see every process and process IDs running on the system, use the ps command with the -e option.
ps -e | less
The less is piped to display the output in an organized way.
Another way to list the process IDs of all the processes, the ps command is used with options -a, -u, and -x.
ps -aux
Here, -a is used to display the processes of all users, -u provides the user-oriented detailed information of the processes, and -x is used to list independent background processes such as processes controlled by daemons. Pipe less with the ps aux command to print the output in an organized way.
ps -aux | less
Using pgrep Command
The pgrep command is an advanced way of looking through the PIDs. It is used to list the process according to the mentioned criteria. For example, to list the process IDs associated with a user sam, use the command given below:
pgrep -l -u sam
Or, to get the PID of the process sshd, execute the following command:
pgrep sshd
Multiple users can also be mentioned just separate each user using the comma.
Other options that can be used with pgrep are: -n and -o to are used to list the newest process and the oldest process only, respectively.
Using pidof Command
The pidof prints the process ID of the process if the name is given and it is running. For example, to get the PID of sshd use:
pidof sshd
Using top Command
The top is an interactive command, essentially used to display the processes and threads on Linux.
top
To learn more about top command, execute man top command. To quit the session, press the q button.
Using htop Command
The htop command is a newer version of the top command. It can also be used to get the process ID. Unlike top, it does not come preinstalled on Linux distributions. It provides color and detailed information on the system process.
htop
In the above-mentioned sections, we have seen methods for finding the PID of the processes, which is an important argument used to kill the process.
What are the Signals to Kill Process on Linux
Only root users or users with sudo privileges can kill a process on Linux.
Two significant signals are used to kill the process on Linux:
- SIGTERM
- SIGKILL
SIGTERM: Terminate the process also known as soft kill. It sends signals to parent and child processes before killing. However, it does not guarantee whether the mentioned process will be killed or not. It is by default used by the kill command.
SIGKILL: Immediately kills the process and may lead to generating errors. It ensures that the process will be killed. However, it does not send any signal to parent or child processes, consequently creating zombie processes.
The SIGKILL is the most effective way to kill any unresponsive process. However, it is preferred to use SIGTERM as it will not cause any harm to the system processes.
How to Kill Process on Linux
There are various ways to kill a process on Linux, such as:
- Using killall Command
- Using kill Command
- Using pkill Command
- Using top Command
- Using htop Command
1. Using killall Command
The killall command is used to kill the process if it is mentioned by name.
killall [options] [process_name]
Some commonly used options with the killall command are listed below:
-e | –exact | To match the exact pattern of the process name |
-I | –ignore-case | To ignore case sensitivity of the process name |
-i | –interactive | To ask before killing the process |
-l | –list | To list all the signal names |
-v | –verbose | To display a report on successfully sending signal |
-o | –older-than | To kill the process older than the mentioned time. Commonly used units, s,m,h,d,w,M,y. |
-y | –younger-than | To kill the process younger than the mentioned time. Commonly used units, s,m,h,d,w,M,y. |
-r | –regexp | To kill the process by interpreting the mention regex |
-u | –user | To kill a process, belong to a specific user |
-q | –quiet | To kill a process quietly |
By default, the killall command uses the SIGTERM signal. For example, to kill sshd process with -i option.
sudo killall -i sshd
On executing the command, it will prompt for confirmation.
Similarly, to kill all the process older than 20 minutes, use:
sudo killall -o 20m
If you are not sure about the case of the process name, simply ignore the case-sensitivity using the -I option.
sudo killall -I Snapd
To kill all the processes owned by a user sam, use the command given below:
sudo killall -u sam
2. Using kill Command
The kill command is used to kill the process on Linux by sending a signal to a process or process group.
kill [options] [process_id]
Some commonly used options with the kill command are listed below:
-s | –signal | Used to set signal name or signal number |
-l | –list | Used to list the signal number and names |
-a | –all | Used to kill multiple processes |
The most important option is the -s or –signal option, which is used to specify the signal name and signal number with the kill command.
The commonly used signals are listed below:
Signal Number | Signal Name | Description |
1 | SIGHUP | It indicates that the controlling process has terminated. Generally used to reload the configuration. |
2 | SIGINT | It is used to interrupt the process equivalent to ctrl+c |
4 | SIGILL | It indicated the illegal process |
5 | SIGABRT | It is used to abort the process |
9 | SIGKILL | It is used to hard-kill the process, generally used to immediately killing the process |
11 | SIGSEGV | It indicates the invalid memory reference |
15 | SIGTERM | It is used to soft-kill the process |
Let’s take a look at some examples of using the kill command to kill a process.
To close a process, use the kill command with the process ID or PID.
kill PID
For example, the PID of sshd is 1055, to kill it use the command given below:
Note that PIDs may be different in your case.
As no signal is mentioned in the command above, the SIGTERM or 15 signal is utilized by default.
To immediately kill a process, use SIGKILL or 9 signal with the kill command and PID.
kill -9 PID
Or, –9 can be replaced with the signal name -SIGKILL.
kill -SIGKILL PID
Assume the process ID is 1055, to immediately kill the process, use the kill command with -9.
sudo kill -9 1055
The -9 or -SIGKILL signal is used to kill a process that is unresponsive immediately. It is used when the -SIGTERM fails to kill a process.
After killing the process, it can be restarted using systemctl start with the process name.
Note: Use the -9 or -SIGKILL signal with caution because it can cause to data loss or corruption.
3. Using pkill Command
The pkill command is also a powerful command line utility used to terminate the process with the process name. The general syntax of using the pkill command is given below:
pkill [options] [process_name]
The pkill command is a part of pgrep so they share many options. The commonly used options with the pkill are listed below:
-s | –signal | To set the signal name of the signal number |
-n | –newest | To kill the newest process |
-o | –oldest | To kill the oldest process |
-u | –user | To kill a process created by a user |
-e | –echo | To display what is killed |
-f | –full | To match the full process name to match |
-i | –ignore-case | To ignore the case of the process name |
For example, to kill the sshd process, use the following command:
sudo pkill sshd
Note that in the above command, the process name is case sensitive.
By default, the pkill sends the -SIGTERM or -15 signal.
To immediately kill an unresponsive process, use -9 or -SIGKILL.
sudo pkill -9 sshd
Similarly, to kill the newest process created by user sam, use the command given below:
sudo pkill -n -u sam
Where -n signifies the newest process while -u username.
4. Using top Command
The top command is used to list the Linux processes, since it is an interactive command, it can also be used to manage the processes. For example, to kill the process, execute the top command with sudo, and then press k.
You will be prompted to provide the PID that needs to be killed.
Type the PID and then you will be prompted to provide the signal name or number.
Type the signal name or signal number and then press the Return key.
If you want to abort the operation, press the Esc key at any prompt or provide incorrect PID.
5. Using htop Command
The htop is more interactive compared to the top command. To kill a process using htop, execute the command, then scroll over the desired process.
Then press the F9 or k key, and a list of all the signals will show up. Scroll on the desired signal and press Enter.
To skip the process, simply press the Esc key.
Conclusion
An unresponsive process on Linux consumes a lot of resources and may freeze the system. To get the system back to normal that process needs to be stopped. The first step to kill a process on Linux is to know the process ID or PID of the target process. The PIDs of the process can be listed using ps, pgrep, or top commands. After knowing the PIDs, use the killall, kill, pkill, and top commands to finish off the process.
The kill command is widely used to kill processes with a terminating signal; by default, it uses the default signal SIGTERM and process ID to kill a process, however, if the process is not responding the SIGKILL signal can also be used.