Use of the Linux shell Trap

Source: Internet
Author: User

Original address: http://blog.sina.com.cn/s/blog_62eb16bb01014dbh.html

Once the trap captures the signal, there are three ways to react:

(1) Execute a procedure to process this signal

(2) The default action to accept the signal

(3) Ignoring this signal

Two. The trap provides three basic forms for the above three ways:

The first form of the Trap command, when the shell receives a signal of the same value in the Signal list list, performs a dual

The command string in quotation marks.

Trap ' commands ' signal-list

Trap "Commands" signal-list

To restore the default operation of the signal, use the second form of the trap command:

Trap Signal-list

The third form of trap command allows to ignore the signal

Trap "" Signal-list

Attention:

(1) The signal 11 (paragraph violation) can not be captured, because the shell itself needs to capture the signal to the memory of the dump.

(2) in the trap can define the processing of signal 0 (in fact, there is no such signal), the shell program at its termination (such as the execution of the exit statement) when the signal is emitted.

(3) After capturing the signal specified in the signal-list and executing the corresponding command, if these commands do not terminate the shell program, the shell program will continue to execute the command after the command executed to receive the signal, which will easily lead to the shell program cannot be terminated.

Also, in a trap statement, single and double quotes are different, and when the shell first encounters a trap statement, the command in commands is scanned again. If commands is enclosed in single quotes, then the shell will not replace the variables and commands in the commands, otherwise the variables and commands in commands will be replaced with the specific values at that time.

Signal description
HUP (1) hangs, usually caused by terminal drop or user exit
INT (2) interrupt, usually caused by pressing CTRL + C key combination
Quit (3) exit, usually caused by pressing CTRL + key combination
ABRT (6) Abort, usually caused by some serious execution error
ALRM (14) Alarm, typically used to process timeouts
Term (15) termination, usually sent at system shutdown

Use of the Linux shell Trap

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.