-
Notifications
You must be signed in to change notification settings - Fork 4
/
wait.sh
executable file
·51 lines (41 loc) · 1.04 KB
/
wait.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
#!/bin/bash
set -e
timeout=${WAIT_HOSTS_TIMEOUT:-30}
waitAfterHosts=${WAIT_AFTER_HOSTS:-0}
waitBeforeHosts=${WAIT_BEFORE_HOSTS:-0}
echo "Waiting for ${waitBeforeHosts} seconds."
sleep $waitBeforeHosts
# our target format is a comma separated list where each item is "host:ip"
if [ -n "$WAIT_HOSTS" ]; then
uris=$(echo $WAIT_HOSTS | sed -e 's/,/ /g' -e 's/\s+/\n/g' | uniq)
fi
# wait for each target
if [ -z "$uris" ];
then echo "No wait targets found." >&2;
else
for uri in $uris
do
host=$(echo $uri | cut -d: -f1)
port=$(echo $uri | cut -d: -f2)
[ -n "${host}" ]
[ -n "${port}" ]
echo "Waiting for ${uri}."
seconds=0
while [ "$seconds" -lt "$timeout" ] && ! nc -z -w1 $host $port
do
echo -n .
seconds=$((seconds+1))
sleep 1
done
if [ "$seconds" -lt "$timeout" ]; then
echo "${uri} is up!"
else
echo " ERROR: unable to connect to ${uri}" >&2
exit 1
fi
done
echo "All hosts are up"
fi
echo "Waiting for ${waitAfterHosts} seconds."
sleep $waitAfterHosts
exit 0