diff --git a/README.md b/README.md index f1956f7..7aa776b 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ When using this tool, you only need to pick the `wait-for` file as part of your ## Usage ``` -./wait-for host:port [-t timeout] [-- command args] +./wait-for host:port [host2:port2 ... [hostN:portN]] [-t timeout] [-- command args] -q | --quiet Do not output any status messages -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout -- COMMAND ARGS Execute command with args after the test finishes diff --git a/wait-for b/wait-for index ddfc39e..97d0a06 100755 --- a/wait-for +++ b/wait-for @@ -3,6 +3,10 @@ TIMEOUT=15 QUIET=0 +CONNS= +HOST= +PORT= + echoerr() { if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi } @@ -11,7 +15,7 @@ usage() { exitcode="$1" cat << USAGE >&2 Usage: - $cmdname host:port [-t timeout] [-- command args] + $cmdname host:port [host2:port2 ... [hostN:portN]] [-t timeout] [-- command args] -q | --quiet Do not output any status messages -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout -- COMMAND ARGS Execute command with args after the test finishes @@ -22,26 +26,23 @@ USAGE wait_for() { for i in `seq $TIMEOUT` ; do nc -z "$HOST" "$PORT" > /dev/null 2>&1 - + result=$? if [ $result -eq 0 ] ; then - if [ $# -gt 0 ] ; then - exec "$@" - fi - exit 0 + return 0 fi sleep 1 done - echo "Operation timed out" >&2 + echo "Operation timed out waiting for $HOST:$PORT" >&2 exit 1 } + while [ $# -gt 0 ] do case "$1" in *:* ) - HOST=$(printf "%s\n" "$1"| cut -d : -f 1) - PORT=$(printf "%s\n" "$1"| cut -d : -f 2) + CONNS="$CONNS $1" shift 1 ;; -q | --quiet) @@ -71,9 +72,23 @@ do esac done -if [ "$HOST" = "" -o "$PORT" = "" ]; then +if [ "$CONNS" = "" ]; then echoerr "Error: you need to provide a host and port to test." usage 2 fi -wait_for "$@" +result= + +for conn in $CONNS +do + HOST="$(printf "%s\n" "$conn"| cut -d : -f 1)" + PORT="$(printf "%s\n" "$conn"| cut -d : -f 2)" + + wait_for "$@" + result=$? +done + +if [ $result -eq 0 -a $# -gt 0 ] +then + exec "$@" +fi