In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use Bash signal capture in the script. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
The startup of the Shell script is not difficult to detect, but the termination detection of the Shell script is not easy because we are not sure whether the script will end as expected or fail due to an unexpected error. It is useful to record what you are working on when the script fails, but sometimes it is not convenient to do so. The existence of the trap command in Bash is to solve this problem, which can capture the termination signal of the script and respond in some preset way.
Response failed
If an error occurs, it may lead to a series of errors. In the following example script, you first create a temporary directory in / tmp so that you can unpack, process files, and so on in the temporary directory, and then package it in another compressed format:
#! / usr/bin/env bashCWD= `pwd`TMP = ${TMP:-/tmp/tmpdir} # # create tmp dirmkdir "${TMP}" # # extract files to tmptar xf "${1}"-- directory "${TMP}" # # move to tmpdir and run commandspushd "${TMP}" for IMG in * .jpg Do mogrify-verbose-flip-flop "${IMG}" donetar-- create-- file "${1% .*}" .tar * .jpg # # move back to originpopd # # bundle with bzip2bzip2-- compress "${TMP}" / "${1% .*}" .tar\-- stdout > "${1% .*}" .tbz # # clean up/usr/bin/rm-r / tmp/tmpdir
In general, this script can be executed as expected. But if the files in the archive are PNG files instead of the expected JPEG files, the script will fail halfway, and another problem arises: the last step to delete the temporary directory is not performed properly. It won't make any difference if you delete the temporary directory manually, but if you don't delete the temporary directory manually, it must deal with an existing temporary directory the next time the script is executed. it's full of unpredictable remaining files.
One solution is to add a preventive deletion logic at the beginning of the script to handle this situation. However, this approach seems to be a bit violent, and we should solve this problem structurally. Using trap is an elegant approach.
Use trap to capture signal
We can use trap to capture the signal when the program is running. If you have used the kill or killall command, you have already used a signal called SIGTERM. In addition, you can issue the trap-l or trap-- list command to list other more signals:
$trap-- list 1) SIGHUP 2) SIGINT 3) SIGILL 4) SIGILL 5) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR111) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGTERM16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGPWR31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+338 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) 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 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-263) SIGRTMAX-1 64) SIGRTMAX
In addition to the above, signals that can be recognized by trap include:
EXIT: the signal sent when the process exits
ERR: the signal sent by a process when it exits with a non-zero status code
DEBUG: a Boolean value that represents debug mode
If you want to achieve signal capture in Bash, you only need to add the command that needs to be executed after the trap, plus the list of signals to be captured.
For example, the following line captures the SIGINT signal sent by the user pressing Ctrl + C while the process is running:
Trap "{echo 'Terminated with Ctrl+C';}" SIGINT
Therefore, the defects in the above script can be fixed by using trap to capture signals such as SIGINT, SIGTERM, process error exit, process normal exit, and correctly handle the temporary directory:
#! / usr/bin/env bashCWD= `pwd`TMP = ${TMP:-/tmp/tmpdir} trap\ "{/ usr/bin/rm-r" ${TMP} "; exit 255;}"\ SIGINT SIGTERM ERR EXIT # # create tmp dirmkdir "${TMP}" tar xf "${1}"-directory "${TMP}" # # move to tmp and run commandspushd "${TMP}" for IMG in *. Jpg Do mogrify-verbose-flip-flop "${IMG}" donetar-- create-- file "${1% .*}" .tar * .jpg # # move back to originpopd # # zip tarbzip2-- compress $TMP/ "${1% .*}" .tar\-- stdout > "${1% .*}" .tbz
For more complex functions, you can also use the Bash function to simplify trap statements.
This is the end of this article on "how to use Bash signal capture in a script". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.