From fd01fcebf14e0689943bdf6b865bdcd07c62c86f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Migu=C3=A9lez=20Palomo?= Date: Fri, 14 Dec 2018 14:38:27 +0100 Subject: [PATCH 1/2] Added support for multiple expressions host:port --- wait-for | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) 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 From ccd77ae27e94cdb11d2734cf9268349cef9e850f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Migu=C3=A9lez=20Palomo?= Date: Fri, 14 Dec 2018 14:39:34 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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