bash-trap

how to trap errors inside the if statement

丶灬走出姿态 提交于 2021-02-10 03:12:30
问题 Running the following code: #!/bin/bash set -o pipefail set -o errtrace set -o nounset set -o errexit function err_handler () { local error_code="$?" echo "TRAP!" echo "error code: $error_code" exit } trap err_handler ERR echo "wrong command in if statement" if xsxsxsxs then echo "if result is true" else echo "if result is false" fi echo -e "\nwrong command directly" xsxsxsxs exit produces the following output: wrong command in if statement trap.sh: line 21: xsxsxsxs: command not found if

how to trap errors inside the if statement

戏子无情 提交于 2021-02-10 03:06:45
问题 Running the following code: #!/bin/bash set -o pipefail set -o errtrace set -o nounset set -o errexit function err_handler () { local error_code="$?" echo "TRAP!" echo "error code: $error_code" exit } trap err_handler ERR echo "wrong command in if statement" if xsxsxsxs then echo "if result is true" else echo "if result is false" fi echo -e "\nwrong command directly" xsxsxsxs exit produces the following output: wrong command in if statement trap.sh: line 21: xsxsxsxs: command not found if

trap fails to catch SIGSEGV

旧时模样 提交于 2021-01-28 12:11:01
问题 I'm using this script to test trap: #!/bin/bash trap "echo segfault!" SIGSEGV g++ forever.cpp ./a.out And forever.cpp just runs a recursive function: void forever(){ forever(); } int main(){ forever(); } However it gives Segmentation fault: 11 instead of printing segfault . I'm not sure why. 回答1: The trap statement traps signals received by bash , not its children. The child receives the segfault and will be exiting with an appropriate exit code. You should therefore check the exit code from

bash trap won't ignore signal

淺唱寂寞╮ 提交于 2021-01-23 05:11:24
问题 Please consider this bash-script: #!/bin/bash trap '' INT echo sleep: sleep 5 echo rsync: rsync -a /usr/lib /var/tmp Trying to interrupt sleep with ctrl-c fails, as expected. But rsync is interruptible (the order of sleep & rsync doesn't matter)? Any ideas are welcome! Edit: Found a difference: rsync itself starts 2 child procs (client/server, which produce the 2 error msgs, i assume), and these seem not to inherit the 'ignorance' of its parent. Must dive into bash sources and find out how

bash trap won't ignore signal

左心房为你撑大大i 提交于 2021-01-23 04:57:40
问题 Please consider this bash-script: #!/bin/bash trap '' INT echo sleep: sleep 5 echo rsync: rsync -a /usr/lib /var/tmp Trying to interrupt sleep with ctrl-c fails, as expected. But rsync is interruptible (the order of sleep & rsync doesn't matter)? Any ideas are welcome! Edit: Found a difference: rsync itself starts 2 child procs (client/server, which produce the 2 error msgs, i assume), and these seem not to inherit the 'ignorance' of its parent. Must dive into bash sources and find out how

Reliably kill sleep process after USR1 signal

淺唱寂寞╮ 提交于 2020-06-25 03:36:33
问题 I am writing a shell script which performs a task periodically and on receiving a USR1 signal from another process. The structure of the script is similar to this answer: #!/bin/bash trap 'echo "doing some work"' SIGUSR1 while : do sleep 10 && echo "doing some work" & wait $! done However, this script has the problem that the sleep process continues in the background and only dies on its timeout. (note that when USR1 is received during wait $!, the sleep process lingers for its regular

Externally disabling signals for a Linux program

谁都会走 提交于 2020-01-01 04:23:26
问题 On Linux, is it possible to somehow disable signaling for programs externally ... that is, without modifying their source code? Context: I'm calling a C ( and also a Java ) program from within a bash script on Linux. I don't want any interruptions for my bash script, and for the other programs that the script launches (as foreground processes). While I can use a... trap '' INT ... in my bash script to disable the Ctrl C signal, this works only when the program control happens to be in the

Does trap work as expected while piping?

无人久伴 提交于 2019-12-22 04:42:23
问题 Here is minimal code for issue demonstration: http://pastebin.com/5TXDpSh5 #!/bin/bash set -e set -o pipefail function echoTraps() { echo "= on start:" trap -p trap -- 'echo func-EXIT' EXIT echo "= after set new:" trap -p # we can ensure after script done - file '/tmp/tmp.txt' was not created trap -- 'echo SIG 1>/tmp/tmp.txt' SIGPIPE SIGHUP SIGINT SIGQUIT SIGTERM } trap -- 'echo main-EXIT1' EXIT echo "===== subshell trap" ( echoTraps; ) echo "===== pipe trap" echoTraps | cat echo "===== done

Bash script: can not properly handle SIGTSTP

我与影子孤独终老i 提交于 2019-12-21 09:14:30
问题 I have a bash script that mounts and unmounts a device, which performing some read operations in between. Since the device is very slow, the script takes about 15 seconds to complete (the mount taking atleast 5-6 seconds). Since leaving this device mounted can cause other problems, I don't want this script to be interrupted. Having said that, I can correctly handle SIGINT (Ctrl+c), but when I try to handle SIGTSTP (Ctrl+z), the script freezes. Which means the signal is trapped but the handler

Trap signal in child background process

淺唱寂寞╮ 提交于 2019-12-20 19:41:57
问题 I am unable to trap a signal when running in a child / background process. Here is my simple bash script : #!/bin/bash echo "in child" trap "got_signal" SIGINT function got_signal { echo "trapped" exit 0 } while [ true ]; do sleep 2 done When running this and later do kill -SIGINT (pid) Everything works as expected, it prints 'trapped' and exits. Now, if I start the same script from a parent script like this : #!/bin/bash echo "starting the child" ./child.sh & Then the child does not trap the