Kill a Process in Linux Command Line
Found a misbehaving process? Here's how to teach a lesson to it by terminating it using various commands.
It has been an awesome day on your Linux system, and suddenly a process starts to slow down the whole computer. It is not that important, and you want to stop its execution.
If the command/process is running in the foreground, you can use the Ctrl+C terminal shortcut. However, if the process is not visible (running in the background), you can use dedicated commands to 'kill it'.
The term "killing a process" refers to stopping a process mid execution. If you know the process ID (PID), you can use the kill command like this:
kill <signal> <PID>
In the above syntax, signal
refers to the kill signal you want to send for termination and PID
refers to the ID of the process.
There is also a killall command and I'll discuss both kill and killall commands in this article.
Before you start killing processes, you should know a few things. Such as what termination signals are available, how to find PID etc.
Let's start with the signals first.
Termination signals in Linux
When a process is terminated by the OS or by the user i.e. the process did not finish on its own, it is sent a terminal signal.
Below are the available termination signals in Linux:
Signal | Numerical Value | Description |
---|---|---|
SIGHUP | 1 | Signal Hangup: Sent to a process when the terminal controlling it is closed. |
SIGINT | 2 | Signal Interrupt: The signal sent to a process when a user terminates a process. (eg Ctrl + X) |
SIGKILL | 9 | Signal Kill: The signal that immediately quits a process, without allowing it to save its current state. |
SIGTERM | 15 | Signal Terminate: Sent to the signal to request termination of process. This signal can be ignored by a process. But this is the preferred way of terminating a process as it can release the resources when the process receives SIGTERM. |
SIGSTOP | 19 (for x86, ARM and most others) 17 (for Alpha) 23 (for MIPS) 24 (for PARISC) |
Signal Stop: Signal for stopping a process, but to be resumed at a later time. |
It is most likely that you'll be using signals 9 and 15. Read more about the difference between SIGKILL and SIGTERM.
Getting the PID of a process
You also need to know the details of the process you want to terminate. With kill command, you must provide the id of a process (PID). You can get the PID from the process name:
pidof exact_process_name
Terminating a process in the Linux command line
Let's see the kill command first as you'll be using it more than killall.
Using kill command
The kill
command requires that you know the ID of a process that you want to kill and, optionally, the termination signal.
To simply kill a command, use the following syntax:
kill [signal] <PID>
Sending a termination signal to a PID is optional, and if no signal is provided, kill
defaults to sending SIGTERM (15
), ending in a graceful termination of said process.
I started a background process of sleep command (and it gave me a PID). Let's try killing it using the kill
command.
Killing this particular instance of sleep
would look like the following:
$ sleep 120 &
[1] 125686
$ kill 125686
[1] + terminated sleep 120
If I wanted to use a termination signal, I could either use the numerical value or the signal itself:
$ sleep 120 &
[1] 125746
$ kill -SIGKILL 125746
[1] + killed sleep 120
####----##-----####
$ sleep 120 &
[1] 125759
$ kill -9 125759
[1] + killed sleep 120
Using killall command
If you do not know what the PID of a process is, or if the said process has multiple child processes, and you want to kill the child processes and the parent process at once, you can use killall
command.
killall [signal] <process-name>
Similar to kill
command, specifying a termination signal is optional. When no termination signal is specified, killall
will send SIGTERM (15
) to gracefully shut down said process.
To demonstrate the use of killall
, I want to kill two running sleep commands.
$ sleep 120 &
[1] 18609
$ sleep 2000 &
[2] 18612
$ killall sleep
[1]- Terminated sleep 120
[2]+ Terminated sleep 2000
Additionally, you can use the -e
flag to find an exact match of the process name.
Using pkill command
An alternative command to kill
is the pkill
command. It is a "combination" of pgrep and kill
command.
The killall
kills all the processes that have a matching name. On the other hand, pkill
uses pattern matching to match processes and kills them.
Below is the syntax:
pkill [options] pattern
A few useful options available in pkill
command as following:
-u
: Processes owned by a particular owner-x
: Processes that match pattern exactly-signal
: Specify a termination signal (default is SIGTERM)
Assuming I have a user guest
on my computer who should not have any processes running currently, and if there are, I want to terminate them. How can I do that?
$ ps aux | grep sleep
guest 126061 0.0 0.0 6664 428 pts/3 S 10:14 0:00 sleep 50
$ sudo pkill -u guest
Note that I used sudo
because I was dealing with processes that did not belong to my user.
With this article, you learned 3 different commands for killing a process in Linux. I feel it will be the kill command that you more than the others. Am I right?
Team LHB indicates the effort of a single or multiple members of the core Linux Handbook team.