Skip to content

Instantly share code, notes, and snippets.

@dansimau
Created February 24, 2011 16:41
Show Gist options
  • Save dansimau/842415 to your computer and use it in GitHub Desktop.
Save dansimau/842415 to your computer and use it in GitHub Desktop.
Bash function for running a command, checking the return code, and re-trying it x times after y sleep seconds.
#
# "<cmd>" <retry times> <retry wait>
#
do_retry()
{
cmd="$1"
retry_times=$2
retry_wait=$3
c=0
while [ $c -lt $((retry_times+1)) ]; do
c=$((c+1))
echo "Executing \"$cmd\", try $c"
$1 && return $?
if [ ! $c -eq $retry_times ]; then
echo "Command failed, will retry in $retry_wait secs"
sleep $retry_wait
else
echo "Command failed, giving up."
return 1
fi
done
}
@dansimau
Copy link
Author

Eg.:

Some script:

#!/bin/bash
#
# Do something when Foo Site goes down -- but only if it's really down (we'll recheck it to avoid
# false positives -- try again 3 times with a 10 second delay between each)
#

if ! do_retry "curl http://foo.site/" 3 10; then
    echo "Site is down, folks"
fi

Result:

$ ./test.sh 
Executing "curl http://foo.site/", try 1
curl: (6) Couldn't resolve host 'foo.site'
Command failed, will retry in 10 secs
Executing "curl http://foo.site/", try 2
curl: (6) Couldn't resolve host 'foo.site'
Command failed, will retry in 10 secs
Executing "curl http://foo.site/", try 3
curl: (6) Couldn't resolve host 'foo.site'
Command failed, giving up.
Site is down, folks
$ 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment