trap(1) trap(1)
NAME
trap - define signal handling
SYNOPSIS
trap[ commandlist] signalnumber ... Format 1
trap Format 2
DESCRIPTION
The 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 (Format 1):
- 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 (Format 2).
Format 1: Define signal handling
trap[ commandlist] signalnumber ...
commandlist
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.
Page 1 Reliant UNIX 5.44 Printed 11/98
trap(1) trap(1)
Executing commands
You can specify one or more commands for commandlist (e.g. trap
pwd 2). 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 argu-
ment separators or semicolons appear in it.
If the given commandlist is not a null string, the signal han-
dling behavior it defines will only be valid in the current
shell. trap must be explicitly called in each subshell; other-
wise, default actions are automatically restored.
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:
'commandlist'
Special characters are not interpreted until the shell actu-
ally invokes trap and executes the commands. Shell variables
are thus evaluated only when the trap routines are executed.
"commandlist"
The characters $, \, and `...` are interpreted by the shell
when the trap is set. Shell variables are often still unde-
fined 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 commandlist the
specified signals are ignored (e.g. trap "" 2).
The corresponding signals are also ignored in every subshell.
Page 2 Reliant UNIX 5.44 Printed 11/98
trap(1) trap(1)
Resetting signal handling to default
If commandlist is not specified (e.g. trap 2), the shell reverts
to default actions for the signals specified.
signalnumber
Number identifying the signal for which the shell is to execute
the defined actions [see signal(5)]. Several blank-separated sig-
nals may be specified. commandlist will then be executed as soon
as any one of these signals is received.
In the Korn shell ksh you can specify the signal names instead of
the signal numbers.
The following signals are relevant for the shell:
_________________
| 0 | EXIT |
|_____|__________|
| 1 | SIGHUP |
|_____|__________|
| 2 | SIGINT |
|_____|__________|
| 3 | SIGQUIT |
|_____|__________|
| 6 | SIGABRT |
|_____|__________|
| 9 | SIGKILL |
|_____|__________|
| 14 | SIGALRM |
|_____|__________|
| 15 | SIGTERM |
|_____|__________|
If 0 is specified for signalnumber this causes the specified
commandlist to be executed on exiting the current shell; 0 is
not a signal. This means
- if you have called trap interactively, commandlist will be
executed as soon as you press the CTRL-D key.
- if trap is specified in a shell script, commandlist will be
executed after this script.
Signal 9 (SIGKILL) always kills the process, so trap '' 9 will
not work.
Signal 11 (SIGSEGV) cannot be specified with trap.
Page 3 Reliant UNIX 5.44 Printed 11/98
trap(1) trap(1)
Format 2: Display changed signal handling routines
trap
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 stan-
dard output in the following format:
signalnumber: commandlist
or in the Korn shell ksh:
trap -- commandlist signal
Only the signal numbers for which default actions were modified
earlier (with trap) are displayed.
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 pro-
cess or by the user at the terminal (e.g. by pressing DEL). 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 are relevant for the shell:
______________________________________________________________________
| Signal | Signal | Cause |
| number | | |
|________|__________|_________________________________________________|
| 1 | SIGHUP | Disconnection of link to terminal |
|________|__________|_________________________________________________|
| 2 | SIGINT | DEL |
|________|__________|_________________________________________________|
| 3 | SIGQUIT | CTRL-\ |
|________|__________|_________________________________________________|
| 9 | SIGKILL | The command kill -9 PID, where PID is the pro- |
| | | cess identification number of the corresponding|
| | | shell |
|________|__________|_________________________________________________|
| 15 | SIGTERM | The command kill -15 PID, where PID is the pro-|
| | | cess identification number of the corresponding|
| | | shell |
|________|__________|_________________________________________________|
Page 4 Reliant UNIX 5.44 Printed 11/98
trap(1) trap(1)
LOCALE
The LCMESSAGES environment variable governs the language in which
message texts are displayed. If LCMESSAGES is undefined or is defined
as the null string, it defaults to the value of LANG. If LANG is like-
wise undefined or null, the system acts as if it were not internation-
alized.
EXAMPLES
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 exter-
nally by the DEL key or by the command kill -2 processid.
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-D
.
.
.
login: rose
Password:
$ cat logfile
Last logged out: Mon Mar 20 22:17:23 MEZ 1989
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
.
.
Page 5 Reliant UNIX 5.44 Printed 11/98
trap(1) trap(1)
.
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. Oth-
erwise, 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.
NOTES
Some differences in behavior may occur when using trap, depending on
which shell is being used. The possible differences are not described
specifically.
SEE ALSO
exit(1), kill(1), ksh(1), sh(1), signal(5).
Page 6 Reliant UNIX 5.44 Printed 11/98