From cac0af49f3f8a93842e09832184671a2e00cea36 Mon Sep 17 00:00:00 2001 From: lou Date: Mon, 10 Aug 2026 12:45:21 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=90=AF=E5=8A=A8+=E7=9C=8B=E9=97=A8?= =?UTF-8?q?=E7=8B=97:=20start.sh=20=E5=B9=82=E7=AD=89=E6=8B=89=E8=B5=B7?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1+watchdog,=20watchdog=2030s=20=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E8=87=AA=E5=8A=A8=E6=81=A2=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- start.sh | 38 ++++++++++++++++++++++++++++++++++++++ watchdog.sh | 22 ++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100755 start.sh create mode 100755 watchdog.sh diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..81d9c80 --- /dev/null +++ b/start.sh @@ -0,0 +1,38 @@ +#!/bin/bash +# 7z-encrypt 服务端自启动脚本 (幂等: 已在跑则跳过) +# 用法: /root/server/start.sh [--force] +set -e + +cd /root/server + +if pgrep -f 'python3 [m]ain\.py' > /dev/null 2>&1 && [ "$1" != "--force" ]; then + echo "[start] 服务已在运行, 跳过" + exit 0 +fi + +# 杀掉残留进程 (防重复拉起), 再启动 +pkill -9 -f 'python3 [m]ain\.py' 2>/dev/null || true +sleep 1 + +# 后台常驻: setsid + nohup 防 SSH 断连杀进程; SZ_TOKEN 与客户端预共享 +(setsid nohup env SZ_TOKEN=sz-token-95197 python3 main.py > /tmp/sz-server.log 2>&1 &) + +# 等启动完成 +for i in 1 2 3 4 5 6; do + sleep 1 + grep -q 'startup complete' /tmp/sz-server.log 2>/dev/null && break +done + +if grep -q 'startup complete' /tmp/sz-server.log 2>/dev/null; then + echo "[start] 服务已启动 (PID $(pgrep -f 'python3 [m]ain\.py' | head -1))" +else + echo "[start] 启动失败, 日志尾部:" + tail -5 /tmp/sz-server.log + exit 1 +fi + +# 拉起看门狗 (防重复, watchdog.sh 自身检查) +if ! pgrep -f 'watchdog[.]sh' > /dev/null 2>&1; then + (setsid nohup /root/server/watchdog.sh > /dev/null 2>&1 &) + echo "[start] 看门狗已启动" +fi diff --git a/watchdog.sh b/watchdog.sh new file mode 100755 index 0000000..54f66dd --- /dev/null +++ b/watchdog.sh @@ -0,0 +1,22 @@ +#!/bin/bash +# 7z-encrypt 服务看门狗: 每 30s 检查服务进程, 挂了自动拉起 +# 由 start.sh 启动 (容器唤醒后自动恢复); 日志 /tmp/sz-watchdog.log +LOGFILE=/tmp/sz-watchdog.log + +log() { + echo "$(date '+%F %T') $1" >> "$LOGFILE" +} + +# 防重复: 已有 watchdog 实例则退出 +if pgrep -f 'watchdog[.]sh' > /dev/null 2>&1; then + exit 0 +fi + +log "[watchdog] 启动, PID $$" +while true; do + if ! pgrep -f 'python3 [m]ain\.py' > /dev/null 2>&1; then + log "[watchdog] 服务挂了, 拉起..." + /root/server/start.sh >> "$LOGFILE" 2>&1 || log "[watchdog] 拉起失败" + fi + sleep 30 +done