Allow multiple host checks

pull/79/head
Ben Wolfe 5 years ago
parent 54d1f0bfeb
commit 3f4f2de63c

@ -8,7 +8,7 @@
wait-for-it.sh host:port [-s] [-t timeout] [-- command args] wait-for-it.sh host:port [-s] [-t timeout] [-- command args]
-h HOST | --host=HOST Host or IP under test -h HOST | --host=HOST Host or IP under test
-p PORT | --port=PORT TCP port under test -p PORT | --port=PORT TCP port under test
Alternatively, you specify the host and port as host:port Alternatively, you specify the host and port as host:port, multiple parameters allowed
-s | --strict Only execute subcommand if the test succeeds -s | --strict Only execute subcommand if the test succeeds
-q | --quiet Don't output any status messages -q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT -t TIMEOUT | --timeout=TIMEOUT
@ -27,6 +27,15 @@ wait-for-it.sh: www.google.com:80 is available after 0 seconds
google is up google is up
``` ```
```
$ ./wait-for-it.sh www.google.com:80 images.google.com:80 -- echo "both google search and google images are up"
wait-for-it.sh: waiting 15 seconds for www.google.com:80
wait-for-it.sh: www.google.com:80 is available after 0 seconds
wait-for-it.sh: waiting 15 seconds for images.google.com:80
wait-for-it.sh: images.google.com:80 is available after 0 seconds
both google search and google images are up
```
You can set your own timeout with the `-t` or `--timeout=` option. Setting the timeout value to 0 will disable the timeout: You can set your own timeout with the `-t` or `--timeout=` option. Setting the timeout value to 0 will disable the timeout:
``` ```

@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Use this script to test if a given TCP host/port are available # Use this script to test if a given TCP host/port(s) are available
WAITFORIT_cmdname=${0##*/} WAITFORIT_cmdname=${0##*/}
@ -12,7 +12,8 @@ Usage:
$WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args] $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args]
-h HOST | --host=HOST Host or IP under test -h HOST | --host=HOST Host or IP under test
-p PORT | --port=PORT TCP port under test -p PORT | --port=PORT TCP port under test
Alternatively, you specify the host and port as host:port Alternatively, you specify the host and port as host:port,
multiple parameters allowed
-s | --strict Only execute subcommand if the test succeeds -s | --strict Only execute subcommand if the test succeeds
-q | --quiet Don't output any status messages -q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT -t TIMEOUT | --timeout=TIMEOUT
@ -67,14 +68,19 @@ wait_for_wrapper()
return $WAITFORIT_RESULT return $WAITFORIT_RESULT
} }
declare -a WAITFORIT_HOSTS
declare -a WAITFORIT_PORTS
I=0
# process arguments # process arguments
while [[ $# -gt 0 ]] while [[ $# -gt 0 ]]
do do
case "$1" in case "$1" in
*:* ) *:* )
WAITFORIT_hostport=(${1//:/ }) WAITFORIT_hostport=(${1//:/ })
WAITFORIT_HOST=${WAITFORIT_hostport[0]} I=$((I + 1))
WAITFORIT_PORT=${WAITFORIT_hostport[1]} WAITFORIT_HOSTS[I]=${WAITFORIT_hostport[0]}
WAITFORIT_PORTS[I]=${WAITFORIT_hostport[1]}
shift 1 shift 1
;; ;;
--child) --child)
@ -90,21 +96,21 @@ do
shift 1 shift 1
;; ;;
-h) -h)
WAITFORIT_HOST="$2" WAITFORIT_HOSTS[0]="$2"
if [[ $WAITFORIT_HOST == "" ]]; then break; fi if [[ $WAITFORIT_HOSTS[0] == "" ]]; then break; fi
shift 2 shift 2
;; ;;
--host=*) --host=*)
WAITFORIT_HOST="${1#*=}" WAITFORIT_HOSTS[0]="${1#*=}"
shift 1 shift 1
;; ;;
-p) -p)
WAITFORIT_PORT="$2" WAITFORIT_PORTS[0]="$2"
if [[ $WAITFORIT_PORT == "" ]]; then break; fi if [[ $WAITFORIT_PORTS[0] == "" ]]; then break; fi
shift 2 shift 2
;; ;;
--port=*) --port=*)
WAITFORIT_PORT="${1#*=}" WAITFORIT_PORTS[0]="${1#*=}"
shift 1 shift 1
;; ;;
-t) -t)
@ -131,8 +137,8 @@ do
esac esac
done done
if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then if [[ "${#WAITFORIT_HOSTS[*]}" == "0" || "${#WAITFORIT_PORTS[*]}" == "0" ]]; then
echoerr "Error: you need to provide a host and port to test." echoerr "Error: you need to provide at least one host and port to test."
usage usage
fi fi
@ -153,19 +159,23 @@ else
WAITFORIT_BUSYTIMEFLAG="" WAITFORIT_BUSYTIMEFLAG=""
fi fi
if [[ $WAITFORIT_CHILD -gt 0 ]]; then for i in "${!WAITFORIT_HOSTS[@]}"; do
wait_for WAITFORIT_HOST=${WAITFORIT_HOSTS[$i]}
WAITFORIT_RESULT=$? WAITFORIT_PORT=${WAITFORIT_PORTS[$i]}
exit $WAITFORIT_RESULT if [[ $WAITFORIT_CHILD -gt 0 ]]; then
else
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
wait_for_wrapper
WAITFORIT_RESULT=$?
else
wait_for wait_for
WAITFORIT_RESULT=$? WAITFORIT_RESULT=$?
exit $WAITFORIT_RESULT
else
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
wait_for_wrapper
WAITFORIT_RESULT=$?
else
wait_for
WAITFORIT_RESULT=$?
fi
fi fi
fi done
if [[ $WAITFORIT_CLI != "" ]]; then if [[ $WAITFORIT_CLI != "" ]]; then
if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then

Loading…
Cancel
Save