I'm trying to build a generic retry
shell function to re-run the specified shell command a few times if it fails in the last time, here is my code:
retry() {
declare -i number=$1
declare -i interrupt=0
trap "echo Exited!; interrupt=1;" SIGINT SIGTERM SIGQUIT SIGKILL
shift
for i in `seq $number`; do
echo "\n-- Retry ${i}th time(s) --\n"
$@
if [[ $? -eq 0 || $interrupt -ne 0 ]]; then
break;
fi
done
}
It works great for wget
, curl
and other all kinds of common commands. However, if I run
retry 10 rsync local remote
, send a ctrl+c to interrupt it during transferring progress, it reports
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(700) [sender=3.1.3]
It seems that rsync
suppresses the SIGINT and other some related signals inside, then returns a code 20
to the outside caller. This return code didn't make the loop break, then I send a few ctrl+c to interrupt the next rsync
commands. It prints Exited!
only for the last ctrl+c and trap
catch it.
Questions:
Why does it first return code 20 didn't make the loop break?
How to let the
trap
catch the SIGINT signal butrsync
, if not, what should I do?