#!/bin/bash # DMP 服务守护脚本 - 带日志和健康检查 LOG_DIR="/Users/inkling/Desktop/dmp/logs" mkdir -p "$LOG_DIR" SERVER_LOG="$LOG_DIR/server.log" TUNNEL_LOG="$LOG_DIR/tunnel.log" MONITOR_LOG="$LOG_DIR/monitor.log" echo "================================================" | tee -a "$MONITOR_LOG" echo "🚀 DMP 服务启动 - $(date)" | tee -a "$MONITOR_LOG" echo "================================================" | tee -a "$MONITOR_LOG" echo "" | tee -a "$MONITOR_LOG" # 停止旧进程 echo "🛑 停止旧进程..." | tee -a "$MONITOR_LOG" pkill -f "node server.js" 2>/dev/null pkill -f "cloudflared tunnel" 2>/dev/null sleep 2 # 进入项目目录 cd /Users/inkling/Desktop/dmp # 启动 Node.js 服务器 echo "📦 启动 Node.js 服务器..." | tee -a "$MONITOR_LOG" nohup node server.js > "$SERVER_LOG" 2>&1 & SERVER_PID=$! echo " PID: $SERVER_PID" | tee -a "$MONITOR_LOG" # 等待服务器启动 sleep 3 # 测试本地服务 echo "🔍 测试本地服务..." | tee -a "$MONITOR_LOG" if curl -s -f http://localhost:3456 > /dev/null 2>&1; then echo " ✅ 本地服务正常" | tee -a "$MONITOR_LOG" else echo " ❌ 本地服务启动失败" | tee -a "$MONITOR_LOG" echo " 查看日志: tail -f $SERVER_LOG" | tee -a "$MONITOR_LOG" exit 1 fi # 启动 Cloudflare Tunnel echo "🔗 启动 Cloudflare Tunnel..." | tee -a "$MONITOR_LOG" nohup cloudflared tunnel --config cloudflare-tunnel.yml run dmp-tunnel > "$TUNNEL_LOG" 2>&1 & TUNNEL_PID=$! echo " PID: $TUNNEL_PID" | tee -a "$MONITOR_LOG" # 等待 Tunnel 建立连接 echo "⏳ 等待 Tunnel 连接..." | tee -a "$MONITOR_LOG" sleep 5 # 检查 Tunnel 状态 if cloudflared tunnel info dmp-tunnel 2>&1 | grep -q "active connection"; then echo " ✅ Tunnel 已连接" | tee -a "$MONITOR_LOG" else echo " ⚠️ Tunnel 正在建立连接..." | tee -a "$MONITOR_LOG" fi echo "" | tee -a "$MONITOR_LOG" echo "================================================" | tee -a "$MONITOR_LOG" echo "✅ 服务启动完成" | tee -a "$MONITOR_LOG" echo "================================================" | tee -a "$MONITOR_LOG" echo "" | tee -a "$MONITOR_LOG" echo "📊 状态信息:" | tee -a "$MONITOR_LOG" echo " Node.js PID: $SERVER_PID" | tee -a "$MONITOR_LOG" echo " Tunnel PID: $TUNNEL_PID" | tee -a "$MONITOR_LOG" echo "" | tee -a "$MONITOR_LOG" echo "🌐 访问地址:" | tee -a "$MONITOR_LOG" echo " 本地: http://localhost:3456" | tee -a "$MONITOR_LOG" echo " 公网: https://dmp.ink1ing.tech" | tee -a "$MONITOR_LOG" echo "" | tee -a "$MONITOR_LOG" echo "📝 日志位置:" | tee -a "$MONITOR_LOG" echo " 服务器: $SERVER_LOG" | tee -a "$MONITOR_LOG" echo " Tunnel: $TUNNEL_LOG" | tee -a "$MONITOR_LOG" echo " 监控: $MONITOR_LOG" | tee -a "$MONITOR_LOG" echo "" | tee -a "$MONITOR_LOG" echo "🔍 查看日志:" | tee -a "$MONITOR_LOG" echo " tail -f $TUNNEL_LOG" | tee -a "$MONITOR_LOG" echo "" | tee -a "$MONITOR_LOG" echo "🛑 停止服务:" | tee -a "$MONITOR_LOG" echo " pkill -f 'node server.js'" | tee -a "$MONITOR_LOG" echo " pkill -f 'cloudflared tunnel'" | tee -a "$MONITOR_LOG" echo "" | tee -a "$MONITOR_LOG" echo "================================================" | tee -a "$MONITOR_LOG" echo "" | tee -a "$MONITOR_LOG" # 保存 PID 到文件 echo "$SERVER_PID" > "$LOG_DIR/server.pid" echo "$TUNNEL_PID" > "$LOG_DIR/tunnel.pid" echo "💡 提示: 服务在后台运行,关闭此窗口不影响服务" echo "" echo "按回车键查看实时 Tunnel 日志,或按 Ctrl+C 退出..." read -t 5 # 显示实时日志 tail -f "$TUNNEL_LOG"