TrumanWong

trap

Capture signals and other events and execute commands.

Summary

trap [-lp] [[arg] signal_spec ...]

The main purpose

  • Used to specify the action to be taken when a signal is received.
  • Perform cleanup when script is interrupted.

Options

-l prints the signal name and the number corresponding to the signal name.
-p displays the trap command associated with each signal.

Parameters

arg: The command to be executed when the signal is received.

signal_spec: signal name or number corresponding to the signal name.

return value

Returns 0 if the expression execution result is successful, and returns 1 when the parameter signal_spec does not specify a valid value.

About signals

Signal is an inter-process communication mechanism that provides an asynchronous software interrupt to the application program, giving the application program the opportunity to accept commands (i.e. signals) sent by other programs or terminals. After the application receives the signal, there are three ways to handle it: ignore, default, or catch. After a process receives a signal, it checks the handling mechanism for the signal. If it is SIG_IGN, the signal will be ignored; if it is SIG_DFT, the system's default processing action will be adopted, usually terminating the process or ignoring the signal; if a processing function (capture) is specified for the signal, the current process will be interrupted. The task being executed is turned to execute the signal processing function, and then the interrupted task is continued after returning.

In some cases, we do not want our shell script to be interrupted while running. For example, if we write a shell script to be the default shell of a certain user, so that the user can only do a certain job after entering the system, such as For database backup, we don't want users to use Ctrl+C and other methods to enter the shell state and do things we don't want to do. This uses signal processing.

Here are some common signals you may encounter:

Signal name Number of signals Description
SIGHUP 1 This signal is issued when the user terminal connection (normal or abnormal) ends, usually when the terminal's control process ends, to notify each job in the same session, at which time they are no longer associated with the control terminal. When logging in to Linux, the system will assign a terminal (Session) to the logged-in user. All programs running in this terminal, including the foreground process group and background process group, generally belong to this Session. When the user logs out of Linux, the foreground process group and the background process that outputs to the terminal will receive the SIGHUP signal. The default action of this signal is to terminate the process, so the foreground process group and processes with terminal output in the background will be terminated. For daemons that are disconnected from the terminal, this signal is used to notify it to re-read the configuration file.
SIGINT 2 Program termination (interrupt) signal, issued when the user types Ctrl+C.
SIGQUIT 3 Similar to SIGINT, but controlled by the QUIT character (usually Ctrl+\). A core file is generated when a process exits due to receiving SIGQUIT, which is similar to a program error signal in this sense.
SIGFPE 8 Issued when a fatal arithmetic error occurs. This includes not only floating-point arithmetic errors, but also all other arithmetic errors such as overflow and division by zero.
SIGKILL 9 Used to end the program immediately. This signal cannot be blocked, processed or ignored.
SIGALRM 14 Clock timing signal, which calculates actual time or clock time. The alarm function uses this signal.
SIGTERM 15 Program end (terminate) signal. Unlike SIGKILL, this signal can be blocked and processed. It is usually used to require the program to exit normally; the kill command generates this signal by default.

example

When the shell receives the commands HUP INT PIPE QUIT TERM, the currently executing program will execute exit 1.

[root@pc root]$ trap "exit 1" HUP INT PIPE QUIT TERM

1 Clean up temporary files

Here's how to delete the file and then exit if someone tries to kill the program from the terminal:

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

Execute the shell program. If the program receives a signal of 2, then these two files (work1 and dataout) will be automatically deleted.

Add signal 1 SIGHUP:

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

2 Ignore the signal

If the command listed in the trap is empty, the specified signal will be ignored when received:

$ trap '' 2

Ignore multiple signals:

$ trap '' 1 2 3 15

3 Reset Trap

When you change the action taken when a signal is received, you can omit the first parameter to reset to the default behavior.

$ trap 1 2

Notice

  1. trap -l is equivalent to executing kill -l.
  2. Please see the kill command for sending signals.
  3. This command is a built-in bash command. For related help information, please see the help command.
  4. It is recommended that you read the following reference materials to learn more about this command: