On 3/10/16 10:07 AM, Olof Schonbeck wrote: > In a small bash script we have a trap to cleanup some files when exiting. You > run the script by ssh to the machine in question and execute the script. If > your ssh session dies the trap should trigger and clean up the files as the > script exit but this doesn't happen.
I can reproduce this more-or-less exactly on bash-4.3.42 on Mac OS X, and it's a race condition. The read system call called by readline (read -e) either returns an error before sshd sends SIGHUP or it doesn't. When it errors before the SIGHUP, which appears to be the usual case, the read builtin returns and the EXIT trap gets called. The trap is executing (and is then killed) when the SIGHUP arrives. If you effectively ignore the HUP by trapping it with `true', the EXIT trap runs to conclusion just fine. If the SIGHUP arrives and kills the read builtin, the EXIT trap gets run when the fatal signal causes the shell to exit. You can see this if you change the SIGHUP trap to trap "echo HUP >> /tmp/trap" SIGHUP You'll see the HUP show up somewhere in /tmp/trap. The reason the 'trap cleanup HUP' doesn't work, though I can't reproduce that, is probably that the shell has exited -- without running any exit trap -- before the HUP arrives. Bash doesn't run the EXIT trap recursively, so it won't be re-executed as a result of the SIGHUP if it's already running as the result of the shell exiting normally. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRU c...@case.edu http://cnswww.cns.cwru.edu/~chet/