Note: The following material was developed by Claude Cantin of the Research Computing Support Group of the National Research Council of Canada, and is used with his permission.
Processes may be sent signals using either the kill command, or a control key combination such as CTRL-C. The interrupt signal (CTRL-C) usually kills the process.
The trap command typically appears as one of the first lines in the shell script. It contains the commands to be executed when a signal is detected as well as what signals to trap.
#!/bin/sh TMPFILE=/usr/tmp/junk.$$ trap 'rm -f $TMPFILE; exit 0' 1 2 15 . . .
Upon receiving signals 1, 2 or 15, $TMPFILE would be deleted and the script would terminate the shell script normally. This shows how trap may be used to clean up before exiting.
#!/bin/sh TMPFILE=/usr/tmp/junk.$$ trap '' 0 1 2 . . .
The above example shows how trap may be used to ignore specific signals (0, 1 and 2).
NOTE that when the signal is received, the command currently being executed is interrupted, and execution flow continues at the next line of the script.