Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- command="$1"
- max_tries="$2"
- interval="$3"
- : ${max_tries:="5"}
- : ${interval:="10"}
- # Load sane bash defaults
- source "$( dirname "$0" )/../lib/sanebash"
- source "$( dirname "$0" )/../lib/pretty"
- # Override shell options
- set +o errexit
- printf "Running command: \""
- printf.green "$command"
- echo "\""
- n=1
- until [[ "$n" -ge "$max_tries" ]]
- do
- eval "$command" && {
- printf.green "Success "
- echo "(tries: \"$n\")"
- exit_code="0"
- break
- }
- exit_code="$?"
- printf.yellow "Failed "
- echo "(tries: \"$n\", waiting \"$interval\" seconds before trying again)"
- sleep "$interval"
- n="$(( n + 1 ))"
- done
- if [[ "$exit_code" -ne 0 ]]
- then
- printf.red "Fail "
- echo "Never completed successfully after \"$n\" tries."
- echo "Quitting."
- fi
- exit "$exit_code"
Add Comment
Please, Sign In to add comment