Skip to content

Instantly share code, notes, and snippets.

@gadelkareem
Created March 16, 2022 23:26
Show Gist options
  • Save gadelkareem/eabd0d6e41fa3a6d9ac5d94c74d92439 to your computer and use it in GitHub Desktop.
Save gadelkareem/eabd0d6e41fa3a6d9ac5d94c74d92439 to your computer and use it in GitHub Desktop.
Retry Bash function
#!/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