< Previous | Contents | Next >
Sending Signals To Processes With kill
The kill command is used to send signals to programs. Its most common syntax looks like this:
kill [-signal] PID...
kill [-signal] PID...
If no signal is specified on the command line, then the TERM (Terminate) signal is sent by default. The kill command is most often used to send the following signals:
Table 10-4: Common Signals
Number | Name | Meaning |
1 | HUP | Hangup. This is a vestige of the good old days when terminals were attached to remote |
computers with phone lines and modems. The signal is used to indicate to programs that the controlling terminal has “hung up.” The effect of this signal can be demonstrated by closing a terminal session. The foreground program running on the terminal will be sent the signal and will terminate. | ||
This signal is also used by many daemon programs to cause a reinitialization. This means that when a daemon is sent this signal, it will restart and re-read its configuration file. The Apache web server is an example of a daemon that uses the HUP signal in this way. | ||
2 | INT | Interrupt. Performs the same function as the Ctrl-c key sent from the terminal. It will usually terminate a program. |
9 | KILL | Kill. This signal is special. Whereas programs may choose to handle signals sent to them in different ways, including ignoring them all together, the KILL signal is never actually sent to the target program. Rather, the kernel immediately terminates the process. When a process is terminated in this manner, it is given no opportunity to “clean up” after itself or save its work. For this reason, the KILL signal should only be used as a last resort when other termination signals fail. |
15 | TERM | Terminate. This is the default signal sent by the kill command. If a program is still “alive” enough to receive signals, it will terminate. |
18 | CONT | Continue. This will restore a process after a STOP signal. |
19 | STOP | Stop. This signal causes a process to pause without terminating. Like the KILL signal, it is not sent to the target process, and thus it cannot be ignored. |
Let's try out the kill command:
[me@linuxbox ~]$ xlogo &
[1] 13546
[me@linuxbox ~]$ kill -1 13546
[1]+ Hangup xlogo
[me@linuxbox ~]$ xlogo &
[1] 13546
[me@linuxbox ~]$ kill -1 13546
[1]+ Hangup xlogo
In this example, we start the xlogo program in the background and then send it a HUP signal with kill. The xlogo program terminates and the shell indicates that the back- ground process has received a hangup signal. We may need to press the enter key a cou- ple of times before the message appears. Note that signals may be specified either by number or by name, including the name prefixed with the letters “SIG”:
[me@linuxbox ~]$ xlogo &
[1] 13601
[me@linuxbox ~]$ kill -INT 13601 [1]+ Interrupt xlogo [me@linuxbox ~]$ xlogo &
[1] 13608
[me@linuxbox ~]$ kill -SIGINT 13608
[1]+ Interrupt xlogo
[me@linuxbox ~]$ xlogo &
[1] 13601
[me@linuxbox ~]$ kill -INT 13601 [1]+ Interrupt xlogo [me@linuxbox ~]$ xlogo &
[1] 13608
[me@linuxbox ~]$ kill -SIGINT 13608
[1]+ Interrupt xlogo
Repeat the example above and try out the other signals. Remember, we can also use job- specs in place of PIDs.
Processes, like files, have owners, and you must be the owner of a process (or the supe - ruser) in order to send it signals with kill.
In addition to the list of signals above, which are most often used with kill, there are other signals frequently used by the system. Here is a list of other common signals:
Table 10-5: Other Common Signals
Number | Name | Meaning |
3 | QUIT | Quit. |
11 | SEGV | Segmentation Violation. This signal is sent if a program makes illegal use of memory, that is, it tried to write somewhere it was not allowed to. |
20 | TSTP | Terminal Stop. This is the signal sent by the terminal when the Ctrl-z key is pressed. Unlike the STOP signal, the TSTP signal is received by |
the program but the program may choose to ignore it.
28 WINCH Window Change. This is the signal sent by the
system when a window changes size. Some programs , like top and less will respond to this signal by redrawing themselves to fit the new window dimensions.
For the curious, a complete list of signals can be seen with the following command:
[me@linuxbox ~]$ kill -l
[me@linuxbox ~]$ kill -l