Shell Signal capture Command trap

Source: Internet
Author: User
Tags arithmetic

Trap command

tarpThe command is used to perform the action after the specified signal is received, usually in order to complete the cleanup when the shell script is interrupted. For example:
When the script is pressed at execution time CTRL+c , "program exit ..." is displayed and exits ( CTRL+c the signal is SIGINT)

#!/bin/bashtrap "ehco ‘program exit...‘; exit 2" SIGINT....
Signal

Common signals

signal name signal number
sigint 2
sigquit 3
sigfpe 8 In the event of fatal arithmetic Is emitted when an operation is wrong. This includes not only floating-point arithmetic errors, but also all other arithmetic errors such as overflow and divisor 0.
sigkill 9
sigalrm 14 clock timing signal , the actual time or clock time is calculated. The alarm function uses this signal. SIGTERM
Capture Signal
    • Command syntax for trap:

      shell trap command signal
      This command can be a Linux command, or a user-defined function. signalis the signal name or number of signals, you can specify multiple signals, separated by a space.

    • trapCommon use in the shell

      • Execute command before script exits

      When the script is pressed at execution time CTRL+c , "program exit ..." is displayed and exits ( CTRL+c the signal is SIGINT)

      trap "ehco ‘program exit...‘; exit 2" SIGINT
      • Ignore signal

      Example: Ignoring SIGINT SIGQUIT SIGFPE such signals while the script is running

      trap ‘‘ 2 3 8

Shell Signal capture Command 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.