≡ Menu

Linux / UNIX: Kill Command Examples

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:

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

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:

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
Category List of Unix and Linux commands
File Managementcat
Network Utilitiesdighostip
Package Managerapt
Processes Managementbgchrootdisownfgjobskillkillallpwdxtimepidofpstree
Searchingwhereiswhich
User Informationgroupsidlastlastcommlognameuserswwhowhoamilidmembers

Share this tutorial on:
{ 8 comments… add one }
  • suma March 28, 2012, 12:29 pm

    This article is very helpful for beginners

  • pranav August 18, 2012, 3:55 pm

    very very nice…. :)

  • pumaruma January 11, 2013, 9:17 pm

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

  • vaibhav July 23, 2013, 2:04 pm

    Check

    kill -l

    first

  • Johnny Ooi July 7, 2014, 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.

  • suryar September 16, 2014, 11:49 am

    helpful

  • jack November 22, 2015, 4:20 pm

    is any chance to kill processes through bash shell script ? also to make sure that will not affect critical system processes, i mean the root processes ?

    i have tried to use something like this: but doesn’t work. have you got an idea how to fix it ?
    thanks

    kill -9 $(ps | grep “server1” | grep -v grep | awk ‘{ print $1 }’)

  • dipika July 25, 2016, 11:07 am

    What is this signals?

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


   Tagged with: , , , , , , , , , , , , , ,