Created
June 26, 2018 15:12
-
-
Save mhutter/9162b18805f850c2069fce074bc34d52 to your computer and use it in GitHub Desktop.
Bash: Retry command N times
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
#!/bin/bash | |
function retry { | |
local n=0 | |
local max="$1"; shift | |
local cmd="$*" | |
until $cmd; do | |
if [ "$((++n))" -lt "$max" ]; then | |
echo "Retrying in $((n*2)) seconds..." | |
sleep "$((n*2))" | |
else | |
echo "Failed after $n attempts: $cmd" | |
return 1 | |
fi | |
done | |
} | |
retry 3 ping foobar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment