Created
March 5, 2018 16:24
-
-
Save mosfet1kg/61e8e3aedf754c05fd5a02ef3037bc4e to your computer and use it in GitHub Desktop.
retry
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
# 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