39 lines
1.1 KiB
Bash
Executable File
39 lines
1.1 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] 服务已在运行, 跳过"
|
|
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
|