NEXT UP previous
Next: Questions

Signals

You already know that you can stop the execution of most running programs by interrupting them from the keyboard, usually with Ctrl-c.

What actually happens is that the part of the Linux kernel software that controls the keyboard, sends a message to the running process. This message is called a signal. In fact, there are about 30 different signal types that can be sent to a Linux process, each of which is generated by some specific hardware or software condition. In addition to the normal mechanisms for generating and sending signals, the kill command can also be used to send any signal to a process at will.

Each of the signals is given a small integer value which is used as a command line switch to kill, along with the process ID of the process to which the signal should be sent. The general format of the kill command is:

	kill [-sig] pid

where sig is the number of the signal to send and pid is the process ID to which it should be sent. The square brackets just indicate that the specification of the pid is optional - the brackets themselves should not be typed. If a signal number is not specified to the kill command then the terminate signal is used by default.

Some of the more common signals and their numeric values are:

SignalValueNotes
hangup1used to kill your processes when you logout
interrupt2generated from the keyboard (Ctrl-c)
quit3generated from the keyboard (Ctrl-\)
kill9sure process termination - cannot be ignored
alarm14generated at the end of an alarmO system call
terminate15default signal sent by kill command

When a process receives a signal, it can normally do one of three different things:

  1. ignore the signal;
  2. accept the signal's default action;
  3. execute some code to deal with the signal.

The default action for most signals is just to terminate the receiving process (this is why Ctrl-c from the keyboard will usually terminate a process). To change from this default action requires a process to perform some positive setup procedure. In the case of the kill signal (which is signal number 9) this default action cannot be changed, so that a process which receives a kill signal will be terminated.

From inside a shell script it is possible to trap a signal and then to get a sequence of commands executed when that signal is received. This is done with the trap command. There are three basic forms for the trap command, one for each of the three different signal response types:

	trap "commands" signal_list

In this form, trap will execute the commands in the quotes when any of the signals corresponding to the numbers in the signaLlist is received by the script.

To restore signals to their default action, use the trap command in the form:

	trap signal_list

without any commands specified.

The third form, which allows signals to be ignored, has the format:

	trap "" signal_list

which just specifies a null commands string.

A typical example of the use of trap is to clean up temporary files when ah normal termination of a program occurs which might otherwise leave these files cluttering up your file space. The following code fragment assumes that a unique temporary file, called /tmp/tmp$$, is to be created for use within the script and that if abnormal termination of the script occurs, on receipt of either an interrupt or a quit signal, the temporary file needs to be deleted as a clean up operation before the script terminates:

	trap "rm -f /tmp/tmp$$; exit 0" 2 3
	touch /tmp/tmp$$
	#
	# Rest of shell script here
	#
	trap 2 3

This script starts by setting itself up to trap signals 2 and 3 (Ctrl-c and Ctrl-\ from the keyboard). If either of these two signals occurs, it will usually be because the user is trying to break into the script and abort its operation. After the trap has executed, signals 2 and 3 will cause execution of the commands:

	rm -f /tmp/tmp$$; exit 0

The rm command will remove the temporary file and.the exit command then terminates the script. Notice that multiple commands can be entered on the same line if they are separated by semi-colons.

Once the trap command has executed, the rest of the shell script will execute as normal, starting with the creation of the temporary file by the touch command. The last line of this script fragment restores the operation of signals 2 and 3 to their default state, presumably after the temporary file is no longer needed and has thus already been deleted.


NEXT UP previous
Next: Questions