Saturday 15 December 2012

Unix - Signals and Traps

Signals are software interrupts sent to a program to indicate that an important event has occurred. The events can vary from user requests to illegal memory access errors. Some signals, such as the interrupt signal, indicate that a user has asked the program to do something that is not in the usual flow of control.

The following are some of the more common signals you might encounter and want to use in your programs:
Signal NameSignal NumberDescription
SIGHUP1Hang up detected on controlling terminal or death of controlling process
SIGINT2Issued if the user sends an interrupt signal (Ctrl + C).
SIGQUIT3Issued if the user sends a quit signal (Ctrl + D).
SIGFPE8Issued if an illegal mathematical operation is attempted
SIGKILL9If a process gets this signal it must quit immediately and will not perform any clean-up operations
SIGALRM14Alarm Clock signal (used for timers)
SIGTERM15Software termination signal (sent by kill by default).

List of Signals:

There is an easy way to list down all the signals supported by your system. Just issue kill -l command and it would display all the supported signals:

[sankar]$ kill -l
 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) SIGRTM
 

Trapping Signals:

Trapping these signals is quite easy, and the trap command has the following syntax:
$ trap commands signals
Here command can be any valid Unix command, or even a user-defined function, and signal can be a list of any number of signals you want to trap.
There are three common uses for trap in shell scripts:

1.Clean up temporary files
2. Ignore signal 

Cleaning Up Temporary Files:

As an example of the trap command, the following shows how you can remove some files and then exit if someone tries to abort the program from the terminal:


$ trap "rm -f $WORKDIR/work1$$ $WORKDIR/dataout$$; exit" 2

Ignoring Signals:

If the command listed for trap is null, the specified signal will be ignored when received. For example, the command:
$ trap '' 2
Specifies that the interrupt signal is to be ignored. You might want to ignore certain signals when performing some operation that you don't want interrupted. You can specify multiple signals to be ignored as follows:

 
$ trap '' 1 2 3 15

 

 

1 comment: