feat: 添加 keepalive 保活脚本 + cron 定时检查
- 每5分钟检查服务是否运行 - 自动杀掉占用端口的非 node 进程(如 Python http.server) - 自动重启 node 服务
This commit is contained in:
Executable
+45
@@ -0,0 +1,45 @@
|
|||||||
|
#!/bash
|
||||||
|
# data-chart-tool 保活脚本
|
||||||
|
# 检查服务是否运行,如果没有则启动
|
||||||
|
|
||||||
|
PORT=16016
|
||||||
|
APP_DIR="/home/openclaw/.openclaw/workspace-hz4th_coder/works/data-chart-tool"
|
||||||
|
LOG_FILE="$APP_DIR/logs/server.log"
|
||||||
|
|
||||||
|
# 检查端口是否被占用
|
||||||
|
if ss -tlnp | grep -q ":$PORT "; then
|
||||||
|
# 检查是不是 node 进程
|
||||||
|
PID=$(ss -tlnp | grep ":$PORT " | grep -oP 'pid=\K[0-9]+' | head -1)
|
||||||
|
if [ -n "$PID" ]; then
|
||||||
|
CMD=$(ps -p "$PID" -o comm= 2>/dev/null)
|
||||||
|
if [ "$CMD" = "node" ] || [ "$CMD" = "MainThread" ]; then
|
||||||
|
# node 服务在运行,检查是否正常响应
|
||||||
|
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:$PORT/api/health 2>/dev/null)
|
||||||
|
if [ "$HTTP_CODE" = "200" ]; then
|
||||||
|
echo "[$(date)] Service OK (PID=$PID, HTTP=$HTTP_CODE)"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
# 端口被非 node 进程占用(如 Python http.server),杀掉它
|
||||||
|
echo "[$(date)] Port $PORT occupied by $CMD (PID=$PID), killing..."
|
||||||
|
kill -9 "$PID" 2>/dev/null
|
||||||
|
sleep 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 启动 node 服务
|
||||||
|
echo "[$(date)] Starting data-chart-tool..."
|
||||||
|
cd "$APP_DIR"
|
||||||
|
nohup node server.js >> "$LOG_FILE" 2>&1 &
|
||||||
|
echo "[$(date)] Started with PID $!"
|
||||||
|
|
||||||
|
# 等待启动
|
||||||
|
sleep 2
|
||||||
|
|
||||||
|
# 验证
|
||||||
|
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:$PORT/api/health 2>/dev/null)
|
||||||
|
if [ "$HTTP_CODE" = "200" ]; then
|
||||||
|
echo "[$(date)] Service started successfully (HTTP=$HTTP_CODE)"
|
||||||
|
else
|
||||||
|
echo "[$(date)] WARNING: Service may not have started correctly (HTTP=$HTTP_CODE)"
|
||||||
|
fi
|
||||||
Reference in New Issue
Block a user