Created
May 3, 2017 07:44
-
-
Save neilmillard/61513fbdad932a234fbcc61f93811552 to your computer and use it in GitHub Desktop.
bash retry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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