I have a bash script for running a server, which will typically be terminated by the user using Ctrl-C
. On exit it runs a cleanup function, which I don't want to be interrupted by a second Ctrl-C
.
#!/bin/bash
...
function cleanup {
trap '' INT
echo -n " Cleaning up..."
scp $SRV:~/$DIR/server.log . && ssh -t $SRV "rm -rf ~/$DIR"
echo " Finished."
exit 0
}
trap cleanup EXIT
...
At the moment, a second Ctrl-C
before the scp
has finished causes the script to hang indefinitely. I understand this has something to do with the SIGINT
being sent both to the bash script and the scp
process, but I am at a loss as to why this causes the script to hang, rather than just causing cleanup to fail.
So my question is:
- Why does this cause the script to hang?
- How can I prevent the SIGINT from reaching the
scp
andssh
child processes?
scp ...
line - the script hangs beforeecho
is called. And when Iset -x
to show which commands are called, I can see that the&& ssh ...
part is never called either.