Skip to content

Instantly share code, notes, and snippets.

@mikecharles
Last active August 24, 2016 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikecharles/8d50bf041bfd632525c5b5ee43ac1f73 to your computer and use it in GitHub Desktop.
Save mikecharles/8d50bf041bfd632525c5b5ee43ac1f73 to your computer and use it in GitHub Desktop.
Keep trying a command every X minutes, and stop after Y minutes have passed, or the command succeeds
#!/bin/sh
#---------------------------------------------------------------------------------------------------
# Usage
#
usage() {
printf "$(basename "$0") -s <TIME_BETWEEN_TRIES> -t <TIME_TO_TRY> <COMMAND>\n"
printf " where:\n"
printf " -s TIME_BETWEEN_TRIES time (minutes) between each try\n"
printf " -t TIME_TO_TRY total time (minutes) to try the command before giving up\n"
printf " COMMAND command to try\n"
}
#---------------------------------------------------------------------------------------------------
# Check command-line args
#
# Defaults
time_between_tries=0
time_to_try=0
while getopts ':s:t:h' option; do
case "$option" in
s)
time_between_tries=$OPTARG
;;
t)
time_to_try=$OPTARG
;;
h)
usage
exit
;;
\?) printf "Invalid option: -%s\n" "$OPTARG" >&2
usage >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [ $time_between_tries -eq 0 ] && [ $time_to_try -eq 0 ] ; then
usage ; exit
fi
if [[ "$#" -lt 1 ]] ; then
usage ; exit
fi
#---------------------------------------------------------------------------------------------------
# Get command-line arguments
#
command="$@"
#---------------------------------------------------------------------------------------------------
# Define some colors for printfs
#
RED='\e[0;31m'
GREEN='\e[0;32m'
YELLOW='\e[0;33m'
BLUE='\e[0;34m'
MAGENTA='\e[0;35m'
CYAN='\e[0;36m'
WHITE='\e[0;37m'
BLACK='\e[0;38m'
BOLDYELLOW='\e[1;33m'
NOCOLOR='\e[m'
#---------------------------------------------------------------------------------------------------
# Print what's going to happen
#
printf "\n${CYAN}===>${NOCOLOR} Trying the command ${YELLOW}${command}${NOCOLOR} every ${BLUE}${time_between_tries}${NOCOLOR} minutes for ${MAGENTA}${time_to_try}${NOCOLOR} minutes...\n\n"
#---------------------------------------------------------------------------------------------------
# Try the command
#
# Try the command the first time
eval $command
# Capture the return code
return_code=$?
# Print success or failure
if [[ $return_code -eq 0 ]] ; then
printf "\n${CYAN}===>${NOCOLOR} Command ${GREEN}SUCCEEDED${NOCOLOR} the first time\n"
exit 0
fi
# Keep trying again if it failed
try_count=1
while (( $return_code != 0 )) && (( $SECONDS < $((time_to_try * 60)) )) ; do
# Update try count
try_count=$(($try_count + 1))
# Print failure
printf "\n${CYAN}===>${NOCOLOR} Command ${RED}FAILED${NOCOLOR}, trying again in ${time_between_tries} mins...\n\n"
# Sleep
sleep $((time_between_tries * 60))
# Try command
eval $command
# Capture the return code
return_code=$?
done
# Print success or failure
if [[ $return_code -eq 0 ]] ; then
printf "\n${CYAN}===>${NOCOLOR} Command ${GREEN}SUCCEEDED${NOCOLOR} after ${try_count} tries\n"
else
minutes_passed=$((SECONDS / 60))
printf "\n${CYAN}===>${NOCOLOR} Command ${RED}FAILED${NOCOLOR} after ${try_count} tries (${minutes_passed} minutes)\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment