Difference between revisions of "Sending signal to Processes"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
 
Line 1: Line 1:
 +
* You can send singles to both commands / process and shell scripts using the [[kill command]] or [[killall command]].
 +
==kill - send a signal to a process==
 +
The  default  signal for kill is TERM. To list available signals, enter:
 +
<source lang="bash">kill -l</source>
 +
Sample outputs:
 +
<pre>
 +
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
 +
5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
 +
9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
 +
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT
 +
17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
 +
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU
 +
25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
 +
29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN
 +
35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4
 +
39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
 +
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
 +
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
 +
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
 +
55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6
 +
59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
 +
63) SIGRTMAX-1 64) SIGRTMAX
 +
</pre>
 +
===kill command Examples===
 +
The [[kill command]] can send all of the above singles to commands and process. However, commands only give response if they are programmed to recognize those singles. Particularly useful signals include:
 +
# SIGHUP (1) - Hangup detected on controlling terminal or death of controlling process.
 +
# SIGINT (2) - Interrupt from keyboard.
 +
# SIGKILL (9) - Kill signal i.e. kill running process.
 +
# SIGSTOP (19) - Stop process.
 +
# SIGCONT (18) - Continue process if stopped.
 +
To send kill single to PID # 1234 use:
 +
<source lang="bash">kill -9 1234</source>
 +
OR
 +
<source lang="bash">kill -KILL 1234</source>
 +
OR
 +
<source lang="bash">kill -SIGKILL 1234</source>
 +
==killall - kill processes by name==
 +
killall sends a signal to all processes running any of the specified commands . If no signal name is specified, SIGTERM is sent. To terminate all firefox process (child and parent), enter:
 +
<source lang="bash">killall processName
 +
killall firefox-bin</source>
 +
To send SIGKILL to firefox, enter:
 +
<source lang="bash">killall -s SIGKILL firefox-bin</source>
 +
==pkill - kill process==
 +
The [[pkill command]] is another command with additional options to kill process by its name, user name, group name, terminal, UID, EUID, and GID. To send a kill single to php-cgi process, enter:
 +
<source lang="bash">pkill -KILL php-cgi</source>
 +
The above example will kill all users php-cgi process. However, -u option will kill only processes whose effective user ID is set to vivek:
 +
<source lang="bash">pkill -KILL -u vivek php-cgi</source>
 +
pkill will send the specified signal (by default SIGTERM) to each process instead of listing them on stdout.
 +
Make sshd reread its configuration file, enter:
 +
<source lang="bash">pkill -HUP sshd</source>
 
[[Category:Catching signals]][[Category:Commands]]
 
[[Category:Catching signals]][[Category:Commands]]

Revision as of 15:07, 22 September 2009

kill - send a signal to a process

The default signal for kill is TERM. To list available signals, enter:

kill -l

Sample outputs:

 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL
 5) SIGTRAP	 6) SIGABRT	 7) SIGBUS	 8) SIGFPE
 9) SIGKILL	10) SIGUSR1	11) SIGSEGV	12) SIGUSR2
13) SIGPIPE	14) SIGALRM	15) SIGTERM	16) SIGSTKFLT
17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU
25) SIGXFSZ	26) SIGVTALRM	27) SIGPROF	28) SIGWINCH
29) SIGIO	30) SIGPWR	31) SIGSYS	34) SIGRTMIN
35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3	38) SIGRTMIN+4
39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12
47) SIGRTMIN+13	48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14
51) SIGRTMAX-13	52) SIGRTMAX-12	53) SIGRTMAX-11	54) SIGRTMAX-10
55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7	58) SIGRTMAX-6
59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX	

kill command Examples

The kill command can send all of the above singles to commands and process. However, commands only give response if they are programmed to recognize those singles. Particularly useful signals include:

  1. SIGHUP (1) - Hangup detected on controlling terminal or death of controlling process.
  2. SIGINT (2) - Interrupt from keyboard.
  3. SIGKILL (9) - Kill signal i.e. kill running process.
  4. SIGSTOP (19) - Stop process.
  5. SIGCONT (18) - Continue process if stopped.

To send kill single to PID # 1234 use:

kill -9 1234

OR

kill -KILL 1234

OR

kill -SIGKILL 1234

killall - kill processes by name

killall sends a signal to all processes running any of the specified commands . If no signal name is specified, SIGTERM is sent. To terminate all firefox process (child and parent), enter:

killall processName
killall firefox-bin

To send SIGKILL to firefox, enter:

killall -s SIGKILL firefox-bin

pkill - kill process

The pkill command is another command with additional options to kill process by its name, user name, group name, terminal, UID, EUID, and GID. To send a kill single to php-cgi process, enter:

pkill -KILL php-cgi

The above example will kill all users php-cgi process. However, -u option will kill only processes whose effective user ID is set to vivek:

pkill -KILL -u vivek php-cgi

pkill will send the specified signal (by default SIGTERM) to each process instead of listing them on stdout. Make sshd reread its configuration file, enter:

pkill -HUP sshd