上一版给 push.sh 加的 pid 比对刚跑就露馅了:`sh: pgrep: command not found`, 于是 BEFORE 和 AFTER 都是空字符串,`[ -n "$BEFORE" ]` 直接跳过检查——我刚加 的安全网自己就是个摆设。 顺手查了一圈,stop.sh 和 start.sh 里同样的 pgrep 也一直在空转: - stop.sh 的 `pgrep ... || exit 0` 每次都以 127 失败,永远匹配不到那个提前 返回,所以每次停服都白等满 15 秒,也从没真正确认过残留 worker 已经没了。 - start.sh 的等待循环同理,白等 20 秒。 DSM 有 pkill 没有 pgrep,而且非特权的 ps 看不见 root 起的进程(服务是开机 以 root 启动的),两者任一都会让检查静默返回「没有」——这正是最危险的答案, 因为它长得和「已经停干净了」一模一样。 - push.sh 改用 `sudo ps -eo pid,args | grep`;并且 AFTER 为空时直接报错退出, 不再把「看不见」当成「通过」 - stop.sh / start.sh 改用 `ps -eo args | grep -q` Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
52 lines
1.6 KiB
Bash
Executable File
52 lines
1.6 KiB
Bash
Executable File
#!/bin/sh
|
|
# Start Garmin Health Lab. Safe to run repeatedly: an already-running
|
|
# instance is stopped first. Intended for DSM Task Scheduler (boot-up).
|
|
APP="$(cd "$(dirname "$0")/.." && pwd)"
|
|
cd "$APP/backend" || exit 1
|
|
|
|
GUNICORN="$APP/backend/.venv/bin/gunicorn"
|
|
|
|
# Stop whatever is already running, then WAIT for the port to actually be
|
|
# free. Killing only the pid in app.pid left orphaned workers holding :8124;
|
|
# the new master then started, reported success, and served nothing — the
|
|
# site was down while every log line looked normal.
|
|
"$APP/deploy/stop.sh" >/dev/null 2>&1
|
|
|
|
# `ps | grep`, not pgrep: DSM has no pgrep, so this loop used to fail with 127
|
|
# every second and wait the full 20s whether or not anything was still running.
|
|
alive() { ps -eo args 2>/dev/null | grep -q "^$GUNICORN"; }
|
|
|
|
i=0
|
|
while [ $i -lt 20 ]; do
|
|
alive || break
|
|
sleep 1
|
|
i=$((i + 1))
|
|
done
|
|
pkill -9 -f "$GUNICORN" 2>/dev/null
|
|
sleep 1
|
|
|
|
mkdir -p "$APP/logs"
|
|
# --timeout 300: an AI generation against the reasoning model can run for
|
|
# minutes, and gunicorn kills a worker that looks stuck before then.
|
|
nohup "$GUNICORN" \
|
|
--workers 2 --threads 4 --timeout 300 \
|
|
--bind 0.0.0.0:8124 \
|
|
--access-logfile "$APP/logs/access.log" \
|
|
--error-logfile "$APP/logs/error.log" \
|
|
wsgi:app > "$APP/logs/stdout.log" 2>&1 &
|
|
|
|
echo $! > "$APP/app.pid"
|
|
|
|
# Confirm it is actually serving rather than just running.
|
|
i=0
|
|
while [ $i -lt 25 ]; do
|
|
if curl -sf -m 2 -o /dev/null http://127.0.0.1:8124/api/health/status; then
|
|
echo "started pid $(cat "$APP/app.pid") on :8124"
|
|
exit 0
|
|
fi
|
|
sleep 1
|
|
i=$((i + 1))
|
|
done
|
|
|
|
echo "FAILED to serve on :8124 — see $APP/logs/error.log" >&2
|
|
exit 1 |