Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

trap - trap signals

&pagelevel(4)&pagelevel

The POSIX shell built-in trap is used to define how the current shell is to react to subsequent signals received. You can thus use trap in shell scripts to have specific "clean-up" tasks performed before the script terminates, or to define positions at which no interrupts are to take place. trap has two functions:

  • trap defines how the shell is to react to a signal:

    • The shell executes the commands specified in the trap command line.
      Any command possibly terminated earlier will not be resumed after the command list is executed. Shell scripts are resumed with the command that follows the one that was interrupted.

    • The shell ignores the specified signal.

    • The shell reverts to default actions for the specified signals. trap can be used to reset signal handling to the default behavior.

  • trap displays all signals for which signal handling defaults were modified in the current shell.


Syntax


trap[ command_list signal_number| signal_name ...]

command_list

determines how the shell is to react to the signals specified, i.e. whether it is to

  • execute commands if the signal specified is received

  • ignore the specified signal

  • revert to default actions for the signal specified.

Executing commands

You can specify one or more commands for command_list. These commands are to be executed if the signal specified is received. If you specify more than one command, you should separate these by semicolons. The semicolon must be quoted to prevent immediate interpretation by the shell.

The command list must be a single argument, so it must always be enclosed in single quotes '...' or double quotes "..." if argument separators or semicolons appear in it.

If the given command_list is not a null string, the signal handling behavior it defines will only be valid in the current shell. trap must be explicitly called in each subshell; otherwise, default actions are automatically restored (see the section “Signal handling in the shell”).

It is relevant to note here that the specified commands are interpreted twice by the shell:

  • once when the shell sets the trap, and

  • once when the corresponding signal occurs, and the shell invokes trap to execute the defined commands.

It can thus make a difference whether single or double quotes are used:

'command_list'
Special characters are not interpreted until the shell actually invokes trap and executes the commands. Shell variables are thus evaluated only when the trap routines are executed.

"command_list"
The characters $, \, and ` ...` are interpreted by the shell when the trap is set. Shell variables are often still undefined at this stage, however.

If you want to prevent the shell script from being executed further when a signal has been received, specify exit as the last command in the command list.

Ignoring signals

If a null string ("" or ’’) is specified for command_list the specified signals are ignored.

The corresponding signals are also ignored in every subshell.

Resetting signal handling to default

If command_list is not specified the shell reverts to default actions for the signals specified (see the section “Signal handling in the shell”).

signal_number | signal_name

Number or name identifying the signal for which the shell is to execute the defined actions (see signal() [4]). Several blank-separated signals may be specified. command_list will then be executed as soon as any one of these signals is received.

The following signals (signal number/signal name) are relevant for the shell:
0 / EOF (Terminate the shell)
1 / SIGHUP
2 / SIGINT
3 / SIGQUIT
15 / SIGTERM

If 0 is specified for signal_number this causes the specified command_list to be executed on exiting the current shell; 0 is not a signal. This means

  • if you have called trap interactively, command_list will be executed as soon as you press CTRL+D.

  • if trap is specified in a shell script, command_list will be executed after this script.

Signal 9 (SIGKILL) always kills the process, so trap '' 9 will not work.

No argument specified

The trap command with no arguments prints the list of commands associated with each signal number for which signal handling has been altered in the current shell. The list is written to standard output in the following format:

signal_number: command_list...

Note, however, that only the signal numbers for which the default actions were modified earlier (with the command trap) are displayed (see the section “Signal handling in the shell” below).

Signal handling in the shell

A process can receive a signal at any time during its execution. Such signals may either be generated by the process itself, by another process or by the user at the terminal (e.g. by pressing CTRL+C. The shell then reacts to the signal in one of the following ways:

  • It ignores the incoming signal.

  • It terminates.

  • It calls a function to deal with the signal.

The following signals (number/name) are relevant to users who have used rlogin to access the POSIX shell:

1 / SIGHUP

Disconnection of link to terminal

2 / SIGINT

CTRL+C

3 / SIGQUIT

CTRL+L

9 / SIGKILL

The command kill -9 PID, where PID is the process identification number of the corresponding shell

15 / SIGTERM

The command kill -15 PID, where PID is the process identification number of the corresponding shell

Depending on whether or not you have defined signal handling with trap, the interactive shell (IS) or the shell script in the background (SSB) will react to these signals as follows:

Signal number 1

IS: ignore
SSB: abort

Signal number 2

IS: take defined action after next command or after pressing the enter key, otherwise: ignore
SSB: ignore

Signal number 3

IS: take defined action after next command or after pressing the enter key, otherwise: ignore
SSB: ignore

Signal number 9

IS: abort
SSB: abort

Signal number 15

IS: ignore
SSB: ignore


Depending on whether or not you have defined signal handling with trap, a shell script (shs) or the current foreground process in a shell script (fgp) will react as follows (where PID is the process ID of the shell script):

Signal handling undefined for shell script and current foreground process in shell script

kill -1 PID

shs: abort immediately
fgp: continue running

kill -2 PID

shs: abort after normal termination of foreground process
fgp: continue running

CTRL+C

shs: abort immediately
fgp: abort immediately

kill -3 PID

shs: abort after normal termination of foreground process
fgp: continue running

CTRL+L

shs: abort immediately
fgp: abort immediately, writing core dump to disk

kill -9 PID

shs: abort immediately
fgp: continue running

kill -15 PID

shs: abort after normal termination of foreground process
fgp: continue running

Signal handling undefined for shell script, defined for current foreground process in shell script

kill -1 PID

shs: abort immediately
fgp: continue running

kill -2 PID

shs: abort after normal termination of foreground process
fgp: take defined action

CTRL+C

shs: abort after normal termination of foreground process
fgp: take defined action

kill -3 PID

shs: abort after normal termination of foreground process
fgp: take defined action

CTRL+L

shs: abort after normal termination of foreground process
fgp: take defined action

kill -9 PID

shs: abort immediately
fgp: continue running

kill -15 PID

shs: abort after normal termination of foreground process
fgp: take defined action

Signal handling defined for shell script, undefined for current foreground process in shell script

kill -1 PID

shs: take defined action after normal termination of foreground process; then resume execution
fgp: continue running (signal not delivered)

kill -2 PID

shs: take defined action after normal termination of foreground process; then resume execution
fgp: continue running (signal not delivered)

CTRL+C

shs: take defined action; then resume execution
fgp: abort immediately

kill -3 PID

shs: take defined action after normal termination of foreground process; then resume execution
fgp: continue running (signal not delivered)

CTRL+L

shs: take defined action; then resume execution
fgp: abort immediately, writing core dump to disk

kill -9 PID

shs: abort immediately
fgp: continue running

kill -15 PID

shs: take defined action after normal termination of foreground process; then resume execution
fgp: continue running (signal not delivered)

Signal handling defined for shell script and current foreground process in shell script

kill -1 PID

shs: take defined action after normal termination of foreground process; then resume execution
fgp: continue running (signal not delivered)

kill -2 PID

shs: take defined action after normal termination of foreground process; then resume execution
fgp: continue running (signal not delivered)

CTRL+C

shs: take defined action; then resume execution
fgp: take defined action; then resume execution

kill -3 PID

shs: take defined action after normal termination of foreground process; then resume execution
fgp: continue running (signal not delivered)

CTRL+L

shs: take defined action; then resume execution
fgp: take defined action; then resume execution

kill -9 PID

shs: abort immediately
fgp: continue running

kill -15 PID

shs: take defined action after normal termination of foreground process; then
resume execution
fgp: continue running (signal not delivered)


The signal() function can be used in C programs to define how signal handling is to be implemented (see signal() [4]).

Locale

The following environment variables affect the execution of trap:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other.

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Example 1

To ensure that signal 2 is ignored in a shell script, the following line is included in it:

trap '' 2

The two single quotes, i.e. the null string, cause signal 2 to be ignored. This means that the shell script cannot be terminated externally by CTRL+C or by the command kill -2 process_id.

Example 2

Use of the command trap in an interactive shell:

$ trap 'echo Last logged out: `date` >>$HOME/logfile' 0

$ trap

0, echo Last logged out: `date` >>$HOME/logfile

$ CTRL+C

...

login: rose

Password:

$ cat logfile

Last logged out: Mon Mar 9 18:17:23 MEZ 2009

The trap in the above example defines that a message is to be written to the file $HOME/logfile on exiting the current shell. The command list must be enclosed in single quotes here, since date is only to be executed when the shell terminates.

Example 3

The shell script traptest illustrates how temporary files can be deleted if signals are received during execution. The script contains the following lines (without the line numbers):

1  TMP=/usr/rtmp/$$
2  trap "rm -f $TMP; trap 0; exit 1" 1 2 3 15
3  trap "rm -f $TMP; exit 0" 0
4  ls > $TMP
   .
   .
   .

Line 1:

The file name /usr/rtmp/$$ is assigned to the variable TMP. The shell substitutes the process ID of the current shell for $$, thus creating a unique file name.

Line 2:

The command list is enclosed in double quotes, since the TMP variable has already been assigned a value. The following actions are defined for signals 1, 2, 3, and 15: delete the file /usr/rtmp/$$, reset the end of script definition (0) (see line 3), and terminate the script with exit status1.

Line 3:

0 is the only signal-number specified, i.e. the definition for the end of the script is: delete the file /usr/rtmp/$$, and terminate the script with exit status 0. This definition must be reset in line 2 with trap 0, since the command exit terminates the shell script. Otherwise, line 3 would cause the script to terminate with exit status 0.

Line 4:

The file /usr/rtmp/$$ is created. This line must not precede the trap command; otherwise, if the script terminates before the shell executes the first trap command, the file will not be deleted.

In this case the command exit should be explicitly invoked at the end of the command list, otherwise the commands that follow trap in the script could execute in an undefined state.

See also

exit, kill
signal() [4]