forked from raksha-life/rescuekerala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait.sh
60 lines (52 loc) · 1.23 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
52
53
54
55
56
57
58
59
60
#!/bin/bash
should_run=true
after_boot() {
if [ ! -z "$AFTER_BOOT" ]; then
chmod +x $AFTER_BOOT
"$AFTER_BOOT"
echo "After boot exited with code $?"
fi
}
before_shutdown() {
should_run=false
if [ ! -z "$BEFORE_SHUTDOWN" ]; then
chmod +x $BEFORE_SHUTDOWN
"$BEFORE_SHUTDOWN"
echo "Before shutdown exited with code $?"
exit $?
fi
}
wait_for() {
if [ ! -z "$WAIT_FOR" ]; then
for i in $(echo $WAIT_FOR | tr "," "\n")
do
array=$(echo $i | tr ":" "\n")
host="${array[0]}"
port="${array[1]}"
code=1
while [ "$code" != "0" ] && [ should_run ]
do
echo "Waiting for $i to come up..."
nc -z -v -w5 $host $port
code=$?
echo "Check exited with code $code."
if [ "$code" != "0" ]; then
sleep 5
fi
done
echo "$i is up. Continuing."
done
fi;
}
trap before_shutdown HUP INT TERM KILL
wait_for;
echo "Starting command $@..."
"$@" &
PID=$!
echo "Command $@ is running with PID $PID"
echo "Starting after boot..."
after_boot;
echo "Finished after boot."
echo "Ready."
wait $PID
exit $?