Skip to content

Instantly share code, notes, and snippets.

@a-ludi
Last active February 10, 2020 09:41
Show Gist options
  • Save a-ludi/326a58b5a6b85c9c523833f614090438 to your computer and use it in GitHub Desktop.
Save a-ludi/326a58b5a6b85c9c523833f614090438 to your computer and use it in GitHub Desktop.
retry – retry a command until it succeeds
#!/bin/bash
# Unofficial Bash Strict Mode (http://redsymbol.net/articles/unofficial-bash-strict-mode/)
set -euo pipefail
IFS=$'\n\t'
PROG="$(basename "$0")"
VERSION="0.1.0"
UPSTREAM_URL='https://gist.github.com/a-ludi/326a58b5a6b85c9c523833f614090438'
function set_defaults()
{
ERROR_EXIT="${ERROR_EXIT:-42}"
NUM_RETRIES=3
}
function error()
{
if (( $# > 0 ));
then
echo "$PROG: error: $@"
else
echo
fi
} >&2
function bail_out()
{
[[ -v IS_SILENT ]] && exit "$ERROR_EXIT"
error "$@"
exit "$ERROR_EXIT"
}
function bail_out_usage()
{
[[ -v IS_SILENT ]] && exit "$ERROR_EXIT"
error "$@"
error
set_defaults
print_usage
exit "$ERROR_EXIT"
}
function warn()
{
[[ -v IS_SILENT ]] || echo "$PROG: [WARNING] $@"
} >&2
function print_usage()
{
echo "USAGE: $PROG [-eh] [-n<uint($NUM_RETRIES)>] [--] <command>..."
} >&2
function print_help()
{
print_usage
echo
echo 'This program executes <command> over and over again until it succeeds or the'
echo 'maximum number of retries is reached.'
echo
echo 'Positional arguments:'
echo ' <command>... A shell command to be executed.'
echo
echo 'Optional arguments:'
echo " --eval, -e Use 'eval' to execute <command>."
echo " --num-retries, -n Maximum number of retries (default: $NUM_RETRIES)."
echo ' --help, -h Prints this help.'
echo ' --usage Print a short command summary.'
echo ' --silent, -s Supress any output; even errors and warnings.'
echo ' --version Print software version.'
echo
echo 'Environment:'
echo " ERROR_EXIT Exit code if an error in '$PROG' happens."
echo ' LAST_EXIT Last exit code of <command>; initially set to 0.'
echo ' RETRY_ITERATION Number of failed iterations. This is exported to <command>'
echo ' and may be used to alter its parameters, e.g. allocate'
echo ' more resuorces.'
} >&2
function print_version()
{
echo "$PROG v$VERSION"
echo
echo 'This software is available for downloads at'
echo
echo " $UPSTREAM_URL"
echo
echo
echo "Copyright (c) 2020, Arne Ludwig <arne.ludwig@posteo.de>"
echo
echo 'Permission is hereby granted, free of charge, to any person obtaining a copy'
echo 'of this software and associated documentation files (the "Software"), to deal'
echo 'in the Software without restriction, including without limitation the rights'
echo 'to use, copy, modify, merge, publish, distribute, sublicense, and/or sell'
echo 'copies of the Software, and to permit persons to whom the Software is'
echo 'furnished to do so, subject to the following conditions:'
echo
echo 'The above copyright notice and this permission notice shall be included in all'
echo 'copies or substantial portions of the Software.'
echo
echo 'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR'
echo 'IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,'
echo 'FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE'
echo 'AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER'
echo 'LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,'
echo 'OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE'
echo 'SOFTWARE.'
} >&2
function parse_args()
{
for ARG in "$@";
do
case "$ARG" in
-e|--eval)
USE_EVAL=1
shift
;;
-n*|--num-retries=*)
NUM_RETRIES="${ARG#-n}"
NUM_RETRIES="${NUM_RETRIES#--num-retries=}"
shift
;;
-h|--help)
print_help
exit
;;
--usage)
print_usage
exit
;;
--silent|-s)
IS_SILENT=1
shift
;;
--version)
print_version
exit
;;
--)
shift
break
;;
*)
break
;;
esac
done
(( $# > 0 )) || bail_out_usage 'error: missing <command>' >&2
(( "$NUM_RETRIES" >= 0 )) || bail_out_usage 'error: --num-retries must be a non-negative integer' >&2
COMMAND=( "$@" )
(( "$NUM_RETRIES" > 0 )) || warn '<command> is not being executed because --num-retries=0' >&2
}
function run_command()
{
if [[ -v USE_EVAL ]];
then
eval "${COMMAND[@]}"
else
"${COMMAND[@]}"
fi
}
function main()
{
set_defaults
parse_args "$@"
export RETRY_ITERATION=0
export LAST_EXIT=0
while (( RETRY_ITERATION < NUM_RETRIES )); do
run_command && exit
LAST_EXIT="$?"
(( ++RETRY_ITERATION ))
done
exit "$LAST_EXIT"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment