Linux Operating System comes with kill command to terminate a process. The command makes it possible to continue running the server without the need to reboot after a major change/update. Here comes the great power of Linux and this is one of the reasons, why Linux is running on 90% of servers, on the planet.
In this article, we will show you how to use kill, killall, and pkill commands to terminate a process in Linux.
The main difference between these tools is that kill
terminates processes based on Process ID number (PID), while the killall
and pkill
commands terminate running processes based on their names and other attributes.
Regular users can kill their own processes, but not those that belong to other users, while the root user can kill all processes.
System Kill Signals
kill
, killall
, and pkill
send a given signal to specified processes or process groups. When no signal is specified, each tool sends 15
(TERM
) .
The most commonly used signals are:
1
(-HUP): to reload a process.9
(-KILL): to kill a process.15
(-TERM): to gracefully stop a process.
Signals can be specified in three different ways:
- using number (e.g., -1)
- with the “SIG” prefix (e.g., -SIGHUP)
- without the “SIG” prefix (e.g., -HUP).
Use the -l
option to list all available signals:
$ kill -l
or
killall -l
[ads]
Terminating processes using the kill command
To terminate a process with the kill command, first you need to find the process PID. You can do this using different commands such as top
, ps
, pidof
f and pgrep
.
Let’s say the Firefox browser has become unresponsive, and you need to kill the Firefox process. To find the process ID, use the pidof command
:
$ pidof firefox
The command will print all Firefox processes:
2551 2514 1963 1856 1771
Once you know the Firefox processes PIDs to terminate all of them send the TERM signal:
$ kill -9 2551 2514 1963 1856 1771
How about killing a process using process name
You must be aware of process name, before killing and entering a wrong process name may screw you.
$ pkill firefox
Kill more than one process at a time.
$ kill PID1 PID2 PID3 or $ kill -9 PID1 PID2 PID3 or $ kill -SIGKILL PID1 PID2 PID3
With pkill you can also send a signal to processes that are owned by a given user. To kill only the firefox processes owned by the user “rasho”, you would type:
$ pkill -9 -u rasho firefox
Terminating processes using the killall Command
The killall command terminates all programs that match a specified name.
Using the same scenario as before, you can kill the Firefox process by typing:
$ killall -9 firefox
killall accepts several options such as sending signals to processes owned by a given user, matching processes names against regular expressions, and the creation time. You can get a list of all options by typing killall (without any arguments) on your terminal.
For example, to terminate all processes running as a user “rasho”, you would run the following command:
$ sudo killall -u rasho
Conclusion
In this post, we saw the various ways to kill processes in Linux. Learning these commands is essential for proper system administration and management. If you want to explore more of those commands, have a look at their respective man pages. Don’t forget to give your valuable feedback in comment section.
See also:The Linux top command understanding