-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathwait-for
executable file
·58 lines (50 loc) · 1.12 KB
/
wait-for
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
#!/bin/sh
# Script taken from https://github.com/vnegrisolo/wait-for
log() {
echo "[wait-for] [`date +'%Y%m%d%H%M%S'`] $@"
}
usage() {
echo "Usage: `basename "$0"` [--timeout=15] <HOST>:<PORT> [<HOST_2>:<PORT_2>]"
}
unknown_arg() {
log "[ERROR] unknown argument: '$@'"
usage
exit 2
}
wait_for() {
timeout=$1 && host=$2 && port=$3
log "wait '$host':'$port' up to '$timeout'"
for i in `seq $timeout` ; do
if nc -z "$host" "$port" > /dev/null 2>&1 ; then
log "wait finish '$host:$port' [`expr $(date +%s) - $START`] seconds"
exit 0
fi
log "wait attempt '${host}:${port}' [$i]"
sleep 1
done
log "[ERROR] wait timeout of '$timeout' on '$host:$port'"
exit 1
}
trap 'kill $(jobs -p) &>/dev/null' EXIT
START=$(date +%s)
timeout=15
pids=""
for i in $@ ; do
case $i in
--timeout=*) timeout="${i#*=}" ;;
-t=*) timeout="${i#*=}" ;;
*:* )
wait_for "$timeout" "${i%%:*}" "${i##*:}" &
pids="$pids $!"
;;
*) unknown_arg "$i" ;;
esac
done
status=0
for pid in $pids; do
if ! wait $pid ; then
status=1
fi
done
log "wait done with status=$status"
exit $status