Created
August 7, 2018 12:55
-
-
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)
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
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