bash-trap

How can I achieve bash EXIT trap when exec-ing another binary?

醉酒当歌 提交于 2019-12-20 17:26:47
问题 I'd like to use a bash EXIT trap and use exec to avoid spawning a new process. Is this possible? That is, #!/bin/bash touch $0.$$ trap "rm -v $0.$$" EXIT /bin/echo Hello removes the temporary file $0.$$ using bash's EXIT trap while #!/bin/bash touch $0.$$ trap "rm -v $0.$$" EXIT exec /bin/echo Hello never "fires" the trap (no message from rm , file $0.$$ exists after completion). It, of course, makes sense that the trap can't fire as bash is no longer in control after the exec . Is there some

Bash not trapping interrupts during rsync/subshell exec statements

匆匆过客 提交于 2019-12-18 20:53:17
问题 Context: I have a bash script that contains a subshell and a trap for the EXIT pseudosignal, and it's not properly trapping interrupts during an rsync . Here's an example: #!/bin/bash logfile=/path/to/file; directory1=/path/to/dir directory2=/path/to/dir cleanup () { echo "Cleaning up!" #do stuff trap - EXIT } trap '{ (cleanup;) | 2>&1 tee -a $logfile }' EXIT ( #main script logic, including the following lines: (exec sleep 10;); (exec rsync --progress -av --delete $directory1 /var/tmp/

Is it possible to detect *which* trap signal in bash? [duplicate]

孤街浪徒 提交于 2019-12-17 18:48:50
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Identifying received signal name in bash shell script When using something like trap func_trap INT TERM EXIT with: func_trap () { ...some commands... } Is there a way in the function block to detect which trap has called it? Something like: func_trap () { if signal = INT; then # do this else # do that fi } Or do I need to write a separate function for each trap type that does something different? Is there a bash

how to get the original caller lineno when executing a function returning a non-zero value

浪尽此生 提交于 2019-12-14 03:46:33
问题 I've made a func.sh script to be sourced containing: 1. function testfunc () 2. { 3. echo "--> TESTFUNC CALLED" 4. caller 0 5. 6. # here I mimic that something went wrong 7. echo "now I return a non-zero value" 8. return 1 9. } Then I've made a main.sh script 1. #!/bin/bash 2. 3. source 'func.sh' 4. testfunc 5. 6. exit 0 My goal is to catch lineno 4 (in the above script), where I haven't managed correctly the returning value. To do that I tried: 1. #!/bin/bash 2. 3. set -o errexit 4. 5.

Bash, CTRL+C in eval not interrupting the main script

℡╲_俬逩灬. 提交于 2019-12-14 03:46:01
问题 In my bash script, I'm running an external command that's stored in $cmd variable. (It could be anything, even some simple bash oneliner.) If ctrl + C is pressed while running the script, I want it to kill the currently running $cmd but it should still continue running the main script. However, I would like to preserve the option to kill the main script with ctrl + C when the main script is running. #!/bin/bash cmd='read -p "Ooook?" something; echo $something; sleep 4 ' while true; do echo

Perl trapping Ctrl-C (sigint) in bash

﹥>﹥吖頭↗ 提交于 2019-12-13 19:13:25
问题 I'm reading How do we capture CTRL ^ C - Perl Monks, but I cannot seem to get the right info to help with my problem. The thing is - I have an infinite loop, and 'multiline' printout to terminal ( I'm aware I'll be told to use ncurses instead - but for short scripts, I'm more comfortable writing a bunch of printf s ). I'd like to trap Ctrl-C in such a way, that the script will terminate only after this multiline printout has finished. The script is (Ubuntu Linux 11.04): #!/usr/bin/env perl

What is the actual signal behind ERR

落花浮王杯 提交于 2019-12-12 18:28:13
问题 I've read in several places (including SO) that -e is considered "poor form" and is unreliable for exiting a script on any error. A better way to handle errors seems to be using trap , as such: trap "echo there was an error; exit 1;" ERR I can't seem to locate in the man pages what signal ERR is actually? I'm assuming it's SIGQUIT but I can't find for sure. man 7 signal only has the normal signals you would expect SIGTERM SIGQUIT SIGINT , etc. man trap has references to ERR signal, but doesn

Why does my bash script take so long to respond to kill when it runs in the background?

拈花ヽ惹草 提交于 2019-12-10 21:08:13
问题 (Question revised, now that I understand more about what's actually happening) : I have a script that runs in the background, periodically doing some work and then sleeping for 30 seconds: echo "background script PID: $$" trap 'echo "Exiting..."' INT EXIT while true; do # check for stuff to do, do it sleep 30 done & If I try to kill this script via kill or kill INT , it takes 30 seconds to respond to the signal. I will answer this question below, since I found a good explanation online. ( My

Bash: Trap ERR does not work when pipe operator is used

China☆狼群 提交于 2019-12-10 17:39:04
问题 I am trying to log everything that comes out of stdout and stderr into a log file and still preserve the console. For this, I just appended: |& tee -a log_file.log to every command. However, I also want to run a custom command if any error occurred during the script. For this, I added the following at the beginning of the script: trap "echo Non-zero exit code detected" ERR . The problem is by using the pipe operator, the echo in the trap does not execute anymore. Script 1, without pipe: $cat

Recover after bash trap

别等时光非礼了梦想. 提交于 2019-12-09 13:24:53
问题 I've a bash script with some file manipulations and I would like to process a loop until the end of the block after pressing CTRL + C . I've made an example: #!/bin/bash # Register signal handler ABORT=0; trap ABORT=1 SIGINT; # Create temp dir TEMPDIR=$(mktemp -d -t $0); # Helper functions function do_other_stuff { true; } # Process files for ((COUNTER = 0; COUNTER < 3 && ABORT == 0; COUNTER++)); do FILE=/some/directory/$COUNTER.txt; BASE=$(basename $FILE); cp $FILE $TEMPDIR; > $FILE; do