-
Notifications
You must be signed in to change notification settings - Fork 1
/
wait-for-it.sh
executable file
·106 lines (91 loc) · 1.74 KB
/
wait-for-it.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env sh
##!/usr/bin/env bash
# check if netcat is installed
check=$(nc &> /dev/null; echo $?)
if [[ ! "$check" -eq 1 ]]; then
echo Error: 'nc' not found.
exit 1
fi
cmdname=$(basename $0)
usage()
{
cat << USAGE >&2
Usage:
./$cmdname -h host -p port [-c command] [-t timeout] [-q quiet]
-h Host or IP under test
-p TCP port under test
-c Command to execute if test succeeds
-t Timeout in seconds, zero for no timeout
-q Don't output any status messages
USAGE
exit 1
}
# process arguments
while [[ $# -gt 0 ]]
do
case "$1" in
-h)
HOST="$2"
shift 2
;;
-p)
PORT="$2"
shift 2
;;
-c)
COMMAND="$2"
shift 2
;;
-t)
TIMEOUT="$2"
shift 2
;;
-q)
QUIET=1
shift 1
;;
--help)
usage
;;
*)
echoerr "Unknown argument: $1"
usage
;;
esac
done
if [[ "$HOST" == "" || "$PORT" == "" ]]; then
echo "Error: you need to provide a host and port to test."
usage
fi
TIMEOUT=${TIMEOUT:-0}
QUIET=${QUIET:-0}
wait=1
step=1
run=0
if [[ ! $QUIET -eq 1 ]]; then
echo Testing $HOST:$PORT
fi
while :
do
result=$(nc $HOST $PORT -w $wait -z &> /dev/null; echo $?)
if [ "$result" -eq "0" ]; then
run=1
break
fi
if [[ ! $QUIET -eq 1 ]]; then
echo $step
fi
sleep 1
if [ $step -eq $TIMEOUT ] && [ ! $TIMEOUT -eq 0 ]; then
break
fi
step=$((step+1))
done
if [ $run -eq 1 ]; then
if [[ ! $QUIET -eq 1 ]]; then
echo Success
fi
if [[ ! "$COMMAND" == "" ]]; then
exec $COMMAND
fi
fi