openclaw - 💡(How to fix) Fix CLI 执行命令时会尝试启动新的 Gateway 实例,与 systemd 服务冲突 [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#52851Fetched 2026-04-08 01:18:29
View on GitHub
Comments
1
Participants
2
Timeline
2
Reactions
0
Timeline (top)
commented ×1cross-referenced ×1

Code Example

systemctl --user start openclaw-gateway

---

ps aux | grep openclaw-gateway
   # zw  39705 ... openclaw-gateway  # systemd 启动的,正常

---

openclaw cron list
   # 或
   openclaw gateway --help

---

ps aux | grep -E "openclaw|gateway"
   # zw  39705 ... openclaw-gateway  # systemd 启动的
   # zw  56979 ... openclaw           # CLI 进程
   # zw  56988 ... openclaw-gateway   # CLI 尝试启动的新 Gateway,CPU 飙升 145%

---

[Unit]
Description=OpenClaw Gateway (v2026.3.2)
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/node /home/zw/.npm-global/lib/node_modules/openclaw/dist/index.js gateway --port 18789
Restart=always
RestartSec=5
KillMode=process
Environment=OPENCLAW_GATEWAY_PORT=18789
Environment=OPENCLAW_GATEWAY_TOKEN=xxx
Environment=OPENCLAW_SYSTEMD_UNIT=openclaw-gateway.service
Environment=OPENCLAW_SERVICE_MARKER=openclaw
Environment=OPENCLAW_SERVICE_KIND=gateway

[Install]
WantedBy=default.target
RAW_BUFFERClick to expand / collapse

问题描述

当 systemd 服务运行 Gateway 时,执行任何 openclaw CLI 命令都会尝试启动新的 Gateway 实例,导致多进程冲突。

环境信息

  • OpenClaw 版本: 2026.3.13
  • 操作系统: WSL2 (Linux 6.6.87.2-microsoft-standard-WSL2)
  • Node.js: v22.22.0
  • Gateway 运行方式: systemd 用户服务

复现步骤

  1. 通过 systemd 启动 Gateway 服务:

    systemctl --user start openclaw-gateway
  2. 检查 Gateway 进程:

    ps aux | grep openclaw-gateway
    # zw  39705 ... openclaw-gateway  # systemd 启动的,正常
  3. 执行任何 openclaw CLI 命令:

    openclaw cron list
    # 或
    openclaw gateway --help
  4. 再次检查进程:

    ps aux | grep -E "openclaw|gateway"
    # zw  39705 ... openclaw-gateway  # systemd 启动的
    # zw  56979 ... openclaw           # CLI 进程
    # zw  56988 ... openclaw-gateway   # CLI 尝试启动的新 Gateway,CPU 飙升 145%

预期行为

CLI 应该检测到已有 Gateway 实例在运行,直接连接而不是尝试启动新实例。

实际行为

CLI 启动了新的 Gateway 子进程,由于端口 18789 已被 systemd 服务占用,新进程无法正常工作,CPU 使用率异常升高。

相关问题

  • #52671 (CLI 连接 Gateway 握手超时)

可能的解决方案

  1. CLI 在启动前检测端口是否已被占用
  2. 检测 systemd 服务是否在运行
  3. 使用环境变量 OPENCLAW_SYSTEMD_UNIT 判断是否由 systemd 管理
  4. 如果检测到已运行的 Gateway,直接连接而不是启动新实例

systemd 服务配置

[Unit]
Description=OpenClaw Gateway (v2026.3.2)
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/node /home/zw/.npm-global/lib/node_modules/openclaw/dist/index.js gateway --port 18789
Restart=always
RestartSec=5
KillMode=process
Environment=OPENCLAW_GATEWAY_PORT=18789
Environment=OPENCLAW_GATEWAY_TOKEN=xxx
Environment=OPENCLAW_SYSTEMD_UNIT=openclaw-gateway.service
Environment=OPENCLAW_SERVICE_MARKER=openclaw
Environment=OPENCLAW_SERVICE_KIND=gateway

[Install]
WantedBy=default.target

extent analysis

Fix Plan

To prevent the CLI from starting a new Gateway instance when one is already running via systemd, we need to modify the CLI to detect if a Gateway is already running and connect to it instead of launching a new instance.

Step-by-Step Solution:

  1. Detect Running Gateway:

    • Before executing any command, check if the OPENCLAW_SYSTEMD_UNIT environment variable is set, indicating systemd management.
    • Use the systemctl command to check if the openclaw-gateway service is active.
  2. Connect to Existing Gateway:

    • If the service is active, skip starting a new Gateway instance and directly connect to the existing one.
    • Modify the CLI to handle this logic, potentially using a flag or environment variable to indicate when to connect instead of starting a new instance.
  3. Example Code Snippet:

    const { execSync } = require('child_process');
    const systemctlCommand = 'systemctl --user show -p ActiveState openclaw-gateway';
    
    try {
      const output = execSync(systemctlCommand).toString();
      if (output.includes('ActiveState=active')) {
        console.log('Gateway is already running, connecting to it...');
        // Logic to connect to the existing Gateway instance
      } else {
        console.log('Starting new Gateway instance...');
        // Logic to start a new Gateway instance
      }
    } catch (error) {
      console.error('Error checking Gateway status:', error);
      // Handle error, potentially starting a new instance or exiting
    }
  4. Modify systemd Service:

    • Ensure the OPENCLAW_SYSTEMD_UNIT environment variable is set in the systemd service file as shown in the provided configuration.

Verification

  • Start the Gateway service via systemd: systemctl --user start openclaw-gateway
  • Execute an openclaw CLI command: openclaw cron list
  • Check processes to verify only one Gateway instance is running: ps aux | grep -E "openclaw|gateway"

Extra Tips

  • Consider adding a timeout or retry mechanism when connecting to the existing Gateway instance to handle potential connection issues.
  • Ensure proper logging and error handling in the modified CLI logic to aid in debugging any future issues.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix CLI 执行命令时会尝试启动新的 Gateway 实例,与 systemd 服务冲突 [1 comments, 2 participants]