Previous | Table of Contents | Next |
There are several methods of delivering signals to a program or script. One of the most common is for a user to type CONTROL-C or the INTERRUPT key while a script is executing. In this case a SIGINT is sent to the script and it terminates.
The other common method for delivering signals is to use the kill command as follows:
kill -signal pid
Here signal is either the number or name of the signal to deliver and pid is the process ID that the signal should be sent to.
TERM
In previous chapters you looked at the kill command without the signal argument. By default the kill command sends a TERM or terminates a signal to the program running with the specified pid. Recall from Chapter 6, Processes, that a PID is the process ID given by UNIX to a program while it is executing. Thus the commands
kill pid kill -s SIGTERM pid
are equivalent.
Now look at a few examples of using the kill command to deliver other signals.
HUP
The following command
$ kill -s SIGHUP 1001
sends the HUP or hang-up signal to the program that is running with process ID 1001. You can also use the numeric value of the signal as follows:
$ kill -1 1001
This command also sends the hang-up signal to the program that is running with process ID 1001. Although the default action for this signal calls for the process to terminate, many UNIX programs use the HUP signal as an indication that they should reinitialize themselves. For this reason, you should use a different signal if you are trying to terminate or kill a process.
QUIT and INT
If the default kill command cannot terminate a process, you can try to send the process either a QUIT or an INT (interrupt) signal as follows:
$ kill -s SIGQUIT 1001
or
$ kill -s SIGINT 1001
One of these signals should terminate a process, either by asking it to quit (the QUIT signal) or by asking it to interrupt its processing (the INT signal).
kill
Some programs and shell scripts have special functions called signal handlers that can ignore or discard these signals. To terminate such a program, use the kill signal:
$ kill -9 1001
Here you are sending the kill signal to the program running with process ID 1001. In this case you are using the numeric value of the signal, instead of the name of the signal. By convention, the numeric value of the kill signal, 9, is always used for it.
The kill signal has the special property that it cannot be caught, thus any process receiving this signal terminates immediately with extreme prejudice. This means that a process cannot cleanly exit and might leave data it was using in a corrupted state. You should only use this signal to terminate a process when all the other signals fail to do so.
A program or script can handle a signal in three different ways:
The first method for dealing with a signal requires no additional code in your shell script. This is the default behavior for all shell scripts that do not explicitly handle signals. All the scripts that you have looked at so far handle signals using this method.
In this section you will look at the second and third methods of dealing with signals.
The trap command sets and unsets the actions taken when a signal is received. Its syntax is
trap name signals
Here name is a list of commands or the name of a shell function to execute when a signal in the list of specified signals is received. If name is not given, trap resets the action for the given signals to be the default action.
There are three common uses for trap in shell scripts:
You will look at a fourth use, setting up a timer, later in this chapter.
Previous | Table of Contents | Next |