Skip to content

Instantly share code, notes, and snippets.

@mosfet1kg
Created March 5, 2018 16:24
Show Gist options
  • Save mosfet1kg/61e8e3aedf754c05fd5a02ef3037bc4e to your computer and use it in GitHub Desktop.
Save mosfet1kg/61e8e3aedf754c05fd5a02ef3037bc4e to your computer and use it in GitHub Desktop.
retry
# https://unix.stackexchange.com/questions/82598/how-do-i-write-a-retry-logic-in-script-to-keep-retrying-to-run-it-upto-5-times/82610
function fail {
echo $1 >&2
exit 1
}
function retry {
local n=1
local max=5
local delay=15
while true; do
"$@" && break || {
if [[ $n -lt $max ]]; then
((n++))
echo "Command failed. Attempt $n/$max:"
sleep $delay;
else
fail "The command has failed after $n attempts."
fi
}
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment