Skip to content

Instantly share code, notes, and snippets.

@erchn
Created September 26, 2016 19:05
Show Gist options
  • Save erchn/a3a5e4903a2e545a888b181d3fae21ca to your computer and use it in GitHub Desktop.
Save erchn/a3a5e4903a2e545a888b181d3fae21ca to your computer and use it in GitHub Desktop.
retry running things with a few arguments, goes well with the Cronitor wrapper.
#!/bin/bash
num=5
retrycode=99
sleep=300
usage() {
cat <<EOF
$0 [-r retry_exit_code] [-s sleep_secs] [-n times_to_loop]
EOF
}
while getopts ":n:r:s:" opt; do
case $opt in
n)
num=$OPTARG
;;
s)
sleep=$OPTARG
;;
r)
retrycode=$OPTARG
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
exit 1
;;
\?)
usage
exit 1
;;
esac
done
shift $(($OPTIND-1))
x=0
while true; do
$@
EXIT=$?
if [ $EXIT -eq $retrycode ]; then
if [ $((++x)) -eq $num ]; then
echo "command $@ failed $num times with exit code $retrycode, permanent failure..."
break
else
echo "command failed with code $retrycode, retrying in $sleep seconds"
sleep $sleep
fi
else
break
fi
done
exit $EXIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment