Skip to content

Instantly share code, notes, and snippets.

@jt-nti
Created August 7, 2018 12:55
Show Gist options
  • Save jt-nti/3a3b85b53ba00f93dc60f3fabbe32e5e to your computer and use it in GitHub Desktop.
Save jt-nti/3a3b85b53ba00f93dc60f3fabbe32e5e to your computer and use it in GitHub Desktop.
Experimental function to retry a command with exponential backoff (based on https://stackoverflow.com/questions/8350942/how-to-re-run-the-curl-command-automatically-when-the-error-occurs discussion)
function retry_with_backoff {
local attempt=1
local max_attempts=5
local timeout=1
local exitCode=0
while : ; do
"$@"
exitCode=$?
if [ $exitCode -ne 0 ] && [ $attempt -lt $max_attempts ]; then
sleep $timeout
attempt=$(( attempt + 1 ))
timeout=$(( timeout * 2 ))
else
return $exitCode
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment