Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A process is a term used to refer to the running phase of a program. Every process is given a unique Proces ID(PID). But, often, it is seen that many processes become unresponsive or show some error while running. In these scenarios, ending them like we end other processes usually is impossible since they become unresponsive. Thus, ending such operations is done manually through some other commands.
In this article, we will study “How to Kill a Process in Linux?”
What is Killing a Process in Linux?
As we have noted before, a process is termed the running phase of a program. And often, some processes become unresponsive or take a lot of time and resources to complete or show errors while running. So, knowing how to end such processes manually using some commands is crucial.
Each process is given a Process ID(PID) in Linux. This ID is necessary to identify a process uniquely. In Linux, each process has access to the whole memory through virtual memory. This results in efficient programming. Thus, we can swiftly kill a process in Linux.
Why do we Need to Kill a Process?
Let us note the main points on the need to kill a process.
If a program has become unresponsive or frozen, the standard closing button may not work. Thus, the process should be ended forcibly.
Some processes are ended to free up memory space or some other system resources. Some programs often run in the background even when they are not used or have been closed. In such cases, processes have to be killed.
Another point that should be considered is that no critical tasks that may hinder the system's working should be ended.
Commands for Killing a Process in Linux
There are specific commands to kill a process in Linux: kill, killall, and Pkill. We will discuss these commands one by one.
Kill Command
First, let us determine the PID of the process we want to kill. We can achieve this using the following commands:
Linux Terminal
Linux Terminal
$ pidof bash
OR
Linux Terminal
Linux Terminal
$ ps aux | grep bash
Here, the aux options are:-
a= shows the processes of all the users
u= shows the owner of the process
x= displays processes not on the terminal
OUTPUT
Thus, we can see that all the details of the bash processes are displayed.
Now, if we want to kill the bash process of PID 76, we will write the following command:
Linux Terminal
Linux Terminal
$ kill -15 76
Now, to check if the process is killed, we again use the aux command, and now we do not see the process with PID 76 in the running processes.
OUTPUT
Let us discuss the two unique signals used with the kill command: SIGTERM(-15) and SIGKILL(-9).
SIGTERM(-15): The code sends this signal by default. It kills the process gracefully. Here, gracefully means saving the data, closing log files, and performing a clean shutdown.
SIGKILL(-9):- This signal forcefully kills the process. Here, the process is messy, and the data may or may not be saved.
Only when SIGTERM cannot end a process we use the SIGKILL signal.
The kill command is easy to use and works for most processes. Plus, we can send unique signals with this command. But, for processes that are in the blocked state, this command may not work.
Killall Command
This command ends all the processes at once based on a common pattern. We can write the process’s name, and all the processes running with that name will end. For example, if we want to end all bash processes, we can achieve it using the following command:
Linux Terminal
Linux Terminal
killall bash
Since only bash processes were running on our system, the bash window closed.
The Killall has some common signals used with it. They are as follows:-
-e:- This signals that the process’ name should match exactly. Otherwise, the command will end all the processes that contain that string.
-s signal:- It specifies a standard signal to the background processes.
-i:- This signal prompts the user before ending a process.
Pkill Command
This command is similar to the killall command. It ends processes based on their common attributes or names. But, it is more flexible regarding sending signals and pattern matching.
For example, to end all the bash processes, we can use the following command:-
Linux Terminal
Linux Terminal
$ pkill bash
Since only bash processes were running on our system, the bash window closed.
The pkill has some standard signals used with it. They are as follows:-
-i:- It matches the pattern or the attribute with the whole command line rather than just the process name matching.
-n:- It matches only those new or most recent processes.
-u username:- It matches only those processes with the specified username.
-s signal:- It sends a specific signal to the rest of the processes.
The essential advantage of the pkill command is that it can kill numerous processes with the same name or some attribute. But, it may not work in blocked processes or kill some unintended processes.
Frequently Asked Questions
What is Linux?
Linux is an operating system. It is an open-source system that is Unix-based. It is based on the Linux kernel. Linus Torvalds released it in September 1991. Users can modify and redistribute the source code of it under the GNU General Public License.
How to kill a process in Linux?
To kill a process in Linux, different types of commands are used. Mainly, three commands are used: kill, killall, and pkill. The kill command ends a process with the specified Process ID. The killall and pkill commands end all the specified name or attribute processes.
Why is there the need to end a process?
Many times some processes show some errors while running. Or it may be a case where some process goes unresponsive or uses a lot of time and resources for completion. In such cases, the need to end the process arises.
Conclusion
Every process running on the system is necessary for one or more reasons. But, sometimes, a process becomes unresponsive or shows some errors. In many scenarios, a process must be terminated to complete the task. Thus, one should know how to close a process. In this article, we studied “How to Kill a Process in Linux?” We explored different commands for it.
We recommend you read the following articles for more information:-