Repository navigation
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·131 lines (121 loc) · 3.97 KB
/
Copy pathstart.sh
File metadata and controls
executable file
·131 lines (121 loc) · 3.97 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/sh
# ARTEX 守护启动脚本(Linux / macOS / Docker ENTRYPOINT)
#
# 用法:
# ./start.sh 前台运行(Ctrl-C 停止)
# nohup ./start.sh >artex.log 2>&1 & 后台常驻
# ./start.sh -addr :9000 额外参数原样透传给 artex
#
# 它只做一件事:把 artex 跑起来,进程退出后按退出码决定要不要再拉起。
#
# 0 用户正常停止 → 退出循环
# 75 程序请求重启 → 立刻重跑(页面点了"一键更新"或"回滚")
# 其他 崩溃 → 退避后重跑(1→2→4…最多 60 秒)
#
# 刻意不在这里做下载、SHA256 校验或换装:那些逻辑在 sh 和 bat 上要写两套,
# 而它们恰恰是最不能出错的一环——一旦换上跑不起来的二进制,本脚本会忠实地
# 反复拉起它,用户只能上机器手工救。所以校验/换装全部留在 Go 里(selfupdate 包),
# 由 artex 自己在启动时完成,脚本保持傻瓜化。
set -u
cd "$(dirname "$0")" || exit 1
detect_script_lang() {
override=$(printf '%s' "${ARTEX_LANG:-}" | tr '[:upper:]' '[:lower:]')
case "$override" in
zh|zh_*|zh-*) printf 'zh\n'; return 0 ;;
en|en_*|en-*) printf 'en\n'; return 0 ;;
esac
tz=""
if [ -n "${TZ:-}" ]; then
tz=${TZ#:}
elif [ -r /etc/timezone ]; then
tz=$(tr -d ' \t\r\n' < /etc/timezone 2>/dev/null || true)
fi
if [ -z "$tz" ] && [ -L /etc/localtime ]; then
tz=$(readlink /etc/localtime 2>/dev/null || true)
fi
if [ -z "$tz" ] && command -v timedatectl >/dev/null 2>&1; then
tz=$(timedatectl show -p Timezone --value 2>/dev/null || true)
fi
case "$tz" in
*zoneinfo/*) tz=${tz#*zoneinfo/} ;;
esac
tz_lc=$(printf '%s' "$tz" | tr '[:upper:]' '[:lower:]')
case "$tz_lc" in
asia/shanghai|asia/chongqing|asia/chungking|asia/harbin|asia/kashgar|asia/urumqi|\
asia/hong_kong|asia/macau|asia/macao|asia/taipei|\
prc|roc|hongkong|etc/gmt-8|cst-8|cst-8:*|hkt|hkt-8*)
printf 'zh\n'
return 0
;;
"")
abbr=$(date +%Z 2>/dev/null || true)
offset=$(date +%z 2>/dev/null || true)
if { [ "$abbr" = "CST" ] && [ "$offset" = "+0800" ]; } || [ "$abbr" = "HKT" ]; then
printf 'zh\n'
return 0
fi
;;
esac
printf 'en\n'
}
ARTEX_SCRIPT_LANG=$(detect_script_lang)
msg() {
if [ "$ARTEX_SCRIPT_LANG" = "zh" ]; then
printf '%s' "$1"
else
printf '%s' "$2"
fi
}
BIN=./artex
[ -x "$BIN" ] || { printf '%s\n' "$(msg "[artex] 找不到可执行文件 $BIN" "[artex] Executable not found: $BIN")" >&2; exit 1; }
RESTART_CODE=75
MAX_DELAY=60
child=0
stopping=0
# 转发停止信号给 artex 本体。
#
# Docker 下这是必需的:docker stop 只把 SIGTERM 发给 PID 1(也就是本脚本),
# 不会发给子进程。不转发的话 artex 收不到信号、做不了优雅关闭,10 秒后被 SIGKILL
# 硬杀,正在跑的任务直接断在半路。
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=$!
# 信号会打断 wait 并让它返回 >128。此时子进程其实还在做优雅关闭,
# 必须再 wait 一次才能拿到它真正的退出码。
wait "$child"
code=$?
if [ "$code" -gt 128 ]; then
wait "$child"
code=$?
fi
child=0
if [ "$stopping" -eq 1 ]; then
printf '%s\n' "$(msg "[artex] 已停止" "[artex] Stopped")"
exit 0
fi
case "$code" in
0)
printf '%s\n' "$(msg "[artex] 正常退出" "[artex] Exited cleanly")"
exit 0
;;
"$RESTART_CODE")
# 更新/回滚已就绪:重跑后 artex 会在启动时完成换装(见 selfupdate.Bootstrap)。
printf '%s\n' "$(msg "[artex] 请求重启(应用新版本)…" "[artex] Restart requested (applying update)...")"
delay=1
;;
*)
printf '%s\n' "$(msg "[artex] 异常退出 (code=$code),${delay}s 后重启" "[artex] Exited unexpectedly (code=$code), restarting in ${delay}s")" >&2
sleep "$delay"
delay=$((delay * 2))
[ "$delay" -gt "$MAX_DELAY" ] && delay=$MAX_DELAY
;;
esac
done