Linux / UNIX: Kill Command Examples

I‘m a new Linux and Unix user. How do I send a KILL signal to a process under Linux or Unix-like operating systems using command prompt?

Use the kill command to send a signal to each process specified by a pid (process identifier). The default signal is SIGTERM (terminate the process).[donotprint]

kill command details
Description Terminate process with given signal
Category
Difficulty
Root privileges No
Estimated completion time 10m

[/donotprint]

Purpose

Removing a background process or job

Syntax

The basic syntax is as follows:

kill PID

OR

kill -s signalName PID

OR

kill -signalName PID

OR

kill -signalNumber PID

Where,

  1. signalNumber : A non-negative decimal integer, specifying the signal to be sent instead of the default TERM.
  2. signalName : A symbolic signal name specifying the signal to be sent instead of the default TERM.
  3. PID : Specify the list of processes that kill should signal. Each PID can be any one of the following:
    1. n : If PID is a positive value, the kill command sends the process whose process ID is equal to the PID.
    2. 0 : All processes in the current process group are signaled.
    3. -1 : All processes with pid larger than 1 will be signaled i.e. the kill command sends the signal to all processes owned by the effective user of the sender.

Common Linux and UNIX signal names and numbers

All available UNIX signals have different names, and are mapped to certain numbers as described below:

Number Name (short 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

Note the specific mapping between numbers and signals can vary between Unix implementations, please see the manual page entry signal(5), by typing the following command:

man 5 signal

man 5 signal

OR

man 7 signal

man 7 signal

A list of Specifies signal names are stored in /usr/include/sys/signal.h; so use more command to view the same:
$ more /usr/include/sys/signal.h

kill command examples

To list the signal names, pass the -l option as follows:
$ kill -l
Sample outputs:

 1) SIGHUP  2) SIGINT   3) SIGQUIT  4) SIGILL
 5) SIGTRAP  6) SIGABRT  7) SIGEMT   8) SIGFPE
 9) SIGKILL 10) SIGBUS  11) SIGSEGV 12) SIGSYS
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGURG
17) SIGSTOP 18) SIGTSTP 19) SIGCONT 20) SIGCHLD
21) SIGTTIN 22) SIGTTOU 23) SIGIO 24) SIGXCPU
25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
29) SIGINFO 30) SIGUSR1 31) SIGUSR2 

Finding a PID of process

Use either "pgrep command" or "ps command with grep command as shell pipe/filter". In this example, find a PID for firefox, enter:
$ pgrep firefox
OR
$ ps aux | grep [f]irefox
Sample outputs:

Fig.01: Finding PID using ps or pgrep command

Send a kill single to process ID 257

Use the following command to kill pid 257 and exit gracefully:

kill 257

kill 257

To find pid of any job or command use ps command as described above:

ps | grep command
ps aux | grep command
ps aux | grep apache
pgrep apache

ps | grep command
ps aux | grep command
ps aux | grep apache
pgrep apache

The following all are equivalent commands with -9 SIGKILL (i.e forcefully kill 1414 process without "clean up") and should be only use as last resort to kill process:

kill -s SIGKILL 1414 
kill -s KILL 1414 
kill -s 9 1414 
kill -SIGKILL 1414 
kill -KILL 1414

kill -s SIGKILL 1414
kill -s KILL 1414
kill -s 9 1414
kill -SIGKILL 1414
kill -KILL 1414

How do I send the default TERM/SIGTERM (15) to multiple pids # 1212, 1313, and 1414?

Type the following command:
$ kill 1212 1313 1414
Type the following command to send KILL single to multiple pids:
$ kill -9 1212 1313 1414

How do I specify the signal to send to PID # 4242?

Pass the -s option to given the signal as a signal number or a signal name. The syntax is:
$ kill -s signal pid
To send stop/suspend signal for pid # 4242, enter:
$ kill -s stop 4242
In this example:

## Start a gui command called xeyes in background:
xeyes &
jobs -l
 
## Stop it xeyes whose pid is 3405  by sending STOP (stop/suspend) signal
kill -s STOP 3405
jobs -l
 
## Resume/run stopped xeyes whose pid is 3405 by sending CONT (continue) signal
kill -s CONT 3405
jobs -l
 
## Finally kill xeyes whose pid is 3405 by sending TERM (exit/terminate safely) signal
kill -s TERM 3405

## Start a gui command called xeyes in background:
xeyes &
jobs -l ## Stop it xeyes whose pid is 3405 by sending STOP (stop/suspend) signal
kill -s STOP 3405
jobs -l ## Resume/run stopped xeyes whose pid is 3405 by sending CONT (continue) signal
kill -s CONT 3405
jobs -l ## Finally kill xeyes whose pid is 3405 by sending TERM (exit/terminate safely) signal
kill -s TERM 3405

How do I kill privileged process/PID owned by root or system user?

You need to login to root user either using su or sudo command to sends the specified signal to the specified process or process group owned by root/system user. In this example, login to the system using su command:

$ su -
Password: 
# pgrep httpd
4243
# kill 4243

OR use sudo command if configured on your system to kill PID # 4243:

$ sudo kill 4243
Password:

kill command options

From the kill(1) command man page:

Option Meaning Example(s)
-s signal_name A symbolic signal name specifying the signal to be sent instead of the default TERM kill -s KILL 201
kill -s TERM 201
kill -s SIGKILL 1313
-l Print a list of signal names kill -l
kill -l 19
kill -l KILL

Related media

This tutorials is also available in a quick video format:

See also
  • kill(1) Linux/Unix command man page
Category List of Unix and Linux commands
File Management cat
Network Utilities dighostip
Package Manager apkapt
Processes Management bgchrootdisownfgjobskillkillallpwdxtimepidofpstree
Searching whereiswhich
User Information idgroupslastlastcommlognameuserswwhowhoamilid/libuser-lidmembers

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.