-
Notifications
You must be signed in to change notification settings - Fork 1
/
wait-for-socket.sh
executable file
·84 lines (74 loc) · 1.68 KB
/
wait-for-socket.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
#!/bin/bash -e
usage () {
echo "usage: $0 [-r] [-i interval] [-t timeout] [-v] host port" >&2
exit 1
}
interval=5
timeout=60
verbose=0
outputremaining=0
while getopts ":i:rt:v" opt; do
case "${opt}" in
(i)
interval=$OPTARG
;;
(r)
outputremaining=1
;;
(t)
timeout=$OPTARG
;;
(v)
verbose=1
;;
(*)
usage
;;
esac
done
shift $((OPTIND-1))
host=$1; shift || usage
port=$1; shift || usage
if [[ ${host%%:*} == ${host} ]]; then
NAME="${host}:$port"
else
set -f
NAME="[${host}]:$port"
set +f
fi
if (( outputremaining && verbose )); then
echo "output remaining and verbose are mutually exclusive!" >&2
exit 1
fi
now () {
date +%s
}
base=$(now)
until=$(($base + $timeout))
remaining=$(($until - $(now)))
while true; do
if (( $verbose )); then
if (echo -n | nc -z "$host" "$port"); then
echo "$NAME is ready in $(($(now) - $base)) of ${timeout}s" >&2
break
fi
else
if (echo -n | nc -z "$host" "$port") > /dev/null 2> /dev/null; then
echo "$NAME is ready in $(($(now) - $base)) of ${timeout}s" >&2
break
fi
fi
remaining=$(($until - $(now)))
if (( remaining <= 0 )); then
echo "$NAME not ready, giving up after $(($(now) - $base))s" >&2
if (( $outputremaining )); then
echo 0
fi
exit 1
fi
echo "$NAME not yet ready in $(($(now) - $base))s of ${timeout}s, retrying in ${interval}s" >&2
sleep $interval
done
if (( $outputremaining )); then
echo $remaining
fi