Created
July 26, 2017 06:34
-
-
Save omeranson/69af3ac509c586f2b5716ae8832637ea to your computer and use it in GitHub Desktop.
retry.sh: Retry a command until it succeeds
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 | |
# defaults | |
timeout=2s | |
function usage() { | |
echo "USAGE: $0 [-i interval] <command>" | |
echo " Retry running <command> every <interval> until it succeeds" | |
} | |
# parse command line | |
while true; do | |
case $1 in | |
-h|--help) | |
usage | |
exit 0 | |
;; | |
-i|--interval) | |
shift | |
timeout=$1 | |
shift | |
;; | |
*) | |
command="$*" | |
break | |
;; | |
esac | |
done | |
# prereqs | |
if [ -z "$command" ]; then | |
usage | |
exit 1 | |
fi | |
# run | |
while true; do | |
$command | |
if [ $? -eq 0 ]; then | |
break; | |
fi | |
sleep $timeout | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment