How do I automatically retry an SFTP connection attempt?

女生的网名这么多〃 提交于 2020-01-16 04:26:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!