首页 教程 分类 Skills下载 关于
ZH EN JA KO
安装部署

后台进程管理教程

· 9 分钟

概述

OpenClaw Gateway 需要作为后台服务持续运行。直接在终端运行会在终端关闭时停止。本文介绍多种后台进程管理方案。

使用 OpenClaw 内置命令

OpenClaw 提供了简单的后台启动命令:

# 后台启动
openclaw start --daemon

# 查看运行状态
openclaw status

# 停止服务
openclaw stop

# 重启服务
openclaw restart

使用 PM2 管理

PM2 是 Node.js 生态中最流行的进程管理器,功能丰富且易用。

安装 PM2

npm install -g pm2

启动 OpenClaw

pm2 start openclaw -- start

使用配置文件启动(推荐):

// ecosystem.config.js
module.exports = {
  apps: [{
    name: 'openclaw',
    script: 'openclaw',
    args: 'start',
    instances: 1,
    autorestart: true,
    watch: false,
    max_memory_restart: '500M',
    env: {
      NODE_ENV: 'production'
    },
    error_file: '~/.openclaw/logs/pm2-error.log',
    out_file: '~/.openclaw/logs/pm2-out.log',
    log_date_format: 'YYYY-MM-DD HH:mm:ss'
  }]
};
pm2 start ecosystem.config.js

PM2 常用命令

# 查看进程列表
pm2 list

# 查看日志
pm2 logs openclaw

# 监控资源使用
pm2 monit

# 重启
pm2 restart openclaw

# 重载(零停机)
pm2 reload openclaw

# 停止
pm2 stop openclaw

# 删除进程
pm2 delete openclaw

设置开机自启

pm2 startup
pm2 save

PM2 会自动生成对应系统的开机自启脚本。

使用 systemd 管理

适用于 Linux 系统,更贴近系统层面的管理方式。

创建 Service 文件

sudo nano /etc/systemd/system/openclaw.service
[Unit]
Description=OpenClaw AI Assistant Gateway
After=network.target
Wants=network-online.target

[Service]
Type=simple
User=openclaw
Group=openclaw
WorkingDirectory=/home/openclaw
ExecStart=/usr/local/bin/openclaw start
ExecStop=/usr/local/bin/openclaw stop
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
SyslogIdentifier=openclaw

# 安全设置
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=read-only
ReadWritePaths=/home/openclaw/.openclaw

# 资源限制
MemoryMax=512M
CPUQuota=80%

[Install]
WantedBy=multi-user.target

systemd 常用命令

# 重载 service 文件
sudo systemctl daemon-reload

# 启动
sudo systemctl start openclaw

# 停止
sudo systemctl stop openclaw

# 重启
sudo systemctl restart openclaw

# 查看状态
sudo systemctl status openclaw

# 设置开机自启
sudo systemctl enable openclaw

# 查看日志
journalctl -u openclaw -f
journalctl -u openclaw --since "1 hour ago"

使用 Docker 管理

Docker 自带重启策略,无需额外的进程管理器:

docker run -d \
  --name openclaw \
  --restart unless-stopped \
  -v ~/.openclaw:/root/.openclaw \
  openclaw/openclaw:latest start

Docker Compose:

services:
  openclaw:
    image: openclaw/openclaw:latest
    restart: unless-stopped
    volumes:
      - openclaw-data:/root/.openclaw
    command: start

macOS 使用 launchd

<?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>com.openclaw.gateway</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/openclaw</string>
        <string>start</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/Users/you/.openclaw/logs/launchd-out.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/you/.openclaw/logs/launchd-err.log</string>
</dict>
</plist>
cp com.openclaw.gateway.plist ~/Library/LaunchAgents/
launchctl load ~/Library/LaunchAgents/com.openclaw.gateway.plist

进程监控

无论使用哪种进程管理方式,都建议配置监控:

{
  "gateway": {
    "monitoring": {
      "enabled": true,
      "metrics": {
        "endpoint": "/metrics",
        "format": "prometheus"
      }
    }
  }
}

日志轮转

防止日志文件过大:

# /etc/logrotate.d/openclaw
/home/openclaw/.openclaw/logs/*.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
    copytruncate
}

总结

选择合适的进程管理方案取决于你的环境:Docker 部署优先使用 Docker 自带的重启策略;裸机 Linux 使用 systemd;Node.js 环境使用 PM2;macOS 使用 launchd。核心目标是确保 OpenClaw 崩溃后自动恢复、开机自动启动。

OpenClaw 是开源免费的个人AI助手,支持 WhatsApp、Telegram、Discord 等多平台接入