Skip to content

Instantly share code, notes, and snippets.

@jgraham909
Created November 16, 2015 18:37
Show Gist options
  • Save jgraham909/592615563f7c197b462f to your computer and use it in GitHub Desktop.
Save jgraham909/592615563f7c197b462f to your computer and use it in GitHub Desktop.
#!/bin/bash
FAIL=1 # or some non zero error number you want
MAX_TRIES=4
COUNT=0
BACKOFF=1
if [ $# -lt 1 ];then
echo "You must provide a command to execute. Commands with arguments must be quoted."
exit 1
fi
echo "Executing '$1' up to $MAX_TRIES times"
while [ $COUNT -lt $MAX_TRIES ]; do
($1)
if [ $? -eq 0 ];then
exit 0
fi
let COUNT=COUNT+1
let BACKOFF=BACKOFF*2
echo "Attempt $COUNT failed retrying after $BACKOFF seconds"
sleep $BACKOFF
done
echo "Tried and failed $COUNT times. Giving up."
exit $FAIL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment