How to Kill Linux Processes/Unresponsive Applications Using 'kill & pkill' Command
How do we kill a resource/process in Linux?
Obviously we find the PID of the resource and then pass the PID to the kill command. Speaking more accurately, we can find PID of a resource (say firefox) as:
Syntax :
ps -A | grep -i process name
e.g.
ps -A | grep -i firefox
or
alternatively we can use pgrep command to find PID
Syntax :
pgrep process name
e.g.
pgrep firefox
sample output:
In the above output, the number '2697' is the PID of process (firefox), use the kill command to kill the process as shown below.
Syntax :
kill PID
OR
kill -signalName PID
OR
kill -signalNumber PID
Where,
- signalNumber : A non-negative decimal integer, specifying the signal to be sent instead of the default TERM.
- signalName : A symbolic signal name specifying the signal to be sent instead of the default TERM.
e.g.
kill 2697
The kill command sends a signal to a process, whose PID is passed along with the command.
SIGNAL NUMBER | SIGNAL NAME | Description | Used for |
---|---|---|---|
0 | SIGNULL (NULL) | Null | Check access to pid |
1 | SIGHUP (HUP) | Hangup | Terminate; can be trapped |
2 | SIGINT (INT) | Interrupt | Terminate; can be trapped |
3 | SIGQUIT (QUIT) | Quit | Terminate with core dump; can be trapped |
9 | SIGKILL (KILL) | Kill | Forced termination; cannot be trapped |
15 | SIGTERM (TERM) | Terminate | Terminate; can be trapped |
24 | SIGSTOP (STOP) | Stop | Pause the process; cannot be trapped. This is default if signal not provided to kill command. |
25 | SIGTSTP (STP) | Terminal | Stop/pause the process; can be trapped |
26 | SIGCONT (CONT) | Continue | Run a stopped process |
Alternatively, we can use pkill command, which kills a process based upon name and other attributes of a process. To kill a process say whose name is firefox, we need to execute:
Syntax :
pkill process name
e.g.
pkill firefox
Forced Killing (SIGKILL or kill -9)
The kill command has a misleading name because it does not actually kill processes. Rather, it sends signals to them. Each process is supplied with a set of standard signal handlers by the operating system in order to deal with incoming signals. When no signal is explicitly included in the command, signal 15, named SIGTERM, is sent by default. If this fails, the stronger signal 9, called SIGKILL, should be used. For example, the following command would nearly guarantee that process 485 would be killed:
Syntax :
kill -9 PID
OR
pkill -9 process name
kill -9 2697
OR
pkill -9 firefox
The only situation in which signal 9 will fail is if the process is in the midst of making a system call, which is a request to the kernel (i.e., the core of the operating system) for some action such as process creation. In this situation, the process will die once it has returned from the system call.
To list the signal names, pass the -l option as follows:
There are more than 60 signals that can be used with kill, but, fortunately, most users will only need to be aware of signal 9. The full list is contained in the file /usr/include/linux/signal.h and can be viewed by using kill with its -l option, i.e.,
kill -l