Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Part III
Advanced Topics

HOUR

19  Dealing with Signals
20  Debugging
21  Problem Solving with Functions
22  Problem Solving with Shell Scripts
23  Scripting for Portability
24  Shell Programming FAQs

Hour 19
Dealing with Signals


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.

Because signals can arrive at any time during the execution of a script, they add an extra level of complexity to shell scripts. Scripts must account for this fact and include extra code that can determine how to respond appropriately to a signal regardless of what the script was doing when the signal was received.

In this chapter you will look at the following topics:

  The different types of signals encountered in shell programming
  How to deliver signals using the kill command
  Handling signals
  How to use signals within your script

How Are Signal Represented?

Each type of event is represented by a separate signal. Each signal is only a small positive integer. The signals most commonly encountered in shell script programming are given in Table 19.1. All the listed signals are available on all versions of UNIX.

Table 19.1 Important Signals for Shell Scripts

Name Value Description

SIGHUP 1 Hang up detected on controlling terminal or death of controlling process
SIGINT 2 Interrupt from keyboard
SIGQUIT 3 Quit from keyboard
SIGKILL 9 Kill signal
SIGALRM 14 Alarm Clock signal (used for timers)
SIGTERM 15 Termination signal

In addition to the signals listed in Table 19.1, you might occasionally see a reference to signal 0, which is more of a shell convention than a real signal. When a shell script exits either by using the exit command or by executing the last command in the script, the shell in which the script was running sends itself a signal 0 to indicate that it should terminate.

Getting a List of Signals

All the signals understood by your system are listed in the C language header file signal.h. The location of this file varies between UNIX flavors. Some common locations are

  Solaris and HPUX: /usr/include/sys/signal.h
  Linux: /usr/include/asm/signal.h

Some vendors provide a man page for this file which you can view with one of the following commands:

  In Linux: man 7 signal
  In Solaris: man -s 5 signal
  In HP-UX: man 5 signal

Another way that your system can understand a list of signals is to use the -l option of the kill command. For example on a Solaris system the output is:

$ kill -l
1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGEMT       8) SIGFPE
 9) SIGKILL     10) SIGBUS      11) SIGSEGV     12) SIGSYS
13) SIGPIPE     14) SIGALRM     15) SIGTERM     16) SIGUSR1
17) SIGUSR2     18) SIGCHLD     19) SIGPWR      20) SIGWINCH
21) SIGURG      22) SIGIO       23) SIGSTOP     24) SIGTSTP
25) SIGCONT     26) SIGTTIN     27) SIGTTOU     28) SIGVTALRM
29) SIGPROF     30) SIGXCPU     31) SIGXFSZ     32) SIGWAITING
33) SIGLWP      34) SIGFREEZE   35) SIGTHAW     36) SIGCANCEL
37) SIGLOST

The actual list of signals varies between Solaris, HP-UX, and Linux.

Default Actions

Every signal, including those listed in Table 19.1, has a default action associated with it. The default action for a signal is the action that a script or program performs when it receives a signal.

Some of the possible default actions are

  Terminate the process.
  Ignore the signal.
  Dump core. This creates a file called core containing the memory image of the process when it received the signal.
  Stop the process.
  Continue a stopped process.

The default action for the signals that you should be concerned about is to terminate the process. Later in this chapter you will look at how you can change the default action performed by a script with a signal handler.


Previous Table of Contents Next