Signal capture of 2018-9-17-bash

Source: Internet
Author: User

I. Overview

In the previous chapter, we had an example of a script that detects if an IP address is online, but there is a problem, in the process of running, no matter Ctrl+c how to stop, and no egg, or continue to run, until the last IP address ping can not be stopped, just the current ping operation is finished, the next The ping operation will run, but if you press the shortcut key continuously, there is always a signal caught, but only by the script of the current process to be able to stop, not the ping process, this is the signal.

We can also manually customize the function of the signal capture, then how to customize the processing of the signal capture, then in the ping operation, because the signal is not captured, then can be captured signal after the corresponding processing operation, for bash this is possible, in the system there is a trap command, Capture a signal by deploying a trap, or fire one of its events for snap processing.

We can use -l the option to list all the signals that can be captured.

# trap -l 1) SIGHUP   2) SIGINT   3) SIGQUIT  4) SIGILL   5) SIGTRAP 6) SIGABRT  7) SIGBUS   8) SIGFPE   9) SIGKILL 10) SIGUSR111) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM16) SIGSTKFLT   17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP21) SIGTTIN 22) SIGTTOU 23) SIGURG  24) SIGXCPU 25) SIGXFSZ26) SIGVTALRM   27) SIGPROF 28) SIGWINCH    29) SIGIO   30) SIGPWR31) SIGSYS  34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+338) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+843) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+1348) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-1253) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-758) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-263) SIGRTMAX-1  64) SIGRTMAX    

However, the kill -l same results are obtained for use, but to understand their signal meanings, we can view them through the man manual.

信号捕捉:    列出信号:        trap -l        kill -l        man 7 signal

A signal is a mechanism between interprocess communication, and trap commands allow the bash script to customize how the signal is captured, but it is important to note that the signal cannot be captured, TERM KILL because the meaning of the capture signal is that once the capture is able to do something, it generally captures the signal as HUPand INT so on, of course other signals can also be captured, but none of the above two use so frequently.

    常用的信号:        HUP, INT

Example:

#!/bin/bashtrap  ‘echo "Dou ni wan"‘ INTfor i in {1..254}; do    if ping -W1 -c1 192.168.$i.1 &> /dev/null; then        echo "192.168.$i.1 is up."    else        echo "192.168.$i.1 is down."    fidone
# bash trap.sh 192.168.1.1 is down.192.168.2.1 is down.^CDou ni wan192.168.3.1 is down.^CDou ni wan192.168.4.1 is down.^CDou ni wan192.168.5.1 is down.^CDou ni wan192.168.6.1 is down.

From the above operating results, once emitted Ctrl+c is the INT signal, it will show Dou ni wan , but actually did not stop its ping process, just stop the current ping , and the next cycle of the ping beginning of the run, if the total exit of the current bash process, In a different way.

#!/bin/bashtrap  ‘echo "quit"; exit 1‘ INTfor i in {1..254}; do    if ping -W1 -c1 192.168.$i.1 &> /dev/null; then        echo "192.168.$i.1 is up."    else        echo "192.168.$i.1 is down."    fidone

So this is the meaning of the trap command, the ability to capture the signal, and after capturing the signal, using the defined command to do the processing, then the above signal processing is to show quit and exit the shell process.

The method of the trap command is simple, followed by its arguments, followed by the signal Declaration, and once the signal has been captured, what kind of processing is done, and its command usage is:

trap ‘COMMAND‘ SIGNALS

We can use the form of a command defined in a function to trap define it and then capture it.

#!/bin/bash#trap ‘mytrap‘ INTmytrap() {    echo "Quit"    exit 1}for i in {1..254}; do    if ping -W1 -c1 192.168.$i.1 &> /dev/null; then        echo "192.168.$i.1 is up"    else        echo "192.168.$i.1 is down"    fidone

And after each ping operation is complete, you can create its temporary file to save its associated information. Placed inside the loop, each creation is guaranteed to be deleted, and the output of the fork must be guaranteed.

But this creates countless temporary files, and we need to create an array of them to append to the last element and delete their files when they encounter a terminating signal.

#!/bin/bash#declare -a hosttmpfilestrap ‘mytrap‘ INTmytrap() {    echo "Quit"    rm -f ${hosttmpfiles[@]}    exit 1}for i in {1..254}; do    tmpfile=$(mktemp /tmp/ping.XXXXXX)    if ping -W1 -c1 192.168.$i.1 &> /dev/null; then        echo "192.168.$i.1 is up" | tee $tmpfiles    else        echo "192.168.$i.1 is down" | tee $tmpfiles    fi    hosttmpfiles[${#hosttmpfiles[*]}]=$tmpfiledoneecho ${hosttmpfiles[@]}

Complex processing logic can be written inside a function within a signal capture, including exiting and deleting unhandled files.

Ii. using ASCII colors in bash

How to color in the echo command is a very simple thing, about the relevant tutorial online is also a grab a lot, then its color definition format is:

\033[31m hello \033[0m

The above format indicates what color the font is subsequently controlled, and where it is closed, and then it is displayed in this color if it is not closed, unless the next command has its own output stream to end, in order to avoid this effect, you need to add \033[0m it later, Does not affect the shading behind it, it is shaded within this range.

And that two numbers have a different meaning.

    ##m:        左侧#:            3: 前景色;            4: 背景色;        右侧#: 颜色种类            1, 2, 3, 4, 5, 6, 7

If you are using a single number, you will change the format of the text, such as bold or blinking.

    #m:        加粗、闪烁等功能;多种控制符,可组合使用,彼此间用分号隔开;
echo -e "\033[42;35;5,hello world\033[0m"

Signal capture of 2018-9-17-bash

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.