Bash not trapping interrupts during rsync/subshell exec statements Bash not trapping interrupts during rsync/subshell exec statements shell shell

Bash not trapping interrupts during rsync/subshell exec statements


How about just having all the output from point X be redirected to tee without having to repeat it everywhere and mess with all the sub-shells and execs ... (hope I didn't miss something)

#!/bin/bashlogfile=/path/to/file;directory1=/path/to/dirdirectory2=/path/to/direxec > >(exec tee -a $logfile) 2>&1cleanup () {     echo "Cleaning up!"     #do stuff     trap - EXIT }trap cleanup EXITsleep 10rsync --progress -av --delete $directory1 /var/tmp/$directory2


In addition to set -e, I think you want set -E:

If set, any trap on ERR is inherited by shell functions, command substitutions, and commands executed in a sub‐shell environment. The ERR trap is normally not inherited in such cases.

Alternatively, instead of wrapping your commands in subshells use curly braces which will still give you the ability to redirect command outputs but will execute them in the current shell.


The interupt will be properly caught if you add INT to the trap

trap '{    (cleanup;) | 2>&1 tee -a $logfile}' EXIT INT

Bash is trapping interrupts correctly. However, this does not anwer the question, why the script traps on exit if sleep is interupted, nor why it does not trigger on rsync, but makes the script work as it is supposed to. Hope this helps.