-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·67 lines (57 loc) · 1.48 KB
/
entrypoint.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
#!/usr/bin/env bash
process_scripts() {
local path=$1
if [[ ! -d $path ]]; then
echo "> Entrypoint point path script '${path}' does not exist."
echo "> Skipping.."
else
for script in $(ls $path | sort -n); do
echo "> Sourcing '${path}/${script}'.."
source "${path}/${script}"
done
fi
}
kill_remaining_process() {
# Gather all PIDs except for pid 1 (entrypoint script) into a
# space separated list, send SIGTERM and wait for those process to finish properly
# ignore the process if it is a bash shell
ps -e > /tmp/ps
local PIDS=`grep -v -E "PID|\s1\s|ps" /tmp/ps | awk 'BEGIN { ORS=" " }; {print $1}'`;
if [ -n "$PIDS" ]; then
kill -TERM $PIDS
for PID in $PIDS; do
processName=$(cat /proc/$PID/cmdline)
echo "> Waiting for process $PID ($processName) to finish.."
if [[ "$processName" == "bash" ]]; then
echo "> Skipping bash shell process.."
continue
fi
while [[ -d /proc/$PID ]]; do
sleep 0.1
done
done
fi
}
finish() {
trap "" SIGTERM SIGQUIT SIGINT
echo '> Stopping all services..'
process_scripts "${ENTRYPOINT_ROOT}/stop.d"
echo '> Killing remaining process..'
kill_remaining_process
echo '> Shutting down now.'
}
trap finish SIGTERM SIGQUIT SIGINT
if [ -z ${ENTRYPOINT_ROOT+x} ]; then
ENTRYPOINT_ROOT="/k"
fi
echo '> Starting all services..'
process_scripts "${ENTRYPOINT_ROOT}/start.d"
echo '> Fully Booted.'
if [[ -n "${*}" ]]; then
echo "> Executing \`${*}\`"
/bin/bash -c "${*}" &
else
sleep infinity &
fi
wait $!
finish