We’ll show you How to kill a process in Linux . One of the major advantages of Linux is the ability to terminate a process without having to reboot your server. In this tutorial, we will show you how to kill a process in Linux using the kill, pkill and killall commands.
Table of Contents
1. What is PID
Before we begin, we will need to know what is a Process ID (PID).
PID represents a numeric identification of a process in Linux. Each process has a unique PID. For, example the first process that starts in a Linux based system is the init process and his PID is set to ” 1 “. This process is a parent of all other processes. The init process cannot be killed using the kill commands and this ensures that it is not accidentally killed.
Now, in order to find the PID of every running process on the server we can execute the following command:
This gives us a list of all running process and their respective PIDs.
If we want to find the PID of a specific process instead, we can use the pidof command followed by the name of the process. For example, to find out the PID of our mysql process, we can run the following command:
For even more detailed information we can use the ps aux command together with grep:
ps aux | grep mysql
Now, that we know what PID is and how to find the PID of a specific process we can move on to the next section and learn how to kill it.
2. Kill a process with the kill command in Linux
There are few important rules that we need to know before start using the kill command.
- You can only kill your own processes which are owned by your userid
- You can not kill other user’s processes
- You can not kill system processes (unless you are the root user)
- The root user can kill the process of any other user and any system process
When we kill a process with the kill command, we actually send a specific signal to the PID that we want to kill. The following signals are used by the kill command:
1 = Hung up 9 = Kill 15 = Terminate
The hung up signal is rarely used. Most often we use the kill signal and if it doesn’t work then we can use the terminate signal.
So once we find the PID of the process we want to kill, using one of the methods we described before, we can use the kill -9 PID command to kill the process with that specific PID.
For example, if the PID is 6738, then we can use the following command:
kill -9 6738
3. Kill a process with the pkill command in Linux
If you want to use the name of the process instead of its PID to kill it, then you can use the pkill command. For example, if the process we want to kill is named mysql, then we can use the following command to kill it:
4. Kill a process with the killall command in Linux
The previous two commands are used to kill only one specific process. But, if we want to kill a process along with all of his child processes we can use the killall command:
In this example, we will kill the mysql process and all its child processes.
These are the most common examples of killing a process in Linux.
See Also – How to Find Large Files in Linux
Of course, you don’t have to terminate process in Linux, if you use one of our VPS Hosting services, in which case you can simply ask our expert Linux admins to help you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post on how to kill a process in Linux, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thank you.