Guest User

Untitled

a guest
Jun 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. #!/bin/bash
  2. command="$1"
  3. max_tries="$2"
  4. interval="$3"
  5. : ${max_tries:="5"}
  6. : ${interval:="10"}
  7.  
  8. # Load sane bash defaults
  9. source "$( dirname "$0" )/../lib/sanebash"
  10. source "$( dirname "$0" )/../lib/pretty"
  11.  
  12. # Override shell options
  13. set +o errexit
  14.  
  15. printf "Running command: \""
  16. printf.green "$command"
  17. echo "\""
  18.  
  19. n=1
  20. until [[ "$n" -ge "$max_tries" ]]
  21. do
  22. eval "$command" && {
  23. printf.green "Success "
  24. echo "(tries: \"$n\")"
  25. exit_code="0"
  26. break
  27. }
  28. exit_code="$?"
  29.  
  30. printf.yellow "Failed "
  31. echo "(tries: \"$n\", waiting \"$interval\" seconds before trying again)"
  32.  
  33. sleep "$interval"
  34. n="$(( n + 1 ))"
  35. done
  36.  
  37. if [[ "$exit_code" -ne 0 ]]
  38. then
  39. printf.red "Fail "
  40. echo "Never completed successfully after \"$n\" tries."
  41. echo "Quitting."
  42. fi
  43.  
  44. exit "$exit_code"
Add Comment
Please, Sign In to add comment