后台执行
生产环境中,Gateway 通常需要以守护进程(Daemon)模式在后台运行,并在系统启动时自动启动、崩溃后自动重启。
内置 Daemon 模式
bash
# 以守护进程模式启动
openclaw gateway --daemon
# 查看状态
openclaw status
# 停止
openclaw gateway stop开发 vs 生产
开发时使用前台模式(直接 openclaw gateway)方便查看日志。生产环境使用下述进程管理器确保可靠运行。
systemd(Linux 推荐)
创建服务文件
ini
# /etc/systemd/system/openclaw.service
[Unit]
Description=OpenClaw Gateway
Documentation=https://docs.openclaw.dev
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=openclaw
Group=openclaw
WorkingDirectory=/home/openclaw
Environment=OPENCLAW_GATEWAY_TOKEN=your-token-here
Environment=OPENCLAW_GATEWAY_HOST=0.0.0.0
ExecStart=/usr/local/bin/openclaw gateway
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal
# 安全加固
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=read-only
ReadWritePaths=/home/openclaw/.openclaw
[Install]
WantedBy=multi-user.target管理服务
bash
# 重新加载服务配置
sudo systemctl daemon-reload
# 启用开机自启
sudo systemctl enable openclaw
# 启动
sudo systemctl start openclaw
# 查看状态
sudo systemctl status openclaw
# 查看日志
sudo journalctl -u openclaw -f
# 停止
sudo systemctl stop openclaw
# 重启
sudo systemctl restart openclawsystemd 安全
服务文件中的 NoNewPrivileges、ProtectSystem、ProtectHome 提供额外的系统级安全隔离。
launchd(macOS 推荐)
创建 plist 文件
xml
<?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>dev.openclaw.gateway</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/openclaw</string>
<string>gateway</string>
</array>
<key>EnvironmentVariables</key>
<dict>
<key>OPENCLAW_GATEWAY_TOKEN</key>
<string>your-token-here</string>
</dict>
<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>管理服务
bash
# 安装 plist
cp dev.openclaw.gateway.plist ~/Library/LaunchAgents/
# 加载并启动
launchctl load ~/Library/LaunchAgents/dev.openclaw.gateway.plist
# 查看状态
launchctl list | grep openclaw
# 停止并卸载
launchctl unload ~/Library/LaunchAgents/dev.openclaw.gateway.plistpm2(跨平台 Node.js 进程管理器)
安装 pm2
bash
npm install -g pm2配置和启动
bash
# 直接启动
pm2 start openclaw -- gateway
# 或使用配置文件
pm2 start ecosystem.config.jsjavascript
// ecosystem.config.js
module.exports = {
apps: [{
name: 'openclaw-gateway',
script: 'openclaw',
args: 'gateway',
env: {
OPENCLAW_GATEWAY_TOKEN: 'your-token-here',
OPENCLAW_GATEWAY_HOST: '0.0.0.0'
},
restart_delay: 5000,
max_restarts: 10,
autorestart: true
}]
};管理
bash
# 查看状态
pm2 status
# 查看日志
pm2 logs openclaw-gateway
# 停止
pm2 stop openclaw-gateway
# 重启
pm2 restart openclaw-gateway
# 设置开机自启
pm2 startup
pm2 saveDocker 容器
使用 Docker
bash
docker run -d \
--name openclaw-gateway \
-p 18789:18789 \
-e OPENCLAW_GATEWAY_TOKEN=your-token \
-e OPENCLAW_GATEWAY_HOST=0.0.0.0 \
-v openclaw-data:/root/.openclaw \
--restart unless-stopped \
openclaw/gateway:latest使用 Docker Compose
yaml
# docker-compose.yml
version: "3.8"
services:
openclaw:
image: openclaw/gateway:latest
container_name: openclaw-gateway
ports:
- "18789:18789"
environment:
- OPENCLAW_GATEWAY_TOKEN=${GATEWAY_TOKEN}
- OPENCLAW_GATEWAY_HOST=0.0.0.0
volumes:
- openclaw-data:/root/.openclaw
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:18789/health"]
interval: 30s
timeout: 10s
retries: 3
volumes:
openclaw-data:bash
# 启动
docker compose up -d
# 查看日志
docker compose logs -f openclaw
# 停止
docker compose down方案对比
| 方案 | 平台 | 自动重启 | 开机自启 | 日志管理 |
|---|---|---|---|---|
| systemd | Linux | ✅ | ✅ | journald |
| launchd | macOS | ✅ | ✅ | 文件 |
| pm2 | 跨平台 | ✅ | ✅ | 内置 |
| Docker | 跨平台 | ✅ | ✅ | docker logs |
| 内置 daemon | 跨平台 | ❌ | ❌ | 文件 |
选择建议
- Linux 服务器 → systemd
- macOS → launchd
- 跨平台/简单 → pm2
- 容器化部署 → Docker
