Trap capture signal in Shell in Linux

Source: Internet
Author: User
Tags arithmetic

A signal is a interprocess communication mechanism that provides an application with an asynchronous software interrupt that gives the application the opportunity to accept commands (i.e., signals) sent by other programs live terminals. After the application receives a signal, there are three ways to handle it: ignore , default , or capture . When a process receives a signal, it checks the processing mechanism of the signal. If it's sig_ign, the signal is ignored and, if it is SIG_DFT, the system default processing action is used, usually terminating the process or ignoring the signal, and if a handler function (capture) is given to the signal, the task being performed by the current process is interrupted and the processing function of the signal is executed. Return to execution of the interrupted task.

In some cases, we don't want our shell script to be interrupted at run time, for example, we write the shell script as a user's default shell, so that the user into the system can only do a certain job, such as database backup, we do not want users to use the CTRL C and so on into the shell state, do what we do not want to do. This is used to signal processing.

Here are some of the more common signals you might encounter to use in your program:

Signal Name Number of signals Describe
Sighup 1 This signal is issued at the end of a user terminal connection (normal or abnormal), usually at the end of the control process of the terminal, notifying each job within the same session, when they are no longer associated with the control terminal. When you log on to Linux , the system assigns a terminal (session) to the logged-on user. All programs running at this terminal, including foreground process groups and background process groups, are typically part of this session. When the user exits the Linux login, the foreground process group and the background have the process to the terminal output will receive the SIGHUP signal. The default action for this signal is to terminate the process, so the foreground process group and the process with terminal output in the background will be aborted. For a daemon that is disconnected from the terminal, this signal is used to notify it to re-read the configuration file.
SIGINT 2 program termination (interrupt) signal that is emitted when the user types a intr character (usually ctrl C).
Sigquit 3 Similar to SIGINT, but controlled by quit characters (usually CTRL/). A process produces a core file when it exits Sigquit, in the sense that it is similar to a program error signal.
Sigfpe 8 Emitted when a fatal arithmetic operation error occurs. This includes not only floating-point arithmetic errors, but also all other arithmetic errors such as overflow and divisor 0.
SIGKILL 9 Used to immediately end a program's operation. This signal cannot be blocked, processed and ignored.
Sigalrm 14 Clock timing signal, calculated is the actual time or clock time. The alarm function uses this signal.
Sigterm 15 Program end (terminate) signal, unlike Sigkill, the signal can be blocked and processed. Usually used to require the program to exit normally. The shell command kill defaults to produce this signal.

Capture Signal
When you press CTRL + C or break keys in the execution of a terminal shell program, the normal program terminates immediately and returns a command prompt. This may not always be desirable. For example, you may end up leaving a bunch of temporary files that will not be cleaned up.

Capturing these signals is easy, and the syntax for 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 that you want to capture in the list.

Traps in shell scripts have three common uses:

Clean up temporary files
Ignore signal

To clean up temporary files:
Trap command As an example, the following shows how you can 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

Execute the shell program, this trap angle, these two files work1$$ and dataout$$ will be automatically deleted if the program receives a signal number of 2.

So the user interrupts execution, and if the program executes after this trap you can be assured that these two files will be cleaned up. The exit command as follows RM is necessary, as no execution of it will continue in the program at the point where it is left to receive the signal.

Signal No. 1th is hanging up: either someone intentionally hangs up the line or the line is accidentally disconnected.

You can modify the preceding traps and also delete the specified files, in which case, two signal signal number 1th is added to the list:

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

These files will now be deleted if the row is hung, or press CTRL C to press.

To capture the specified commands must be enclosed in quotation marks if they contain more than one command. Also note that the shell command line scans the trap command to be executed and again when a listed signal is received for the time.

Workdir value $$ So in the previous example, the Trap command will be substituted for the execution time. If you want this substitution to occur at the time of receiving the signal 1 or 2, you can put the single quotation mark inside the command:

$ Trap ' rm $WORKDIR/work1$$ $WORKDIR/dataout$$; Exit ' 1 2

Ignore signal:
If the command listed by the trap is empty, the specified signal is ignored when it is received. For example, the following command:

$ Trap ' 2

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

$ Trap ' 1 2 3 15

Note that the first parameter must be specified as a signal to be ignored, not equivalent to writing the following, and it has separate meanings as well:

$ trap 2

If you ignore a signal, all the child shells also ignore the signal. However, if you specify the action to take in the received signal, all the child shells will still receive the signal in the default operation.

To reset a trap:
When you change the default action that should be taken after receiving the signal, you can change it back to the trap if you just omit the first argument;

$ trap 1 2

The

Reset should take action to receive signal 1 or 2 to return the 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.