Files
lou 4b25231c01 新增孤儿清理工具 + 看门狗每小时自动清理 + start.sh 无条件补拉看门狗
- clean_orphans.py: 只删数据库无记录的孤儿 (tmp 终态/僵尸任务卷, storage 无记录目录)
  - 目录级判断: files 记录指向的目录整体跳过, 内部文件永不单独删除 (防误删)
  - 路径 resolve 统一, 调用方 cwd 无关
  - 边界: tmp 终态超 1h 删/进行中保留/超 7 天判僵尸; storage 孤儿超 24h 删
- watchdog.sh: 每 30s 保活服务 + 每小时自动跑清理 (失败 30s 后重试)
- start.sh: 看门狗拉起移到无条件位置 (原 main.py 在跑时 exit 短路, 看门狗死亡无人接管)
- tests: 新增 test_clean_orphans 5 项 (含有效目录内 chunk 不删的误删回归断言)
- README: 数据目录补 tmp, 新增运维节, 测试数 13->17
2026-08-11 14:22:28 +08:00

39 lines
1.3 KiB
Bash
Executable File

#!/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] 服务已在运行, 跳过"
else
# 杀掉残留进程 (防重复拉起), 再启动
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
fi
# 拉起看门狗 (无条件检查: main.py 在跑也会补拉, 防看门狗死亡后无人接管)
# pgrep 用 [.] 正则防自匹配 (远程命令字符串含字面量也会被 -f 匹配)
if ! pgrep -f 'watchdog[.]sh' > /dev/null 2>&1; then
(setsid nohup /root/server/watchdog.sh > /dev/null 2>&1 &)
echo "[start] 看门狗已启动"
fi