A basic container for a private MediaGoblin media server.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wait-for 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/sh
  2. TIMEOUT=15
  3. QUIET=0
  4. echoerr() {
  5. if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
  6. }
  7. usage() {
  8. exitcode="$1"
  9. cat << USAGE >&2
  10. Usage:
  11. $cmdname host:port [-t timeout] [-- command args]
  12. -q | --quiet Do not output any status messages
  13. -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
  14. -- COMMAND ARGS Execute command with args after the test finishes
  15. USAGE
  16. exit "$exitcode"
  17. }
  18. wait_for() {
  19. for i in `seq $TIMEOUT` ; do
  20. nc -z "$HOST" "$PORT" > /dev/null 2>&1
  21. result=$?
  22. if [ $result -eq 0 ] ; then
  23. if [ $# -gt 0 ] ; then
  24. exec "$@"
  25. fi
  26. exit 0
  27. fi
  28. sleep 1
  29. done
  30. echo "Operation timed out" >&2
  31. exit 1
  32. }
  33. while [ $# -gt 0 ]
  34. do
  35. case "$1" in
  36. *:* )
  37. HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
  38. PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
  39. shift 1
  40. ;;
  41. -q | --quiet)
  42. QUIET=1
  43. shift 1
  44. ;;
  45. -t)
  46. TIMEOUT="$2"
  47. if [ "$TIMEOUT" = "" ]; then break; fi
  48. shift 2
  49. ;;
  50. --timeout=*)
  51. TIMEOUT="${1#*=}"
  52. shift 1
  53. ;;
  54. --)
  55. shift
  56. break
  57. ;;
  58. --help)
  59. usage 0
  60. ;;
  61. *)
  62. echoerr "Unknown argument: $1"
  63. usage 1
  64. ;;
  65. esac
  66. done
  67. if [ "$HOST" = "" -o "$PORT" = "" ]; then
  68. echoerr "Error: you need to provide a host and port to test."
  69. usage 2
  70. fi
  71. wait_for "$@"