Usage of trap in bash

Source: Internet
Author: User
Document directory
  • How bash explains traps

[Root @ cnc-squid2 ~] # Kill-l
1) sighup 2) SIGINT 3) sigquit 4) sigill
5) sigtrap 6) SIGABRT 7) sigbus 8) sigfpe
9) sigkill 10) SIGUSR1 11) SIGSEGV 12) sigusr2
13) sigpipe 14) sigalrm 15) sigterm 17) sigchld
18) sigcont 19) sigstop 20) sigtstp 21) sigttin
22) sigttou 23) sigurg 24) sigxcpu 25) sigxfsz
26) sigvtalrm 27) sigprof 28) sigwinch 29) sigio
30) sigpwr 31) sigsys 34) sigrtmin 35) sigrtmin + 1
36) sigrtmin + 2 37) sigrtmin + 3 38) sigrtmin + 4 39) sigrtmin + 5
40) sigrtmin + 6 41) sigrtmin + 7 42) sigrtmin + 8 43) sigrtmin + 9
44) sigrtmin + 10 45) sigrtmin + 11 46) sigrtmin + 12 47) sigrtmin + 13
(48) sigrtmin + 14 49) sigrtmin + 15 50) SIGRTMAX-14 51) SIGRTMAX-13
SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9
(56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59)
(60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63)
64) sigrtmax

 

Summary

In this case, users who do not want to use your script may end the process via the keyboard out of time, for example, because the input must be provided or some cleanup work must be performed.TrapStatements capture these sequences and can be compiled to execute a series of commands when these signals are not active.

TrapThe statement syntax is as follows:

trap [COMMANDS] [SIGNALS]

MeaningTrapThe command is captured inSignalsThe list may contain or does not containSIGPrefix signal or signal number. If a signal is0OrExit, ThenCommandsRun the command when the shell exits. If one of the signals isDebug,CommandsThe list is executed after each simple command. A signal can also be specifiedErrIn this caseCommandsExecute each time a simple command exits in a non-zero state. Note that these commands do not come from a non-zero exit statusIfStatement execution, or fromWhileOrUntilLoop. If a logicAnd(&) OrOr(|) When a non-zero exit occurs, none of them will be executed, or when a command exits!Operator.

Unless an invalid signal is encounteredTrapThe return status of the command is 0.TrapCommands include a set of options, which are recorded on the bash info page.

Here is a very simple example to capture from the userCTRL+C, Upon which a message is printed. When you try not to specifyKillWhen the signal is used to kill the program, nothing will happen:

#!/bin/bash
# traptest.sh

trap "echo Booh!" SIGINT SIGTERM
echo "pid is $$"

while : # This is the same as "while true".
do
sleep 60 # This script is not really doing anything.
done
How bash explains traps

Whenever bash receives a signal that sets a trap waiting for command completion in advance, the trap will not be executed before the command ends. When bash uses built-in commandsWaitTo wait for an asynchronous command. The acceptance of signals sent by traps will lead to the built-inWaitImmediately after the trap is executed, a launch status greater than 128 is returned.

Detects a used variable.

Every time you debug a long script, you may want to give it a variableTraceAttributes and trapsDebugInformation. Normally you will be like thisVARIABLE= ValueTo assign values to variables. The following statements to replace variables may provide valuable information for your script behavior:

declare -t VARIABLE=value

trap "echo VARIABLE is being used here." DEBUG

# rest of the script
Clear garbage when exiting

WhatisThe command is executed by cron.makewhatis.cronGenerated database:

#!/bin/bash

LOCKFILE=/var/lock/makewhatis.lock

# Previous makewhatis should execute successfully:

[ -f $LOCKFILE ] && exit 0

# Upon exit, remove lockfile.

trap "{ rm -f $LOCKFILE ; exit 255; }" EXIT

touch $LOCKFILE
makewhatis -u -w
exit 0

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.