Last active
March 14, 2023 23:49
-
-
Save kwilczynski/a79bc81fc4707c15bb5ca470a98aca8a to your computer and use it in GitHub Desktop.
Retry something in Bash shell
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
retry() { | |
local n=1 | |
local max=5 | |
local delay=15 | |
while true; do | |
"$@" && break || { | |
if [ $n -lt $max ]; then | |
n=$((n+1)) | |
# No output on failure to allow redirecting output | |
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