Install
openclaw skills install openclaw-guardian-cnOpenClaw 系统守护 skill。每日定时自检、自动检测异常并尝试自救。 用于:(1) 每日定时检查 Gateway 状态 (2) 自动检测进程/WebSocket/插件异常 (3) 自动尝试恢复服务 (4) 用户可自定义自检时间 当用户要求设置每日自检、自助恢复、系统守护、自动重启 Gateway 时触发。
openclaw skills install openclaw-guardian-cn💡 跨平台支持:Linux + Windows 自动适配
cat ~/.openclaw/openclaw.json | grep -A 2 '"channels"'
ls ~/.openclaw/extensions/
openclaw gateway status
openclaw status
| 异常 | 检测方式 | 自救动作 |
|---|---|---|
| Gateway 未运行 | pgrep -f "openclaw-gateway" 无结果 | openclaw gateway start |
| 进程崩溃 | PID 不存在 | openclaw gateway restart |
| 端口占用 | 端口检查失败 | 释放端口或更换端口 |
| 连接断开 | WebSocket 1006 | 重启 Gateway |
每次自救操作后必须验证是否成功:
# 自救函数示例
try_recover() {
local action="$1"
echo "🔧 尝试自救: $action"
# 执行自救动作
eval "$action"
# 等待服务启动
sleep 5
# 验证是否成功
if pgrep -f "openclaw-gateway" > /dev/null; then
echo "✅ 自救成功"
return 0
else
echo "❌ 自救失败,需要手动处理"
return 1
fi
}
| 自救动作 | 验证命令 | 成功标志 |
|---|---|---|
| 启动 Gateway | pgrep -f "openclaw-gateway" | 返回 PID |
| 重启 Gateway | pgrep -f "openclaw-gateway" | 返回 PID |
| 重连插件 | openclaw status | 插件状态正常 |
1. 检测异常: Gateway 进程不存在
2. 记录状态: 当前进程数=0
3. 尝试自救: openclaw gateway start
4. 等待 5 秒
5. 验证结果: pgrep -f "openclaw-gateway"
6. 成功? → 报告 ✅ 已恢复
失败? → 报告 ❌ 需要手动处理
当自救失败时:
在 HEARTBEAT.md 中添加轻量检查,每次 Heartbeat 轮询时快速检查:
## OpenClaw Guardian
- 检查 Gateway 进程是否存活: `pgrep -f "openclaw gateway"`
- 如果进程不在,尝试启动: `openclaw gateway start`
- 如果之前正常,这次突然挂了,报告异常
检查频率:每 1-2 个 Heartbeat 周期做一次(避免太频繁)
告诉用户使用 openclaw cron 设置定时任务:
# 添加每日自检(早上 9 点)
openclaw cron add --name "guardian-daily" --schedule "0 9 * * *" --command "openclaw guardian check"
用户可以说:
首次运行自检时,自动检测操作系统并配置对应守护:
$OSTYPE 或 [Environment]::OSVersion# 自动配置脚本
# 注意:以下路径假设 OpenClaw 安装在 ~/.openclaw 目录下
# 如果你的安装路径不同,请修改对应的路径
OPENCLAW_PATH=~/.openclaw
if ! crontab -l 2>/dev/null | grep -q "openclaw-gateway"; then
(crontab -l 2>/dev/null; echo "*/5 * * * * pgrep -f \"openclaw-gateway\" > /dev/null || cd $OPENCLAW_PATH && node ./dist/daemon-cli.js start >> /tmp/gateway_guardian.log 2>&1") | crontab -
echo "✅ 已自动配置外部守护(每 5 分钟检查)"
else
echo "✅ 外部守护已配置"
fi
当检测到异常时:
自检完成后,报告格式:
🛡️ OpenClaw 自检报告
时间: YYYY-MM-DD HH:mm
----------------
✅ Gateway: 运行中 (PID: xxx)
✅ 内存: 45%
✅ 磁盘: 60%
----------------
自检完成,无异常
或:
🛡️ OpenClaw 自检报告
时间: YYYY-MM-DD HH:mm
----------------
❌ Gateway: 已停止
🔧 尝试自救: 重启 Gateway...
✅ 自救成功!
----------------
状态: 已恢复
问题:如果 Gateway 完全挂了,Agent 本身无法工作,需要外部守护。
解决方案:根据操作系统自动选择对应方案
首次自检时,自动检测并配置对应平台的守护:
# 检测操作系统
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo "🟢 Linux 系统,使用 Cron 守护"
elif [[ "$OSTYPE" == "cygwin" ]] || [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]]; then
echo "🪟 Windows 系统,使用任务计划程序"
else
echo "⚠️ 未知系统"
fi
当用户运行自检时,自动检查并配置 cron 守护:
# 检查是否已有守护
crontab -l | grep "openclaw-gateway"
# 如果没有,自动添加(路径需要根据实际安装位置修改)
# 假设 OpenClaw 安装在 ~/.openclaw
(crontab -l 2>/dev/null; echo '*/5 * * * * pgrep -f "openclaw-gateway" > /dev/null || cd ~/.openclaw && node ./dist/daemon-cli.js start >> /tmp/gateway_guardian.log 2>&1') | crontab -
系统 Cron(独立于 Gateway)
↓ 每 5 分钟
pgrep 检查 Gateway 进程
↓ 如果不存在
执行启动命令
如果需要修改检查间隔:
crontab -e
# 每 3 分钟检查一次
*/3 * * * * pgrep -f "openclaw-gateway" > /dev/null || /path/to/start_gateway.sh
/tmp/gateway_guardian.log当检测到 Windows 系统时,使用以下方案:
复制 scripts/guardian-windows.ps1 到用户目录并配置:
# 1. 修改脚本中的 Gateway 路径
# 2. 设置开机自启
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -File C:\Users\你的用户名\.openclaw\scripts\guardian-windows.ps1"
$trigger = New-ScheduledTaskTrigger -At Startup
Register-ScheduledTask -TaskName "OpenClaw Guardian" -Trigger $trigger -Action $action -RunLevel Highest
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -File C:\Users\你的用户名\.openclaw\scripts\guardian-windows.ps1"
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 5) -RepetitionDuration (New-TimeSpan -Days 9999)
Register-ScheduledTask -TaskName "OpenClaw Guardian" -Trigger $trigger -Action $action -RunLevel Highest
# 检测是否已有守护任务
$task = Get-ScheduledTask -TaskName "OpenClaw Guardian" -ErrorAction SilentlyContinue
if ($null -eq $task) {
# 未配置,自动添加
# ... 执行上面的注册命令
Write-Host "✅ 已自动配置 Windows 守护(每 5 分钟检查)"
} else {
Write-Host "✅ 守护已配置"
}
$env:USERPROFILE\.openclaw\logs\guardian.log首次运行自检时自动检测操作系统并配置:
# 复制脚本到 ~/.openclaw/
mkdir -p ~/.openclaw
cp -r /path/to/openclaw-guardian/scripts/guardian-daily.sh ~/.openclaw/
# 添加 cron 任务(路径根据实际安装位置修改)
(crontab -l 2>/dev/null; echo '*/5 * * * * pgrep -f "openclaw-gateway" > /dev/null || cd ~/.openclaw && node ./dist/daemon-cli.js start >> /tmp/gateway_guardian.log 2>&1') | crontab -
# 添加每日报告任务
(crontab -l 2>/dev/null; echo '0 9 * * * ~/.openclaw/guardian-daily.sh >> /tmp/daily_report.txt 2>&1') | crontab -
# 复制脚本
Copy-Item -Path "C:\path\to\openclaw-guardian\scripts\guardian-windows.ps1" -Destination "$env:USERPROFILE\.openclaw\scripts\"
# 手动修改 Gateway 路径后配置定时任务
# (需要用户修改脚本中的 $GatewayPath 变量)
分享给小伙伴:
openclaw-guardian 文件夹发给他~/.openclaw/skills/ 下