Skip to content

Instantly share code, notes, and snippets.

@neilmillard
Created May 3, 2017 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neilmillard/61513fbdad932a234fbcc61f93811552 to your computer and use it in GitHub Desktop.
Save neilmillard/61513fbdad932a234fbcc61f93811552 to your computer and use it in GitHub Desktop.
bash retry
#!/bin/bash
# Retry
# set ATTEMPTS and TIMEOUT before calling
function retry {
local max_attempts=${ATTEMPTS-5}
local timeout=${TIMEOUT-1}
local attempt=0
local exitCode=0
while (( $attempt < $max_attempts ))
do     
set +e     
"$@"     
exitCode=$?
set -e
    if [[ ${exitCode} == 0 ]] || [[ ${exitCode} == 52 ]]; then
break
fi
    echo "Failure! Retrying in $timeout.." 1>&2
sleep ${timeout}
attempt=$(( attempt + 1 ))
timeout=$(( timeout * 2 ))
done
  if [[ ${exitCode} != 0 ]]; then
echo "You've failed me for the last time! ($@)" 1>&2
fi
  return ${exitCode}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment