-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrapper.sh
executable file
·61 lines (49 loc) · 1.22 KB
/
trapper.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
#!/usr/bin/env bash
#
# Created by brugnara on 03/05/19,
# @ daniele@brugnara.me
#
# Thanks and credits to: Grigorii Chudnov for the original script I only adapted to my needs
# https://medium.com/@gchudnov/trapping-signals-in-docker-containers-7a57fdda7d86
set -x
pid=0
my_wait() {
while [ -e /proc/$1 ]
do
echo "waiting for process $1"
sleep .5
done
echo "process $1 is dead"
}
# SIGUSR1-handler
my_handler() {
echo "my_handler"
}
# SIGTERM-handler
term_handler() {
# let's get the gremlin-server pid in order to be able to kill
# I noticed that the $pid is always 24, funny but I need to be sure.
pid=$(ps aux | grep [j]ava | awk '{print $1}')
if [ $pid -ne 0 ]; then
echo "killing $pid"
kill -SIGTERM "$pid"
my_wait "$pid"
fi
echo "exiting.."
exit 143; # 128 + 15 -- SIGTERM
}
# setup handlers
# on callback, kill the last background process, which is `tail -f /dev/null` and execute the specified handler
trap 'kill ${!}; my_handler' SIGUSR1
trap 'kill ${!}; term_handler' SIGTERM
# run application
. /docker-entrypoint.sh conf/gremlin-server.yaml &
pid="$!"
echo "PID is $pid"
# wait forever
echo "Waiting forever!"
while true
do
tail -f /dev/null & wait ${!}
done
echo "out of wait"