In this article, we will learn what the Kill Process is, why & where it is used. However, if you find this a bit confusing, let me explain it. The kill Process is nothing but a method to kill a process. Therefore, to get a basic idea about it before going ahead, it would be great to have a little discussion about it, which will also help you clear most of the doubts.
In some cases, you may have noticed that when a Linux process becomes unresponsive or consumes a lot of resources, you are left with no option but to kill it. However, such type of situation is not always the case. Therefore, most processes have their own methods of shutting down to avoid these types of situations. Unfortunately, there are also some other types of processes that can malfunction and don't stop themselves. Thus, if a running background process becomes unresponsive or consumes more resources than it should, it becomes necessary to shut down or kill it using a process or method known as the "kill Process".
What Processes Can You Kill in Linux?
Before terminating or killing a process, you need to consider the permissions required to terminate a process, especially when you do not have access to the root account. However, if you have access to the root account or are the only one who owns the system, in that case, you have the power to terminate any kind of process at any time. Otherwise, a normal user who does not have root account access can use "sudo" in the prefix of the command while terminating a process using command line.
What happens when you kill a process?
Whenever a user kills a process, it actually sends a signal to the target process, considered a kill signal or termination message. However, there are several types of termination messages and some of them are as follows:
So, we can say that for most of the purposes, it will be best to use the methods that use the "SIGKILL" signal to kill a process in the safest & fastest way.
How to kill a process?
There are multiple ways to kill a process, but here we will see the most used and universal method you can use on any Linux-based operating system. Although, killing a process usually includes three main steps that we will see one by one.
Step 1: View Running Linux Processes
This is one of the most important steps to kill the process because this is the step in which we will discover which process is the target or consuming the most of the resources. Therefore to do that, you can use the following given command, which will display all the processes running on your system.
As you can see in the above given image, that this command reveals the process ID and users in addition to the amount of memory and CPU power used by each process.
Note: You can also kill a process directly from the "top" interface. All you need to do is press "k" on the keyboard and enter the process's id that you want to kill.
Step 2 Locate the process to Kill
In this step, we will learn how to locate a process because, in some cases, it is necessary to locate a process before we can kill it. However, there are several methods that can be used to locate a process. Still, here we will see the best one that always works as well as is easy to perform:
Locate process using "ps" command
The ps command also displays information about the processes similar to the "top" command that we have used earlier. However, it does not provide a similar interface as the "top" command does. Instead, the "ps" command provides a complete list of running processes, formatted based on the tags we add. To understand it in more detail, let us see its syntax.
For example, executing the command ps -A displays a detailed process list of all processes.
Step 3 Terminating a Process
In general, there are several different methods of killing a process in Linux, depending on whether you know the name of the process running, the PID of the process, or just how long the process has been running. Let us see a command method of terminating a process using the process ID. In this method, we will use the "kill command.
This command is used to kill processes by its PID( or process id). By default, it will send a "SIGTERM" signal that we have discussed above. However, this command can be used to kill multiple processes with a single command. The kill command can only kill a single process at a time with the given process ID. To kill a process, it usually sends a "SIGTERM "signal indicating a process to stop and waits for the program to run its shutdown routine.
There are several options that can be used with the "kill" command. Some of the most commonly used are listed below:
Apart from killing processes by name, we can also use kill command to kill a certain process based on its age using the following command:
Lets us see how we can kill a process using the "kill" command. We can use this method if we know the correct id of the process.
In this example, we will use the "ps" command to view the currently running process. After that, we will choose a process as the target, and by using the "kill" command with the process's name will try to kill a process. So let get into it.
Step1. Execute the following given command in the terminal as the root user:
Step2. Choose the process and remember its process id that you want to terminate. Here, I will choose the process name "calculator," and its process id is 3210, as you can also see in the given image:
Step3. After locating or knowing the process id of the process, we can kill or terminate the target process just by typing the process id next to the "kill" command as shown below:
As soon as you execute the above-given command, the calculator will be closed itself because its process is terminated. However, if you want to verify, you can also use the "ps -A" command to see all running processes, and you can search for the process with process id "3210". Although you will not find it anymore, as shown in the below-given image.
Key facts of Terminating a process
In this article, we have shown and explained several ways to kill a process in Kali Linux. It is essential for all system administrators to learn and understand these Linux termination commands because who can know when you might need to deal with these types of situations.