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