Trap capture signal in shell

Source: Internet
Author: User
A signal is a mechanism for inter-process communication. It provides an asynchronous software interruption for an application to receive commands (that is, signals) sent by other active terminals ). After an application receives a signal, there are three processing methods: ignore, default, or capture. After receiving a signal, the process checks the processing mechanism of the signal. If it is SIG_IGN, the signal is ignored; if it is SIG_DFT, the system will use the default processing action, usually to terminate the process or ignore the signal; if a processing function (capture) is specified for the signal, the current process is interrupted.

A signal is a mechanism for inter-process communication. It provides an asynchronous software interruption for an application to receive commands (that is, signals) sent by other active terminals ). After the application receives the signal, there are three processing methods:Ignore,Default, OrCapture. After receiving a signal, the process checks the processing mechanism of the signal. If it is SIG_IGN, the signal is ignored; if it is SIG_DFT, the system will use the default processing action, usually to terminate the process or ignore the signal; if a processing function (capture) is specified for the signal, the task being executed by the current process will be interrupted and the processing function of the signal will be executed, the task is interrupted.

In some cases, we do not want our shell scripts to be interrupted at runtime. for example, we have written a shell script to set it to the default shell of a user, after a user enters the system, he can only perform some work, such as database backup. We do not want the user to enter the shell state using Ctrl c or the like, and do what we don't want to do. This uses signal processing.

The following are some of the more common signals you may encounter in the program:

Signal name Signal count Description
SIGHUP 1 This signal is sent at the end of the user terminal connection (normal or abnormal). Generally, when the control process of the terminal ends, it notifies all jobs in the same session, they are no longer associated with control terminals. When you log on to Linux, the system assigns a Session to the login user ). All programs running on this terminal, including foreground and background process groups, generally belong to this Session. When you log out of Linux, processes in the frontend process Group and in the background that are output to the terminal will receive a SIGHUP signal. The default operation of this signal is to terminate the process. Therefore, the processes in the foreground process Group and the background that have terminal output will be aborted. For the daemon that is out of the connection with the terminal, this signal is used to notify it to re-read the configuration file.
SIGINT 2 The interrupt signal is sent when you type the INTR character (usually Ctrl C.
SIGQUIT 3 Similar to SIGINT, but controlled by the QUIT character (usually Ctrl/). a process generates a core file when it exits because it receives SIGQUIT, which is similar to a program error signal.
SIGFPE 8 When a fatal arithmetic operation error occurs, it includes not only floating point operation errors, but also overflow and Division 0 and other arithmetic errors.
SIGKILL 9 It is used to immediately end the running of the program. this signal cannot be blocked, processed, or ignored.
SIGALRM 14 Scheduled clock signal, which is used by the actual time or clock time. alarm function.
SIGTERM 15 The terminate signal is different from SIGKILL in that the signal can be blocked and processed. it is usually used to require the program to exit normally. the shell command kill generates this signal by default.

Capture signal
When you press Ctrl + C or Break to execute a shell program on the terminal, the normal program will be terminated immediately and a command prompt will be returned. This may not always be desirable. For example, you may eventually leave a pile of temporary files and will not clear them.

It is easy to capture these signals. the syntax of the trap command is as follows:

$ trap commands signals

The command here can be any valid Linux command or a user-defined function. the signal can be any number of signals and you want to capture the list.

There are three common usage traps in shell scripts:

  1. Clear temporary files
  2. Ignore signal

Clear temporary files:
The trap command is used as an example to show how to delete some files and then exit. if someone tries to abort the program from the terminal:

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

Run the shell program. from the perspective of this trap, the two files work1 $ and dataout $ will be automatically deleted if the program receives 2 signals.

Therefore, the user interrupts the execution. if the program is executed, you can rest assured that the two files will be cleared. The exit command below rm is necessary because execution without it will continue one point in the program and it will receive a signal when it leaves.

The signal No. 1 is hung up: either someone hangs up the line intentionally or the line is accidentally disconnected.

You can modify the previous trap and delete the specified file. in this case, the two signal signals 1 are added to the list:

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

These files will be deleted now. if this row is down, or press Ctrl c.

To capture specified commands must be enclosed in quotation marks if they contain more than one command. In addition, please note that the shell command line scans the trap command to get the execution time, and again when a listed signal is received.

WORKDIR value $. Therefore, in the previous example, the time when the trap command was executed will be replaced. If you want this alternative to happen when the signal 1 or 2 is received, you can put the command in single quotes:

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

Ignore signal:
If the commands listed in the traps are empty, the specified signal reception will be ignored. For example, the following command:

$ trap '' 2

The specified interrupt signal is ignored. You may want to ignore some signals and do not want to interrupt them. Multiple signals can be ignored as follows:

$ trap '' 1 2 3 15

Note: The first parameter must be specified as a signal to be ignored, instead of writing the following content. it has its own meaning:

$ trap  2

If you ignore a signal, all sub-shells also ignore the signal. However, if you specify the action to be taken in the received signal, all the sub-shells will still receive the default operation for this signal.

Reset trap:
When you change the default action that should be taken after receiving the signal, you can change the trap that it comes back, if you just omit the first parameter;

$ trap 1 2

Reset action should be taken to receive signal 1 or 2 return default.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.