Linux / UNIX: Kill Command Examples

by on January 2, 2009 · 5 comments· LAST UPDATED February 4, 2014

in , ,

kill linux unix command

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).

kill command details
DescriptionTerminate process with given signal
Category
Difficulty
Root privilegesNo
Estimated completion time10m
Contents

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:

NumberName (short name) DescriptionUsed for
0SIGNULL (NULL)NullCheck access to pid
1SIGHUP (HUP)HangupTerminate; can be trapped
2SIGINT (INT)InterruptTerminate; can be trapped
3SIGQUIT (QUIT)QuitTerminate with core dump; can be trapped
9SIGKILL (KILL)KillForced termination; cannot be trapped
15SIGTERM (TERM)TerminateTerminate; can be trapped
24SIGSTOP (STOP)StopPause the process; cannot be trapped. This is default if signal not provided to kill command.
25SIGTSTP (STP)TerminalStop/pause the process; can be trapped
26SIGCONT (CONT)ContinueRun 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

OR

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

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

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

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

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
 

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:

OptionMeaningExample(s)
-s signal_nameA symbolic signal name specifying the signal to be sent instead of the default TERMkill -s KILL 201
kill -s TERM 201
kill -s SIGKILL 1313
-lPrint a list of signal nameskill -l
kill -l 19
kill -l KILL

Related media

This tutorials is also available in a quick video format:

See also
CategoryList of Unix and Linux commands
File Managementcat
Network Utilitiesdighostip
Processes Managementbgchrootdisownfgjobskillkillallpwdxtimepidofpstree
Searchingwhereiswhich
User Informationgroupsidlastlastcommlognameuserswwhowhoamilidmembers

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 5 comments… read them below or add one }

1 suma March 28, 2012 at 12:29 pm

This article is very helpful for beginners

Reply

2 pranav August 18, 2012 at 3:55 pm

very very nice…. :)

Reply

3 pumaruma January 11, 2013 at 9:17 pm

mmmmmm, i just ruined my brain, gonna kill somones computer

Reply

4 vaibhav July 23, 2013 at 2:04 pm

Check

kill -l

first

Reply

5 Johnny Ooi July 7, 2014 at 6:28 am

You do NOT have to determine the PID of a process first in order to use kill. If you know the name of the program, you can chain it into the kill command like this:

kill -SIGTERM `pidof firefox`

This would kill all processes of firefox. Note that the mark is the backtick, not the apostrophe.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: