Skip to content

Commit 54d1f0b

Browse files
author
Douglas Gibbons
committed
Merge branch 'fwoelffel-master'
2 parents 4dd67a6 + 9995b72 commit 54d1f0b

File tree

1 file changed

+67
-66
lines changed

1 file changed

+67
-66
lines changed

wait-for-it.sh

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/env bash
22
# Use this script to test if a given TCP host/port are available
33

4-
cmdname=${0##*/}
4+
WAITFORIT_cmdname=${0##*/}
55

6-
echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }
6+
echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }
77

88
usage()
99
{
1010
cat << USAGE >&2
1111
Usage:
12-
$cmdname host:port [-s] [-t timeout] [-- command args]
12+
$WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args]
1313
-h HOST | --host=HOST Host or IP under test
1414
-p PORT | --port=PORT TCP port under test
1515
Alternatively, you specify the host and port as host:port
@@ -24,101 +24,101 @@ USAGE
2424

2525
wait_for()
2626
{
27-
if [[ $TIMEOUT -gt 0 ]]; then
28-
echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT"
27+
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
28+
echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
2929
else
30-
echoerr "$cmdname: waiting for $HOST:$PORT without a timeout"
30+
echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout"
3131
fi
32-
start_ts=$(date +%s)
32+
WAITFORIT_start_ts=$(date +%s)
3333
while :
3434
do
35-
if [[ $ISBUSY -eq 1 ]]; then
36-
nc -z $HOST $PORT
37-
result=$?
35+
if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then
36+
nc -z $WAITFORIT_HOST $WAITFORIT_PORT
37+
WAITFORIT_result=$?
3838
else
39-
(echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1
40-
result=$?
39+
(echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1
40+
WAITFORIT_result=$?
4141
fi
42-
if [[ $result -eq 0 ]]; then
43-
end_ts=$(date +%s)
44-
echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"
42+
if [[ $WAITFORIT_result -eq 0 ]]; then
43+
WAITFORIT_end_ts=$(date +%s)
44+
echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds"
4545
break
4646
fi
4747
sleep 1
4848
done
49-
return $result
49+
return $WAITFORIT_result
5050
}
5151

5252
wait_for_wrapper()
5353
{
5454
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
55-
if [[ $QUIET -eq 1 ]]; then
56-
timeout $BUSYTIMEFLAG $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
55+
if [[ $WAITFORIT_QUIET -eq 1 ]]; then
56+
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
5757
else
58-
timeout $BUSYTIMEFLAG $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
58+
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
5959
fi
60-
PID=$!
61-
trap "kill -INT -$PID" INT
62-
wait $PID
63-
RESULT=$?
64-
if [[ $RESULT -ne 0 ]]; then
65-
echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT"
60+
WAITFORIT_PID=$!
61+
trap "kill -INT -$WAITFORIT_PID" INT
62+
wait $WAITFORIT_PID
63+
WAITFORIT_RESULT=$?
64+
if [[ $WAITFORIT_RESULT -ne 0 ]]; then
65+
echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
6666
fi
67-
return $RESULT
67+
return $WAITFORIT_RESULT
6868
}
6969

7070
# process arguments
7171
while [[ $# -gt 0 ]]
7272
do
7373
case "$1" in
7474
*:* )
75-
hostport=(${1//:/ })
76-
HOST=${hostport[0]}
77-
PORT=${hostport[1]}
75+
WAITFORIT_hostport=(${1//:/ })
76+
WAITFORIT_HOST=${WAITFORIT_hostport[0]}
77+
WAITFORIT_PORT=${WAITFORIT_hostport[1]}
7878
shift 1
7979
;;
8080
--child)
81-
CHILD=1
81+
WAITFORIT_CHILD=1
8282
shift 1
8383
;;
8484
-q | --quiet)
85-
QUIET=1
85+
WAITFORIT_QUIET=1
8686
shift 1
8787
;;
8888
-s | --strict)
89-
STRICT=1
89+
WAITFORIT_STRICT=1
9090
shift 1
9191
;;
9292
-h)
93-
HOST="$2"
94-
if [[ $HOST == "" ]]; then break; fi
93+
WAITFORIT_HOST="$2"
94+
if [[ $WAITFORIT_HOST == "" ]]; then break; fi
9595
shift 2
9696
;;
9797
--host=*)
98-
HOST="${1#*=}"
98+
WAITFORIT_HOST="${1#*=}"
9999
shift 1
100100
;;
101101
-p)
102-
PORT="$2"
103-
if [[ $PORT == "" ]]; then break; fi
102+
WAITFORIT_PORT="$2"
103+
if [[ $WAITFORIT_PORT == "" ]]; then break; fi
104104
shift 2
105105
;;
106106
--port=*)
107-
PORT="${1#*=}"
107+
WAITFORIT_PORT="${1#*=}"
108108
shift 1
109109
;;
110110
-t)
111-
TIMEOUT="$2"
112-
if [[ $TIMEOUT == "" ]]; then break; fi
111+
WAITFORIT_TIMEOUT="$2"
112+
if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi
113113
shift 2
114114
;;
115115
--timeout=*)
116-
TIMEOUT="${1#*=}"
116+
WAITFORIT_TIMEOUT="${1#*=}"
117117
shift 1
118118
;;
119119
--)
120120
shift
121-
CLI=("$@")
121+
WAITFORIT_CLI=("$@")
122122
break
123123
;;
124124
--help)
@@ -131,47 +131,48 @@ do
131131
esac
132132
done
133133

134-
if [[ "$HOST" == "" || "$PORT" == "" ]]; then
134+
if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then
135135
echoerr "Error: you need to provide a host and port to test."
136136
usage
137137
fi
138138

139-
TIMEOUT=${TIMEOUT:-15}
140-
STRICT=${STRICT:-0}
141-
CHILD=${CHILD:-0}
142-
QUIET=${QUIET:-0}
139+
WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15}
140+
WAITFORIT_STRICT=${WAITFORIT_STRICT:-0}
141+
WAITFORIT_CHILD=${WAITFORIT_CHILD:-0}
142+
WAITFORIT_QUIET=${WAITFORIT_QUIET:-0}
143143

144144
# check to see if timeout is from busybox?
145-
TIMEOUT_PATH=$(type -p timeout)
146-
TIMEOUT_PATH=$(realpath $TIMEOUT_PATH 2>/dev/null || readlink -f $TIMEOUT_PATH)
147-
if [[ $TIMEOUT_PATH =~ "busybox" ]]; then
148-
ISBUSY=1
149-
BUSYTIMEFLAG="-t"
145+
WAITFORIT_TIMEOUT_PATH=$(type -p timeout)
146+
WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH)
147+
if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then
148+
WAITFORIT_ISBUSY=1
149+
WAITFORIT_BUSYTIMEFLAG="-t"
150+
150151
else
151-
ISBUSY=0
152-
BUSYTIMEFLAG=""
152+
WAITFORIT_ISBUSY=0
153+
WAITFORIT_BUSYTIMEFLAG=""
153154
fi
154155

155-
if [[ $CHILD -gt 0 ]]; then
156+
if [[ $WAITFORIT_CHILD -gt 0 ]]; then
156157
wait_for
157-
RESULT=$?
158-
exit $RESULT
158+
WAITFORIT_RESULT=$?
159+
exit $WAITFORIT_RESULT
159160
else
160-
if [[ $TIMEOUT -gt 0 ]]; then
161+
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
161162
wait_for_wrapper
162-
RESULT=$?
163+
WAITFORIT_RESULT=$?
163164
else
164165
wait_for
165-
RESULT=$?
166+
WAITFORIT_RESULT=$?
166167
fi
167168
fi
168169

169-
if [[ $CLI != "" ]]; then
170-
if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then
171-
echoerr "$cmdname: strict mode, refusing to execute subprocess"
172-
exit $RESULT
170+
if [[ $WAITFORIT_CLI != "" ]]; then
171+
if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then
172+
echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess"
173+
exit $WAITFORIT_RESULT
173174
fi
174-
exec "${CLI[@]}"
175+
exec "${WAITFORIT_CLI[@]}"
175176
else
176-
exit $RESULT
177+
exit $WAITFORIT_RESULT
177178
fi

0 commit comments

Comments
 (0)