Usage of the trap command

Source: Internet
Author: User

In some cases, we do not want our shell scripts to be interrupted at runtime. For example, we have written a shell script to set it to the default shell of a user, after a user enters the system, he can only perform some work, such as database backup. We do not want the user to enter the shell State using Ctrl + C or the like, and do what we don't want to do. This uses signal processing.

Kill-l can list the system signal names as follows:
-Bash-3.00 # 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
-Bash-3.00 #
Generally, we need to ignore four signals: Hup, Int, quit, tstp, that is, signal 1, 2, 3, 24 The following statement can be used to ignore these interrupt signals:
Trap""1 2 3 24 or trap""Hup int quit tstp
Use trap: 1 2 3 24 or trap hup int quit tstp to restore the default value. Stty-A can be used to list the relationship between the interrupt signal and the keyboard. Run the above command, run tail-F/etc/passwd, and then try to interrupt it with the keyboard, try two different cases (default and ignore. What's more convenient is that we can use trap in shell to define our own signal processing program, just like using signal in C,

For example, trap "echo'Go away'"Int

 

It corresponds to three different signal response methods.
First:
Trap "commands" signal-list
When the script receives signals listed in the signal-list, the trap command executes the commands in double quotation marks.

Second:
Trap signal-list
Trap does not specify any command and receives the default signal operation. The default operation ends the process.

Third:
Trap "" signal-list
The trap command specifies an empty command string that allows ignoring signals.

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.