问题
Is there an option to have Bash retry an SFTP connection n number of times or for x seconds? I'm unable to find any info on making a shell script automatically retry, regardless of the cause of error (server is down, bad password, etc).
回答1:
Try this to try it three times:
c=0; until sftp user@server; do ((c++)); [[ $c -eq 3 ]] && break; done
Long version with error message:
c=0
until sftp user@server; do
((c++))
if [[ $c -eq 3 ]]; then
echo error
break
fi
done
回答2:
You can use until
loop.
STAT=1
until [ $STAT -eq 0 ]; do
sftp user@host
STAT=$?
done
Above syntax will continue till sftp succeeds, If you need for certain number of times then you can use a while
loop with counter.
counter=1
while [ $counter -gt 0 ]; do
sftp user@host
counter=$(($counter-1))
done
来源:https://stackoverflow.com/questions/32787603/how-do-i-automatically-retry-an-sftp-connection-attempt