#!/bin/bash # 装第二版看门狗:30 秒一轮(原来 5 分钟)。 set -u RELAY_DIR=/root/lanremote-relay echo "===== 1. 装脚本 =====" chmod +x "$RELAY_DIR/watchdog.sh" echo " $(ls -la $RELAY_DIR/watchdog.sh | awk '{print $5" 字节"}')" echo echo "===== 2. 服务 =====" cat > /etc/systemd/system/lanremote-watchdog.service <<'EOF' [Unit] Description=LanRemote 隧道看门狗(看连接数,不是看公网) After=lanremote-relay.service lanremote-tunnel.service [Service] Type=oneshot ExecStart=/bin/bash /root/lanremote-relay/watchdog.sh MemoryMax=64M StandardOutput=append:/root/lanremote-relay/watchdog.log StandardError=append:/root/lanremote-relay/watchdog.log EOF echo echo "===== 3. 定时器:30 秒一轮 =====" cat > /etc/systemd/system/lanremote-watchdog.timer <<'EOF' [Unit] Description=每 30 秒检查一次隧道连接数 [Timer] OnBootSec=2min OnUnitActiveSec=30s AccuracySec=5s Unit=lanremote-watchdog.service [Install] WantedBy=timers.target EOF systemctl daemon-reload systemctl enable lanremote-watchdog.timer >/dev/null 2>&1 systemctl restart lanremote-watchdog.timer echo " 已启用" echo echo "===== 4. 立刻跑一次 =====" : > "$RELAY_DIR/watchdog.log" systemctl start lanremote-watchdog.service sleep 6 tail -5 "$RELAY_DIR/watchdog.log" 2>/dev/null | sed 's/^/ /' || echo " (没输出)" echo echo "===== 5. 定时器状态 =====" systemctl list-timers lanremote-watchdog.timer --no-pager 2>/dev/null | head -3 | sed 's/^/ /' echo echo "===== 6. 当前连接数 =====" echo -n " ha_connections = " curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}' echo -n " 看门狗状态: " cat "$RELAY_DIR/watchdog.state" 2>/dev/null | tr '\n' ' ' echo echo echo "===== 7. 观察 2 分钟,看它有没有乱重启 =====" BEFORE=$(systemctl show lanremote-tunnel -p NRestarts | cut -d= -f2) sleep 120 AFTER=$(systemctl show lanremote-tunnel -p NRestarts | cut -d= -f2) echo " 这 2 分钟里隧道自动重启次数: $AFTER(之前 $BEFORE)" echo " 看门狗日志尾部:" tail -6 "$RELAY_DIR/watchdog.log" 2>/dev/null | sed 's/^/ /' echo echo -n " 当前连接数: " curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}'