Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple host checks #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
wait-for-it.sh host:port [-s] [-t timeout] [-- command args]
-h HOST | --host=HOST Host or IP 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
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT
Expand All @@ -27,6 +27,15 @@ wait-for-it.sh: www.google.com:80 is available after 0 seconds
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:

```
Expand Down
54 changes: 32 additions & 22 deletions wait-for-it.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/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##*/}

Expand All @@ -12,7 +12,8 @@ Usage:
$WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args]
-h HOST | --host=HOST Host or IP 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
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT
Expand Down Expand Up @@ -67,14 +68,19 @@ wait_for_wrapper()
return $WAITFORIT_RESULT
}

declare -a WAITFORIT_HOSTS
declare -a WAITFORIT_PORTS
I=0

# process arguments
while [[ $# -gt 0 ]]
do
case "$1" in
*:* )
WAITFORIT_hostport=(${1//:/ })
WAITFORIT_HOST=${WAITFORIT_hostport[0]}
WAITFORIT_PORT=${WAITFORIT_hostport[1]}
I=$((I + 1))
WAITFORIT_HOSTS[I]=${WAITFORIT_hostport[0]}
WAITFORIT_PORTS[I]=${WAITFORIT_hostport[1]}
shift 1
;;
--child)
Expand All @@ -90,21 +96,21 @@ do
shift 1
;;
-h)
WAITFORIT_HOST="$2"
if [[ $WAITFORIT_HOST == "" ]]; then break; fi
WAITFORIT_HOSTS[0]="$2"
if [[ $WAITFORIT_HOSTS[0] == "" ]]; then break; fi
shift 2
;;
--host=*)
WAITFORIT_HOST="${1#*=}"
WAITFORIT_HOSTS[0]="${1#*=}"
shift 1
;;
-p)
WAITFORIT_PORT="$2"
if [[ $WAITFORIT_PORT == "" ]]; then break; fi
WAITFORIT_PORTS[0]="$2"
if [[ $WAITFORIT_PORTS[0] == "" ]]; then break; fi
shift 2
;;
--port=*)
WAITFORIT_PORT="${1#*=}"
WAITFORIT_PORTS[0]="${1#*=}"
shift 1
;;
-t)
Expand All @@ -131,8 +137,8 @@ do
esac
done

if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then
echoerr "Error: you need to provide a host and port to test."
if [[ "${#WAITFORIT_HOSTS[*]}" == "0" || "${#WAITFORIT_PORTS[*]}" == "0" ]]; then
echoerr "Error: you need to provide at least one host and port to test."
usage
fi

Expand All @@ -153,19 +159,23 @@ else
WAITFORIT_BUSYTIMEFLAG=""
fi

if [[ $WAITFORIT_CHILD -gt 0 ]]; then
wait_for
WAITFORIT_RESULT=$?
exit $WAITFORIT_RESULT
else
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
wait_for_wrapper
WAITFORIT_RESULT=$?
else
for i in "${!WAITFORIT_HOSTS[@]}"; do
WAITFORIT_HOST=${WAITFORIT_HOSTS[$i]}
WAITFORIT_PORT=${WAITFORIT_PORTS[$i]}
if [[ $WAITFORIT_CHILD -gt 0 ]]; then
wait_for
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
done

if [[ $WAITFORIT_CLI != "" ]]; then
if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then
Expand Down