How to Kill a Process in Linux
Introduction
Before we begin talking about how to kill a process in Linux, let's briefly understand – What is kill command?
In Linux, killing a process is a fundamental task to terminate unresponsive or unwanted processes. The operating system provides several methods to accomplish this, allowing users to effectively manage their system's processes.
Regular users can kill their own processes but not of the others, whereas the root user can kill all the processes.
This tutorial will introduce various ways to kill a process in Linux, providing users with the necessary knowledge to terminate processes safely and efficiently. We will also address a few FAQs on how to kill a process in Linux.
Step 1 - System Kill Signals
1) kill
, killall
, and pkill
sends a signal to specified processes or process groups. When there is no specific signal, each tool sends 15
(TERM
).
The following are some of the most commonly used signals:
-
1
(-HUP) - used to reload a process. 9
(-KILL) - useful to kill a process.15
(-TERM) - it gracefully stops a process.
There are three different ways in which the signal can be specified:
- using a number (e.g., -1)
- with the “SIG” prefix (e.g., -SIGHUP)
- without the “SIG” prefix (e.g., -HUP).
2) Now use -l
option to list all the available signals, by:
kill -l # or killall -l
Next, the steps below will work on all Linux distributions.
Step 2 - Terminating Processes using the kill
Command
1) To kill a process using the kill
command, you have to first find the PID. You will be able to do this using different commands. Several different commands like top
, ps
, pidof
, as well as pgrep
can be used.
For example, the Firefox browser has become unresponsive, and you need to kill the browser process. Therefore, to find the process ID, usepidof
command:
pidof firefox
2) The above command will then print all the Firefox processes:
Output
2551 2514 1963 1856 1771
3) Now that you know the Firefox PIDs, you can use the TERM
signal to terminate them.
kill -9 2551 2514 1963 1856 1771
Step 3 - Terminating Processes using the killall
Command
1) The killall
command can terminate all programs matching a specified name.
Continuing with the previous example, you can terminate the Firefox process by:
killall -9 firefox
2) killall
supports a number of parameters, including the creation time, matching process names against regular expressions, and sending signals to user-owned processes. Enter killall
to see a list of all available choices on your terminal. For example, you need to use the following command to end all processes that are executing as user John
.
sudo killall -u John
Step 4 - Terminating Processes using pkill
Command
1) To terminate the processes matching with a pattern, you need to use the pkill
command:
pkill -9 firefox
For this to succeed, the name of the process does not have to be an exact match.
2) With pkill
you can send signals to the processes owned by a given user. Further, to kill only the firefox processes owned by the user John, use the following command:
pkill -9 -u sara firefox
FAQ's on how to kill a process in Linux
How can I identify the process ID (PID) of the process I want to kill?
You can use the ps
command to list all running processes and their corresponding PIDs: ps -ef
or ps aux
.
How can I check if a particular process exists before attempting to kill it?
You can use the pgrep
command to check if a process exists by its name or other attributes: pgrep <process_name>
.
Can I kill multiple processes at once?
Yes, you can provide multiple PIDs or process names to the kill
or killall
command to terminate multiple processes simultaneously.
How can I forcefully terminate all processes owned by a specific user?
You can use the pkill
command with the -u
option, followed by the username, to terminate all processes owned by that user: pkill -u <username>
.
What is the difference between kill
and killall
commands?
The kill
command terminates processes by their respective PIDs, while the killall
command terminates processes by their names.
How can I terminate a specific group of processes?
You can use the pkill
command with the -g
option, followed by the process group ID, to terminate all processes in a specific process group: pkill -g <PGID>
.
How can I kill a process in the background from a shell script?
You can use the kill
command with the process ID stored in a variable or retrieved from a file to terminate the process in the background.
Conclusion
We hope this detailed guide helped you to understand how to kill a process in Linux.
Killing a process in Linux is a necessary task for managing system resources and terminating unresponsive or unwanted processes. Understanding how to identify processes by PID or name and using commands like kill
, killall
, and pkill
empowers users to safely and efficiently terminate processes.
It is important to exercise caution when killing processes, particularly system-critical ones, as it can impact systems stability.
By following the tutorial and best practices outlined in this guide, users can effectively manage processes in Linux and ensure the smooth operation of their systems.
If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.