Skip to content

Instantly share code, notes, and snippets.

@omeranson
Created July 26, 2017 06:34
Show Gist options
  • Save omeranson/69af3ac509c586f2b5716ae8832637ea to your computer and use it in GitHub Desktop.
Save omeranson/69af3ac509c586f2b5716ae8832637ea to your computer and use it in GitHub Desktop.
retry.sh: Retry a command until it succeeds
#!/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