1

I have a script that runs through a list of servers to connect to and grabs by SCP over files to store

Occasionally due to various reasons one of the servers crashes and my script gets stuck for around 4 hours before moving on through the list.

I would like to be able to detect a connection issue or a period of time elapsed after script has started and kill that command and move on to next.

I suspect that this would involve a wait or sleep and continue but I am new to loops and bash

#!/bin/bash
#
# Generate a list of backups to grab
df|grep backups|awk -F/ '{ print $NF }'>/tmp/backuplistsmb

# Get each backup in turn

for BACKUP in `cat /tmp/backuplistsmb`
do
        cd /srv/backups/$BACKUP
        scp -o StrictHostKeyChecking=no $BACKUP:* .
sleep 3h
done

The above script works fine but does get stuck for 4 hours should there be a connection issue. It is worth noting that some of the transfers take 10 mins and some 2.5 hours

Any ideas or help would very appreciated

6
  • note for B in cat file; do... is not very much recommended. Better say while read B; do ... done < file. Commented Nov 17, 2016 at 11:33
  • scp -o ConnectTimeout=30 ...? Commented Nov 17, 2016 at 11:41
  • is was thinking of somthing like:if GETSSTUCK # then continue # Skip rest of this particular loop iteration. fi done Commented Nov 17, 2016 at 11:41
  • Possible duplicate of How to setup SSH timeout in shell script Commented Nov 17, 2016 at 11:42
  • scp -o ConnectTimeout=30 - really that easy, will it scip to next if so Commented Nov 17, 2016 at 11:43

1 Answer 1

1

Try to use the timeout program for that:

Usage:

timeout [OPTION] DURATION COMMAND [ARG]...

E.g. time (timeout 3 sleep 5) will run for 3 secs. So in your code you can use:

timeout 300 scp -o StrictHostKeyChecking=no $BACKUP:* .

This limits the copy to 5 minutes.

Sign up to request clarification or add additional context in comments.

2 Comments

Looking back on the comments I think connectionTimeout is what I need as the duration taken to do the backup varies a lot from 5 minutes to 3 hours - thanks
Sure, you can do that. I did not know that option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.