Main index

Introducing UNIX and Linux


Advanced shell programming

Overview
Sending and trapping signals
      Signal names
Functions
Aliases
The 'exec' mechanism
The 'eval' mechanism
Sending data across networks
      Sending printable characters
      Splitting files
Makefiles
Safe programming
Setting up a terminal
More on files
Miscellaneous utilities
Summary
Exercises

Sending and trapping signals

In some circumstances you will wish that accidentally typing ctrl-C will not kill the current process. This would be true, for example, in the execution of a complex script that makes non-trivial changes to files, where your filespace would be left in a mess if the script died when only half-completed. There is a mechanism, known as trapping signals, whereby a shell takes action specified by you when it receives a signal, rather than taking the default action. The command used to intercept a signal is trap, and it is used in the following manner:

trap 'action' signal

The action is either null or a string containing a command, and the signal is one of the signal names. Create a script interrupts containing:

trap 'echo Ouch' INT
echo Beginning
sleep 10
echo ten seconds
sleep 10
echo twenty seconds
sleep 10
echo thirty seconds and ended

and execute it using sh. Try to interrupt it by typing ctrl-C at your terminal a couple of times and see what happens. You should see

sh interrupts
Beginning

ctrl-C
Ouch
ten seconds

ctrl-C
Ouch
twenty seconds

ctrl-C
Ouch
thirty seconds and ended

Similar to SIGINT is SIGQUIT. This signal can usually be generated from your terminal by typing ctrl-\. The difference between the two is that SIGQUIT will on many systems generate a coredump, that is a file named core which contains information about the interrupted command when it received the signal. The file core can be used to examine the state of the program when the signal was received. A core file can be interrogated by an experienced UNIX programmer using utilities such as dbx, but at this stage you will not be interested in its contents. A coredump is usually a big file and should be removed unless you intend to use it. Try the following:

sleep 1000 &
[1] 17465
kill -s QUIT %1
[1]+ Quit (core dumped) sleep 1000

Check which files you have using ls and you should find that one named core has now been created.

When a shell script exits, a signal is sent to it called EXIT, which can be trapped. To see which signals you have trapped, use trap with no arguments. Be careful which signals you trap - in particular, don't try KILL (or you would have difficulty using kill to destroy the process) or HUP (or unpredictable things would happen if you tried to suspend the process). If you have set a trap on a signal, you can remove it by giving it the action - (minus symbol), so:

trap - INT

will restore ctrl-C to its normal function.

Worked example

By setting a trap on your login shell, arrange to be given the message Goodbye when you logout.
Solution: The signal EXIT is sent to your login shell when you logout, so use trap to perform an echo when this signal is intercepted:

trap 'echo Goodbye' EXIT

There are various other signals, although most UNIX systems support many more.


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck