Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

UNIX Signals and Traps


May 23, 2021 UNIX Getting started


Table of contents


Signals and Traps

A signal is a software outage sent to a program that indicates an important event that has occurred. T hese events can range from user requests to illegal memory access errors. These signals, such as interrupt signals, indicate that the user is asking the program to do something out of the ordinary process control.

Here are some common signals you might encounter that you need to use in your program:

The signal name The signal value Describe
SIGHUP 1 The control terminal terminates unexpectedly or is issued at the end of the control process
SIGINT 2 Issued when the user sends an interrupt signal (Ctrl-C).
SIGQUIT 3 Issued when the user sends an exit signal (Ctrl-D).
SIGFPE 8 Issued when an illegal mathematical operation is performed.
SIGKILL 9 A process gets this signal, it must exit immediately, and no purge can be performed.
SIGALRM 14 Clock signal (for timers)
SIGTERM 15 The software terminates the signal (the kill command produces this signal by default).

The list of signals

There is an easy way to list all the signals supported by your system. All supported signals for the system can be displayed using the command kill -l alone:

    $ 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) SIGRTMAX-3  62) SIGRTMAX-2
    63) SIGRTMAX-1  64) SIGRTMAX

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

The default action

Each signal has a default action that follows it. The default action for a signal is how it performs when a script or program receives a signal.

These possible default actions are:

  • The process is terminated.

  • Ignore the signal.

  • Kernel image dump. When a signal is received, this action creates a file called core that contains a memory image of the process.

  • Stop the process.

  • Continue the stopped process.

Send a signal

There are several ways to signal a program or script. The most common of these users is to press Ctrl-C or INTERRUPT while the script is running.

When you press the Ctrl-C key, the signal SIGINT is sent to the script, which will be terminated based on the default action previously defined.

Another common way to transmit signals is to use the kill command, as follows:

    $ kill -signal pid

The signal here is either a signal value or a signal name. the ID of the process to which the signal is to be sent. For example:

    $ kill -1 1001

Send a HUP signal (i.e. an unexpected termination signal) to a program running process ID 1001. Send a kill signal to the same process with the following command:

    $ kill -9 1001

This command kills programs that run a process ID of 1001.

Capture the signal

When a Shell program is in the process of executing, you press the Ctrl-C or Break key at the terminal, usually the program terminates immediately and your command prompts back. B ut that may not always be what you expect. For example, this might leave a bunch of un cleaned temporary files.

It is easy to capture these signals, and the syntax of the capture command (trap) is as follows:

    $ trap commands signals

The commands here can be any valid UNIX command, or even a user-defined function.

Signals can be a list of any number of signals you want to capture.

In shell scripts, traps have three common uses:

  1. Clear temporary files
  2. Ignore the signal

Clear temporary files

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

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

If the program receives a signal with a signal value of 2, the trap command will be executed. Starting with the execution of the trap on the shell program, the two files, work1$$ and dataout$$$ are automatically deleted.

So if the user interrupts the program, the trap will be executed and you can make sure that both files are cleaned up. T his exit command after rm, its existence is necessary. Without it, the program continues at its break point (that is, the moment the signal is received).

Signal value 1 is generated by a hang-up: either someone intentionally hangs up the terminal or the terminal is accidentally disconnected.

In this case, by increasing the signal value of 1 to the signal list, you can delete the two specified files by modifying the trap command above in this way:

    $ trap "rm $WORKDIR/work1$$ $WORKDIR/dataout$$; exit" 1 2

These files will now be deleted if the terminal is suspended or if Ctrl-C is pressed.

The specified trap commands must be enclosed in quotation marks if they contain more than one command. Also note that when the trap command is executed, the shell scans the command line once, and when one of the signals in the signal list is received, another scan is performed.

So in the example above, the values workDIR and $$ will be replaced when the trap command is executed. If you want this substitution to occur only when signal values 1 or 2 are received, you can draw multiple commands in single quotes:

    $ trap 'rm $WORKDIR/work1$$ $WORKDIR/dataout$$; exit' 1 2

Ignore the signal

If the command field of the trap command is empty, the signal is ignored when the specified signal is received. For example, one of the following commands:

    $ trap '' 2

The specified interrupt signal is ignored. W hen you perform something that you might not want to be interrupted, you may want to ignore certain signals. You can specify multiple signals to ignore as follows:

    $ trap '' 1 2 3 15

Note that if a signal is to be ignored, then the first argument must be specified empty, which is not equivalent to the following command, which has its own separate meaning:

    $ trap  2

If you ignore a signal, all your subseats will also ignore it. However, if you specify the action taken to receive a signal, all subs shells will take the same action when they receive the signal.

Reset Traps

When you change the default action after receiving the signal, you can reset the default action back with a simple trap command that omits the first argument.

Then the command

    $ trap 1 2

Reset the actions taken after signals 1 and 2 are received to their original default actions.