In this tutorial we will teach you how to kill a process in Linux.
One of the biggest advantages of Linux is the option to terminate a process without needing to reboot your server. In this guide, we will show you how to kill a process in Linux using the ‘kill’, ‘pkill’, and ‘killall’ commands.
Step 1: What is PID?
Before you start, you first need to know what a Process ID (PID) is.
PID represents a numeric identification of a process in Linux. Every process has a unique PID.
As an example, the first process which begins in a Linux based system is the ‘init’ process and its PID is set to ‘1’. This procedure is a parent of every other process. However, you cannot kill the ‘init’ process with the kill commands which ensures that it was not accidentally killed.
The next thing you have to do is to find the PID of each process running on the server. To do so, you need to run the command below.
This will show you a list of every process which is running and their respective PID.
If you want to look for the PID of a particular process instead, you simply use the ‘pidof’ command followed by the name of the process. As an example, to find out the PID of your ‘mysql’ process, you can execute the command below.
If you would like more detailed information, you can run the ‘ps aux’ command along with grep.
ps aux | grep mysql
Since you know what PID is and how to find the PID of a particular process, you can go on to the next step and learn how to kill it.
Step 2: Killing a process using the ‘kill’ command in Linux.
There are a couple of important things you need to know before you start using the ‘kill’ command:
- You may only kill your own processes which are owned by your userId
- You may not kill other user’s processes
- You may not kill a system process; note that this is only if you are the root user
- The root user can kill the process of any other user and any system process
If you kill a process with the ‘kill’ command, you need to send a particular signal to the PID you want to kill. The following signals are used by the ‘kill’ command:
1 = Hung up
9 = Kill
15 = Terminate
You will rarely see the ‘hung up’ signal used. You generally see the ‘kill’ signal used and, if it is not working, then you may use the ‘terminate’ signal.
After you find the PID of the process you would like to kill, using one of the methods mentioned before, you may use the ‘kill -9 PID’ command to kill the process with that particular PID.
For example, if the PID is 6738, then you will use the command below.
Step 3: Kill a process using the ‘pkill’ command in Linux.
The last two commands are only used to kill one particular process. However, if you are aiming to kill a process along with all of its child processes, you just need to use the ‘killall’ command.
The example above will kill every ‘mysql’ process and its child processes.