simplycoding
simplycoding

Reputation: 2967

How do I automatically retry an SFTP connection attempt?

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).

Upvotes: 2

Views: 7458

Answers (2)

Cyrus
Cyrus

Reputation: 88583

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

Upvotes: 1

Sriharsha Kalluru
Sriharsha Kalluru

Reputation: 1823

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

Upvotes: 2

Related Questions