How to kill a process in Linux

We will show you how to kill a process in Linux. One of the main advantages of Linux is the ability to complete the process without having to restart the server. In this article, we will show you how to kill a process in Linux using the kill, pkill and killall commands.

1. What is PID

Before we start, we need to know what a process ID (PID) is.

PID stands for Linux process digital identification. Each process has a unique PID. Indeed, for example, the first process that starts on a Linux-based system is a process, and its PID is set to 1. This process is the parent of all other processes. The init process cannot be killed with kill commands, and this ensures that it is not accidentally killed.

Now, in order to find the PID of each running process on the server, we can run the following command:

ps -A

This gives us a list of all running processes and their corresponding PIDs.

If we want to find the PID of a specific process, we can use the pidof command followed by the process name. For example, to find out the PID of our MySQL process, you can run the following command:

pidof mysql

For even more details, we can use the command ps aux together with grep:

ps aux | grep mysql

Now that we know what a 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 using the kill command in Linux

There are a few important rules that we need to know before using the kill command.

  • You can only kill your own processes that are owned by your user id
  • You cannot kill processes of other users
  • You cannot kill system processes (unless you are superuser)
  • 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 are actually sending the specific signal to the PID controller 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.

Therefore, once we find the PID of the process we want to kill, use one of the methods we described earlier, we can use the command kill -9 PIDto kill the process from that specific PID.

For example, if the PID is 6738, then we can use the following command:

kill -9 6738

3. Kill the process with the pkill command in Linux

If you want to use the process name instead of its PID to kill it, then you can use the pkill command. For example, if the process we want to kill is called MySQL, then we can use the following command to kill it:

pkill mysql

4. Kill the process using 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 its child processes, we can use the command killall:

killall mysql

In this example, we will kill the MySQL process and all of its child processes.

These are the most common examples of process killing in Linux.

Sidebar