背景

主力机的 OpenClaw 有时会挂掉。没人盯着的话,可能挂好几个小时才发现。

我给备用机写了个监控脚本,每5分钟检查一次主力机。如果挂了,自动重启,还会发飞书通知。

监控脚本

备用机上创建脚本:

#!/bin/bash
# /home/WL/scripts/monitor_main.sh

MAIN_IP="192.168.2.63"
LOG_FILE="/home/WL/logs/monitor_main.log"

# 检查主力机 OpenClaw 进程
check_process() {
    ssh lei@$MAIN_IP "pgrep -f openclaw" 2>/dev/null
}

# 尝试重启(先用 systemctl,失败就用直接启动)
restart_process() {
    ssh lei@$MAIN_IP "systemctl restart openclaw-gateway || /home/lei/.npm-global/bin/openclaw gateway start" 2>/dev/null
}

# 发送飞书通知
send_alert() {
    curl -X POST "https://open.feishu.cn/open-apis/bot/v2/hook/xxx" \
        -H "Content-Type: application/json" \
        -d "{\"msg_type\":\"text\",\"content\":{\"text\":\"$1\"}}"
}

# 主逻辑
if ! check_process > /dev/null; then
    echo "$(date) - OpenClaw not running, restarting..." >> $LOG_FILE
    if restart_process; then
        send_alert "✅ 主力机 OpenClaw 已自动重启"
    else
        send_alert "❌ 主力机 OpenClaw 重启失败,请人工干预"
    fi
fi

配置 cron

# 每5分钟执行一次
*/5 * * * * /home/WL/scripts/monitor_main.sh

效果

总结

这个方案让备用机成了主力机的"备用心脏"。主力机出问题,备用机自动修。

一人公司嘛,能自动化的尽量自动化。