Each Linux system runs several processes. Not every process behaves in the manner we want them to.
Let’s say one process become starts to consume too much memory and causing instability in the system.
To handle this situation, we need to ensure that the process is terminated. We can do this using the kill command.
In this tutorial, we’ll learn the fundamentals of processes and how to use the
Table of Contents
- Access to a Linux machine. I’ll be using an Ubuntu 20.04 machine throughout this tutorial.
- Acting as a
non-root sudo userto ensure a secure environment
What is a Process?
Before we deep dive into the kill command, we should understand the concept of a process.
Let’s assume that we started a Webserver in Ubuntu. A process is triggered for the Webserver to perform specific tasks. As long as the Webserver runs, the process continues to run. When we shut down the program, the process is terminated gracefully.
We can run several processes in Ubuntu at once.
Each process has its permissions and is designed to perform specific tasks.
For example, the Webserver process is meant to do a specific task, and a text editor is designed to run another task. Each process runs in its own virtual address space, due to which if one process crashes, the other processes are not impacted.
Each process has a state, which changes dynamically. A process may have states, which can be running, waiting, stopped, or zombie.
Need to Kill a Process
Let’s assume that we are working on an application in Ubuntu. After a while, the application becomes unresponsive. We close the application. In this case, the process of this application keeps running. We attempt to start the application, but again, the application does not work.
The solution to this is to terminate the process.
If a process is running, it consumes the system resources and may not allow the unresponsive application to start again. When we terminate the process, it allows the application to start and create a new process.
SIGTERM and SIGKILL
It is also important to note that a user without administrative access can only terminate their own processes. They cannot terminate anyone’s process.
For example, a user named john can kill the process that he has started. He cannot kill processes for matt unless he uses the sudo command.
The root user can kill any process that may be running.
SIGTERM – Terminate Processes Gracefully
When terminating a process, by default, Linux sends the SIGTERM signal to the process. The SIGTERM terminates the process gracefully and properly.
SIGKILL – Terminate Processes Forcefully
On the other hand, the SIGKILL terminates a process forcefully. When using SIGKILL, we can either use the SIGKILL phrase or the associated number, which is 9. For example, we can use either one of these commands:
kill -9 5558
Alternatively, we can use the following command:
kill -SIGKILL 5558
Both these commands provide the same results and terminate the process forcefully.
Other than these two signals, there are various other signals available. We can find the information about these signals using the following command:
As shown in the screenshot, there are a total of 64 signal types. We can use any of these with the kill or killall commands.
For example, we can use commands like:
kill -SIGKILL 5558
killall -SIGKILL firefox
At this point, you may be confused on what the differences between the kill and killall commands are. We’ll cover this a bit further in the tutorial
The kill Command – Terminate Processes
We can use the kill command to kill a process, which requires us to know the Process ID (PID) to terminate the process. However, before we terminate a process, we need to find its PID, without which the kill command does not work.
So, as the first step, let’s find the PID of a process. To do this, we start an application, let’s say Mozilla Firefox.
To list the process IDs associated with firefox we run:
5558 5489 5477 5435 5373
Now, we know the PIDs of Mozilla Firefox. Let’s go ahead and use one of the PID to kill it.
The command to kill a process is simple.
The process with the PID 5558 is now terminated. Let’s verify it with the pidof command:
5558 5489 5477 5435 5373
The PID 5558 no longer exists.
5489 5477 5435 5373
We can also kill a process by sending the SIGKILLS signal to the process. For example, take PID is 5489. To send the TERM signal, we use the -9 parameter with the kill command:
kill -9 5489
This command forcefully terminates the PID 5489 It is important to note that we should avoid using the -9 parameter with the kill command. This parameter should be used when processes fail to terminate, and we have to force terminate it.
We now verify the existing PIDs of Mozilla Firefox.
5477 5435 5373
Notice that the PID 5489 no longer exists.
After we get the hang of the kill command, we can combine it with the pidof command, and use it in a substitution expression
$(...) to pass all the PIDs of a process to the kill command.
When we do this, we can kill a process without having to manually first find its PID and then run the kill command to kill it. For example, let’s say that we want to kill the processes for Mozilla Firefox.
To do this, we can run:
kill -9 $(pidof firefox)
The command doesn’t have an output, but in my case, all Firefox related processes have been killed.
The killall Command – Terminate All Matching Processes
A simpler alternative to
kill -9 $(pidof <process_name>) is the killall command. Those two commands are functionally identical.
There was a total of four PIDs that were attached to the Firefox browser. We can kill all of them at once without needing to know the PIDs of an application.
To do this we can just run:
The killall command kills all PIDs related to Mozilla Firefox.
Just like the kill command, we can also use the -9 parameter with the killall command. We can force the termination of all PIDs of an application at once. The command for the killall command with the
-9 parameter is:
killall -9 firefox
All existing PIDs are terminated. Again, we should avoid using the
-9 parameter as a precaution unless there is an absolute necessity.
Well done. Hopefully, this tutorial helped you understand the fundamentals of the kill command in Linux. If you encountered any issues, or have any questions, please feel free to leave a comment or contact us, and we’ll get back to we as soon as we can.