diff --git a/wait-for-it.sh b/wait-for-it.sh index eb722b0..071c2be 100755 --- a/wait-for-it.sh +++ b/wait-for-it.sh @@ -1,15 +1,15 @@ #!/usr/bin/env bash # Use this script to test if a given TCP host/port are available -cmdname=${0##*/} +WAITFORIT_cmdname=${0##*/} -echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } +echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } usage() { cat << USAGE >&2 Usage: - $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 -p PORT | --port=PORT TCP port under test Alternatively, you specify the host and port as host:port @@ -24,47 +24,47 @@ USAGE wait_for() { - if [[ $TIMEOUT -gt 0 ]]; then - echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT" + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" else - echoerr "$cmdname: waiting for $HOST:$PORT without a timeout" + echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout" fi - start_ts=$(date +%s) + WAITFORIT_start_ts=$(date +%s) while : do - if [[ $ISBUSY -eq 1 ]]; then - nc -z $HOST $PORT - result=$? + if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then + nc -z $WAITFORIT_HOST $WAITFORIT_PORT + WAITFORIT_result=$? else - (echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1 - result=$? + (echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1 + WAITFORIT_result=$? fi - if [[ $result -eq 0 ]]; then - end_ts=$(date +%s) - echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds" + if [[ $WAITFORIT_result -eq 0 ]]; then + WAITFORIT_end_ts=$(date +%s) + echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds" break fi sleep 1 done - return $result + return $WAITFORIT_result } wait_for_wrapper() { # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 - if [[ $QUIET -eq 1 ]]; then - timeout $BUSYTIMEFLAG $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT & + if [[ $WAITFORIT_QUIET -eq 1 ]]; then + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & else - timeout $BUSYTIMEFLAG $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT & + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & fi - PID=$! - trap "kill -INT -$PID" INT - wait $PID - RESULT=$? - if [[ $RESULT -ne 0 ]]; then - echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT" + WAITFORIT_PID=$! + trap "kill -INT -$WAITFORIT_PID" INT + wait $WAITFORIT_PID + WAITFORIT_RESULT=$? + if [[ $WAITFORIT_RESULT -ne 0 ]]; then + echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" fi - return $RESULT + return $WAITFORIT_RESULT } # process arguments @@ -72,53 +72,53 @@ while [[ $# -gt 0 ]] do case "$1" in *:* ) - hostport=(${1//:/ }) - HOST=${hostport[0]} - PORT=${hostport[1]} + WAITFORIT_hostport=(${1//:/ }) + WAITFORIT_HOST=${WAITFORIT_hostport[0]} + WAITFORIT_PORT=${WAITFORIT_hostport[1]} shift 1 ;; --child) - CHILD=1 + WAITFORIT_CHILD=1 shift 1 ;; -q | --quiet) - QUIET=1 + WAITFORIT_QUIET=1 shift 1 ;; -s | --strict) - STRICT=1 + WAITFORIT_STRICT=1 shift 1 ;; -h) - HOST="$2" - if [[ $HOST == "" ]]; then break; fi + WAITFORIT_HOST="$2" + if [[ $WAITFORIT_HOST == "" ]]; then break; fi shift 2 ;; --host=*) - HOST="${1#*=}" + WAITFORIT_HOST="${1#*=}" shift 1 ;; -p) - PORT="$2" - if [[ $PORT == "" ]]; then break; fi + WAITFORIT_PORT="$2" + if [[ $WAITFORIT_PORT == "" ]]; then break; fi shift 2 ;; --port=*) - PORT="${1#*=}" + WAITFORIT_PORT="${1#*=}" shift 1 ;; -t) - TIMEOUT="$2" - if [[ $TIMEOUT == "" ]]; then break; fi + WAITFORIT_TIMEOUT="$2" + if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi shift 2 ;; --timeout=*) - TIMEOUT="${1#*=}" + WAITFORIT_TIMEOUT="${1#*=}" shift 1 ;; --) shift - CLI=("$@") + WAITFORIT_CLI=("$@") break ;; --help) @@ -131,47 +131,48 @@ do esac done -if [[ "$HOST" == "" || "$PORT" == "" ]]; then +if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then echoerr "Error: you need to provide a host and port to test." usage fi -TIMEOUT=${TIMEOUT:-15} -STRICT=${STRICT:-0} -CHILD=${CHILD:-0} -QUIET=${QUIET:-0} +WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15} +WAITFORIT_STRICT=${WAITFORIT_STRICT:-0} +WAITFORIT_CHILD=${WAITFORIT_CHILD:-0} +WAITFORIT_QUIET=${WAITFORIT_QUIET:-0} # check to see if timeout is from busybox? -TIMEOUT_PATH=$(type -p timeout) -TIMEOUT_PATH=$(realpath $TIMEOUT_PATH 2>/dev/null || readlink -f $TIMEOUT_PATH) -if [[ $TIMEOUT_PATH =~ "busybox" ]]; then - ISBUSY=1 - BUSYTIMEFLAG="-t" +WAITFORIT_TIMEOUT_PATH=$(type -p timeout) +WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH) +if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then + WAITFORIT_ISBUSY=1 + WAITFORIT_BUSYTIMEFLAG="-t" + else - ISBUSY=0 - BUSYTIMEFLAG="" + WAITFORIT_ISBUSY=0 + WAITFORIT_BUSYTIMEFLAG="" fi -if [[ $CHILD -gt 0 ]]; then +if [[ $WAITFORIT_CHILD -gt 0 ]]; then wait_for - RESULT=$? - exit $RESULT + WAITFORIT_RESULT=$? + exit $WAITFORIT_RESULT else - if [[ $TIMEOUT -gt 0 ]]; then + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then wait_for_wrapper - RESULT=$? + WAITFORIT_RESULT=$? else wait_for - RESULT=$? + WAITFORIT_RESULT=$? fi fi -if [[ $CLI != "" ]]; then - if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then - echoerr "$cmdname: strict mode, refusing to execute subprocess" - exit $RESULT +if [[ $WAITFORIT_CLI != "" ]]; then + if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then + echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess" + exit $WAITFORIT_RESULT fi - exec "${CLI[@]}" + exec "${WAITFORIT_CLI[@]}" else - exit $RESULT + exit $WAITFORIT_RESULT fi