Repository navigation
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·70 lines (61 loc) · 1.8 KB
/
Copy pathstart.sh
File metadata and controls
executable file
·70 lines (61 loc) · 1.8 KB
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
68
69
70
#!/bin/sh
# ARTEX supervisor start script (Linux / macOS / Docker ENTRYPOINT)
#
# Usage:
# ./start.sh run in the foreground (Ctrl-C to stop)
# nohup ./start.sh >artex.log 2>&1 & run persistently in the background
# ./start.sh -addr :9000 extra arguments are passed through to artex as-is
#
# It does exactly one thing: run artex and, once the process exits, decide by exit code whether to relaunch it.
#
# 0 user stopped normally -> exit the loop
# other crash -> rerun after backoff (1→2→4... up to 60 seconds)
set -u
cd "$(dirname "$0")" || exit 1
BIN=./artex
[ -x "$BIN" ] || { echo "[artex] executable $BIN not found" >&2; exit 1; }
MAX_DELAY=60
child=0
stopping=0
# Forward stop signals to artex itself.
#
# Under Docker this is required: docker stop sends SIGTERM only to PID 1 (this script),
# never to the child. Without forwarding, artex never sees the signal and cannot shut down gracefully; after 10 seconds it gets
# hard-killed by SIGKILL and any running task is cut off mid-flight.
forward() {
stopping=1
if [ "$child" -ne 0 ]; then
kill -TERM "$child" 2>/dev/null || true
fi
}
trap forward INT TERM
delay=1
while :; do
"$BIN" "$@" &
child=$!
# A signal interrupts wait and makes it return >128. At that point the child is still shutting down gracefully;
# wait must be called once more to get its real exit code.
wait "$child"
code=$?
if [ "$code" -gt 128 ]; then
wait "$child"
code=$?
fi
child=0
if [ "$stopping" -eq 1 ]; then
echo "[artex] stopped"
exit 0
fi
case "$code" in
0)
echo "[artex] exited normally"
exit 0
;;
*)
echo "[artex] abnormal exit (code=$code), restarting in ${delay}s" >&2
sleep "$delay"
delay=$((delay * 2))
[ "$delay" -gt "$MAX_DELAY" ] && delay=$MAX_DELAY
;;
esac
done