Use of Linux shell trap

Source: Internet
Author: User

Use of Linux shell trap

1. After the trap captures the signal, there are three response methods:

(1) execute a program to process this signal.

(2) default operation for receiving signals

(3) Ignore this signal

Ii. trap provides three basic forms for the above three methods:

The first form of trap Command will execute the double

The command string in quotation marks.

Trap 'commands' signal-list

Trap "commands" signal-list

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

Trap signal-list

The third form of trap command allows ignoring Signals

Trap "" signal-list

Note:

(1) The signal 11 (segment violation) cannot be captured, because the shell itself needs to capture the signal for memory dump.

(2) In the trap, you can define the processing of the signal 0 (in fact, there is no such signal). The shell program sends this signal when it terminates (such as executing the exit Statement.

(3) After capturing the signal specified in the signal-list and executing the corresponding commands, if these commands do not terminate the shell program, the shell program will continue to execute the command after the command received when the signal is received, which will easily cause the shell program to be unable to terminate.

In the trap statement, single quotes and double quotation marks are different. When the shell program first encounters a trap statement, it will scan the commands in commands. If commands is enclosed in single quotes, shell will not replace the variables and commands in commands. Otherwise, the variables and commands in commands will be replaced with the specific values at that time.

Signal description
HUP (1) suspended, usually caused by terminal disconnection or user exit
INT (2) interruption, usually caused by pressing Ctrl + C
QUIT (3) exits, usually caused by pressing Ctrl + key combination
ABRT (6) abort, usually caused by some serious execution errors
ALRM (14) Alarm, usually used to handle timeout
TERM (15) Termination, usually sent when the system is shut down

This article permanently updates the link address:

  • 1
  • 2
  • Next Page

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.