45.8 Handling Signals to Child ProcessesThe Bourne shell trap command ( 44.12 ) controls what the shell does when it gets an interrupt or signal (from the kill ( 38.10 ) command, from a keyboard character like CTRL-c, and so on). To run an external command ( 1.10 ) - like an editor or a simple command such as sort -the shell starts a child process ( 38.3 ) (subprocess). If the program running in the child process wants to handle its own signals, the parent shell should probably pass signals on to the child process. For example, you might run vi as a child process and want to send a CTRL-c to stop vi from what it's doing, but not want the CTRL-c to kill the parent shell script.
When the parent process gets a signal, should it die or keep running?
Should the child get the signal or not? The Bourne shell gives you a
fair amount of flexibility in signal handling. The bad news is that
most
sh
manual pages don't say much about this. And no manual
page I've seen explains a useful choice: using the
Because so much of this is undocumented, I won't try to give you "the answers" for how it should work with your shell. Instead, here are two shell scripts that let you experiment with your shell's signal handling. One script, named parent , starts the second script, child . The child script sets some traps, then starts sleep ( 40.2 ) so it'll be there when you send a signal. This lets you use CTRL-c or other interrupts, if parent is running in the foreground - or the kill command with signal numbers, if you've put parent in the background. You can edit the trap lines in the two scripts to test the setup you want to use. Here's an example. I'll start parent in the background from the C shell, then send it a signal 1 ("hangup" signal):
Now, the scripts:
% Even with this help, the way signal handling works might not be too clear. For more on signal handling, see a book that covers UNIX internals on your system. - |
|