Created
March 16, 2022 23:26
-
-
Save gadelkareem/eabd0d6e41fa3a6d9ac5d94c74d92439 to your computer and use it in GitHub Desktop.
Retry Bash function
This file contains 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
#!/usr/bin/env bash | |
function retry { | |
local retries=$1 | |
shift | |
local count=0 | |
until "$@"; do | |
exit=$? | |
wait=$((2 ** $count)) | |
count=$(($count + 1)) | |
if [[ ${count} -lt ${retries} ]]; then | |
echo "Retry $count/$retries exited $exit, retrying in $wait seconds..." | |
sleep ${wait} | |
else | |
echo "Retry $count/$retries exited $exit, no more retries left." | |
return ${exit} | |
fi | |
done | |
return 0 | |
} | |
function getURL { | |
curl gadelkareem.com | |
} | |
retry 10 getURL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment