OpenClaw Doctor — macOS Gateway 看门狗 v2
24/7 health watchdog for OpenClaw Gateway on macOS. Detects and auto-repairs four layers of failure.
Why
OpenClaw Gateway is the message bridge — if it dies, the AI assistant goes silent. Common failure modes on macOS:
| Layer | Failure | Symptom |
|---|
| L1 | Process crash | Complete silence |
| L2 |
HTTP port stuck | Complete silence |
| L3 | WebSocket 1006 disconnect | Process alive, HTTP OK, but
messages don't arrive (sneakiest!) |
| L4 | Mac sleep/hibernate | All connections die after lid close or idle timeout |
Architecture
CODEBLOCK0
Repair Strategy (escalating)
| Failure | Action | Rationale |
|---|
| Process down | INLINECODE0 → restart Gateway | Process crashed |
| HTTP unreachable |
Restart Gateway | Port stuck |
| WS frequent disconnect | Restart Node first → then full restart | WS layer issue, Node restart usually sufficient |
| Post-sleep recovery | Gateway + Node dual restart | All connections need rebuild |
Setup
1. Copy the script
CODEBLOCK1
2. Configure (optional)
Environment variables for customization:
CODEBLOCK2
3. Install LaunchAgent
Edit references/ai.openclaw.doctor.plist — replace REPLACE_WITH_HOME with your actual home directory path (e.g. /Users/yourname).
CODEBLOCK3
4. Verify
CODEBLOCK4
Logs
Location: ~/.openclaw/logs/doctor.log
| Prefix | Meaning |
|---|
| HEARTBEAT | Periodic health confirmation (hourly) |
| ALERT |
Anomaly detected |
| DIAGNOSIS | Fault classification |
| ACTION | Repair in progress |
| RESOLVED | Fault repaired |
| CRITICAL | All repair attempts failed |
| INFO | General info (e.g. caffeinate started) |
Auto-rotates at 5MB (keeps last 1000 lines).
Troubleshooting
Process alive + HTTP OK but messages don't arrive?
→ WS disconnect. Check: INLINECODE5
Disconnects every night?
→ Mac sleep. Verify caffeinate: INLINECODE6
Doctor itself not running?
→ Check LaunchAgent: launchctl list | grep doctor. Reload if missing.
Temporarily disable:
CODEBLOCK5
Files
- -
scripts/doctor.sh — Main watchdog script - INLINECODE9 — LaunchAgent template
Version History
| Version | Date | Changes |
|---|
| v1 | 2026-03-07 | Initial: process check + HTTP probe + Gateway restart |
| v2 |
2026-03-23 | WS health check + Mac sleep prevention + layered repair + Node restart |
技能名称: openclaw-doctor
详细描述:
OpenClaw Doctor — macOS 网关看门狗 v2
针对 macOS 上 OpenClaw 网关的 24/7 健康监控程序。检测并自动修复四层故障。
为什么需要它
OpenClaw 网关是消息桥梁——如果它宕机,AI 助手将陷入沉默。macOS 上常见的故障模式:
| 层级 | 故障 | 症状 |
|------|------|------|
| L1 | 进程崩溃 | 完全静默 |
| L2 | HTTP 端口卡死 | 完全静默 |
| L3 | WebSocket 1006 断开 | 进程存活,HTTP 正常,但消息无法送达(最隐蔽!) |
| L4 | Mac 睡眠/休眠 | 合盖或空闲超时后所有连接中断 |
架构
LaunchAgent(每 5 分钟)
→ L1: pgrep + launchctl(进程存活?)
→ L2: curl HTTP 探测(端口响应?)
→ L3: 日志扫描 WS 1006 错误(通信健康?)
→ L4: caffeinate 保活(防止睡眠)
→ 遵循最小干预原则的自动修复
修复策略(逐步升级)
| 故障 | 操作 | 理由 |
|------|------|------|
| 进程崩溃 | openclaw doctor --repair → 重启网关 | 进程崩溃 |
| HTTP 不可达 | 重启网关 | 端口卡死 |
| WS 频繁断开 | 先重启 Node → 然后完全重启 | WS 层问题,通常重启 Node 即可 |
| 睡眠后恢复 | 网关 + Node 双重重启 | 所有连接需要重建 |
设置
1. 复制脚本
bash
cp scripts/doctor.sh ~/.openclaw/doctor.sh
chmod +x ~/.openclaw/doctor.sh
2. 配置(可选)
用于自定义的环境变量:
bash
在 ~/.bashrc 或 ~/.zshrc 中(若非默认值)
export OPENCLAW
DOCTORGATEWAY_URL=http://127.0.0.1:18789 # 默认值
export OPENCLAW
DOCTORSERVICE_NAME=ai.openclaw.gateway # 默认值
export OPENCLAW
DOCTORNODE_SERVICE=ai.openclaw.node # 默认值
3. 安装 LaunchAgent
编辑 references/ai.openclaw.doctor.plist — 将 REPLACEWITHHOME 替换为你的实际主目录路径(例如 /Users/你的用户名)。
bash
复制并自定义 plist
cp references/ai.openclaw.doctor.plist ~/Library/LaunchAgents/ai.openclaw.doctor.plist
sed -i s|REPLACE
WITHHOME|$HOME|g ~/Library/LaunchAgents/ai.openclaw.doctor.plist
加载服务
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/ai.openclaw.doctor.plist
4. 验证
bash
手动测试运行
bash ~/.openclaw/doctor.sh
检查日志
tail -10 ~/.openclaw/logs/doctor.log
确认 caffeinate 正在运行
pgrep -la caffeinate
确认 LaunchAgent 已加载
launchctl list | grep doctor
日志
位置:~/.openclaw/logs/doctor.log
| 前缀 | 含义 |
|------|------|
| HEARTBEAT | 定期健康确认(每小时) |
| ALERT | 检测到异常 |
| DIAGNOSIS | 故障分类 |
| ACTION | 修复进行中 |
| RESOLVED | 故障已修复 |
| CRITICAL | 所有修复尝试均失败 |
| INFO | 一般信息(例如 caffeinate 已启动) |
日志在 5MB 时自动轮转(保留最后 1000 行)。
故障排除
进程存活 + HTTP 正常但消息无法送达?
→ WS 断开。检查:grep 1006 /tmp/openclaw/openclaw-$(date +%Y-%m-%d).log | tail -10
每晚断开连接?
→ Mac 睡眠。验证 caffeinate:pgrep -la caffeinate
Doctor 本身未运行?
→ 检查 LaunchAgent:launchctl list | grep doctor。如果缺失则重新加载。
临时禁用:
bash
launchctl bootout gui/$(id -u)/ai.openclaw.doctor
文件
- - scripts/doctor.sh — 主看门狗脚本
- references/ai.openclaw.doctor.plist — LaunchAgent 模板
版本历史
| 版本 | 日期 | 变更 |
|------|------|------|
| v1 | 2026-03-07 | 初始版本:进程检查 + HTTP 探测 + 网关重启 |
| v2 | 2026-03-23 | WS 健康检查 + Mac 睡眠预防 + 分层修复 + Node 重启 |