Need to kill a running process? The kill command in Linux is built for just that. No operating system is perfect. Hence it is common for your Linux system to have to deal with the occasional unresponsive process.
To deal with instances where we need to end a process, we use the kill command in Linux.
In this tutorial, we will understand how the kill command works. Later, we will attempt to use the kill command for our requirements.
Basics of the kill Command
There are several utilities which help a Linux user terminate processes which may be misbehaving. One of the most commonly used utilities for this purpose in the kill command in Linux.
The kill command in Linux is used to manually kill a process using the command line. It is a shell built-in utility for most shells derived from the Bourne shell such as bash.
The options for the bash built-in version of the kill command in Linux is slightly different from the standalone executable found in /bin/kill.
However, for the sake of this tutorial, we will be using the bash built-in version of the kill command. To check all the location which contain the kill command on your system, type the following command in your terminal.
1 2 3 |
root@ubuntu:~# type -a kill kill is a shell builtin kill is /bin/kill |
You should get an output similar to the screenshot above. This output confirms that the shell built-in version of the kill command has priority over its standalone executable.
Hence, using the kill command will execute the bash built-in version. You can use the executable by using the full path for the command as /bin/kill/. However, we will proceed with the bash built-in version of the kill command.
Kill command in Linux
The best way to understand any command is through understanding it’s syntax. Here is how the syntax for the kill command in Linux looks like.
1 |
kill [option] PID |
The command kill is used to send a signal to a process. This signal decided by the option used while calling the kill command.
Process termination Signals with the kill Command
While there is a wide variety of options to choose from, here we have listed the most commonly used ones in the table below.
Option | Signal number | Action |
SIGTERM | 15 | The SIGTERM signal is used to tell a process to end gracefully. This kill signal may get interpreted by the process, or it may get ignored. |
SIGKILL | 9 | The SIGKILL signal is used to force a process to be terminated. It is a signal that cannot be ignored by the process in any case. |
SIGHUP | 1 | The SIGHUP signal is used to ‘hang up’ on a process. This can be used to restart a process. |
SIGINT | 2 | The SIGINT signal is used when the user wants to interrupt a process. |
If we don’t specify the option, the default option will be used for the kill command. The default option for the kill command in Linux is SIGTERM. Options can be used in the kill command in three different ways.
Using the signal number
1 |
kill -9 [PID] |
With the ‘SIG’ prefix
1 |
kill -SIGKILL [PID] |
Without the ‘SIG’ prefix
1 |
kill -KILL [PID] |
What is the Process ID (PID) when using the kill command in Linux?
Moving on to the final parameter, the PID. The PID refers to the process ID for a certain process. Whenever a process starts, it is assigned a unique ID by the kernel.
We use the PID in the kill command in Linux to specify the process to which we want to send the kill signal.
The PID used in the kill command can have any integer value. Here is a list containing the value of the PID and its effect on the kill command.
Value of PID | Effect on kill command |
PID>0 | This sends the appropriate kill signal to the process which as a PID equal to the PID mentioned in the command. |
PID=0 | This sends the appropriate kill signal to all the processes which are running under the same group as the one invoking the command. |
PID=-1 | This sends the appropriate kill signal to all the processes which are running under the same user as the one invoking the command. In the event of such command being invoked by the root user, the kill signal is sent to every process except the kill process and init process. |
PID<-1 | This sends the appropriate kill signal to all the processes which are running in a group, such that the group ID is equal to the absolute value of the PID used in the kill command. |
When we want to know the PID of a process, we use the following command.
1 |
pidof <processname> |
This command will return all the PIDs for the specified process. We can now use this PID to send the desired kill signal to the process.
Using the kill command
Now we have developed an understanding of the kill command in Linux and its parameters. Now it’s time to use this knowledge to perform two common operations involving the use of kill command.
Killing a process
The most common use of the kill command in Linux, as the name suggests, is to kill a process. Here, we will choose to kill the top command processes on our system using the kill command.
For this demonstration, I’ve simply run top on multiple shells to create these processes.
To do so, we will first use the following command to find the PIDs associated with the top command process.
1 |
pidof top |
Now, we use the SIGKILL kill signal to kill all the processes associated with the top command.
Reloading a process
Another function which is possible using the kill command in Linux is to restart a process. This is helpful in case a service is running but it needs to re-read its configuration file to enable recent changes.
For example, if you want to reload the Lighttpd server process on your system, you can run the following command.
1 |
sudo kill -SIGHUP <process ID of Lighttpd> |
Conclusion
If a process becomes unresponsive, there is no way to restart it. Only available options at hand are to either restart our system or to end the particular process. We hope that, through this tutorial, you were able to develop a good understanding of the kill command.
Try the kill command in Linux for yourself. If you have any comments, suggestions or feedback, feel free to reach out to us through the comment section below.