守护进程设置
为什么需要守护进程
AI Agent 的价值在于”永远在线”。如果 Agent 需要你手动启动才能运行,那它就不是一个真正自主的 Agent。守护进程确保:
- 机器开机后 Agent 自动启动
- 进程异常退出后自动重启
- 日志自动收集和轮转
- 系统资源的合理管理
macOS:使用 launchd
快速设置
OpenClaw 提供一键安装命令:
openclaw daemon install
这会创建并加载一个 launchd 配置文件。
手动配置
创建文件 ~/Library/LaunchAgents/ai.openclaw.gateway.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>ai.openclaw.gateway</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/openclaw</string>
<string>gateway</string>
<string>start</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/tmp/openclaw.stdout.log</string>
<key>StandardErrorPath</key>
<string>/tmp/openclaw.stderr.log</string>
</dict>
</plist>
加载服务:
launchctl load ~/Library/LaunchAgents/ai.openclaw.gateway.plist
管理命令
# 查看服务状态
launchctl list | grep openclaw
# 停止服务
launchctl unload ~/Library/LaunchAgents/ai.openclaw.gateway.plist
# 重启服务
launchctl unload ~/Library/LaunchAgents/ai.openclaw.gateway.plist
launchctl load ~/Library/LaunchAgents/ai.openclaw.gateway.plist
Linux:使用 systemd
快速设置
openclaw daemon install --systemd
手动配置
创建文件 /etc/systemd/system/openclaw.service:
[Unit]
Description=OpenClaw Gateway
After=network.target
[Service]
Type=simple
User=your-username
ExecStart=/usr/local/bin/openclaw gateway start
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
启用并启动服务:
sudo systemctl enable openclaw
sudo systemctl start openclaw
管理命令
# 查看状态
sudo systemctl status openclaw
# 查看日志
journalctl -u openclaw -f
# 重启
sudo systemctl restart openclaw
日志管理
守护进程的日志对于排查问题至关重要。建议:
- 开发阶段设置详细日志级别(debug)
- 生产环境使用普通级别(info)
- 定期检查日志文件大小,避免磁盘占满
健康检查
可以通过 Gateway 的健康检查端点验证服务状态:
curl http://127.0.0.1:18789/health
建议配合监控工具设置告警,在服务异常时及时通知。
下一步
最后更新: 2026-03-10