From 79a1ca36c593ae48720030b98fa22a4548a584c1 Mon Sep 17 00:00:00 2001 From: hz4th_coder Date: Fri, 24 Jul 2026 17:34:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20keepalive=20?= =?UTF-8?q?=E4=BF=9D=E6=B4=BB=E8=84=9A=E6=9C=AC=20+=20cron=20=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 每5分钟检查服务是否运行 - 自动杀掉占用端口的非 node 进程(如 Python http.server) - 自动重启 node 服务 --- keepalive.sh | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 keepalive.sh diff --git a/keepalive.sh b/keepalive.sh new file mode 100755 index 0000000..002f3d4 --- /dev/null +++ b/keepalive.sh @@ -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