← 返回
未分类 中文

OpenClaw Gateway Linux Fix

Fix and diagnose OpenClaw Gateway service issues on Linux. Use when the gateway service shows "disabled" status despite running, when `openclaw gateway statu...
修复并诊断Linux上的OpenClaw网关服务问题。适用于网关服务显示“已禁用”但仍在运行,或执行`openclaw gateway status`时出现异常的情况。
maksmirnnov maksmirnnov 来源
未分类 clawhub v1.1.1 1 版本 99763.6 Key: 无需
★ 1
Stars
📥 402
下载
💾 0
安装
1
版本
#latest

概述

OpenClaw Gateway — Linux Fixes

Issue 1: Gateway shows "disabled" despite running

Symptom: openclaw status or openclaw gateway status shows disabled, but the service is actually running.

Root cause: The gateway process spawns systemctl --user is-enabled without XDG_RUNTIME_DIR and DBUS_SESSION_BUS_ADDRESS in its environment. Without these, systemd user bus is unreachable → "Failed to connect to bus: No medium found".

⚠️ Common wrong fix: Adding these vars to ~/.bashrc or shell environment does NOT help — the gateway daemon doesn't inherit your shell env.

Correct fix: Add the vars directly to the systemd unit file:

RUNTIME_DIR="/run/user/$(id -u)"
UNIT=~/.config/systemd/user/openclaw-gateway.service

# Append env vars after [Service] line (idempotent check first)
grep -q "XDG_RUNTIME_DIR" "$UNIT" || UNIT="$UNIT" RUNTIME="$RUNTIME_DIR" python3 -c "
import re, os
unit, runtime = os.environ['UNIT'], os.environ['RUNTIME']
content = open(unit).read()
insert = '\nEnvironment=XDG_RUNTIME_DIR=' + runtime + '\nEnvironment=DBUS_SESSION_BUS_ADDRESS=unix:path=' + runtime + '/bus'
content = re.sub(r'(\[Service\])', r'\1' + insert, content, count=1)
open(unit, 'w').write(content)
print('Unit file updated.')
"

# Reload and restart safely (see Issue 2 for post-restart status check)
systemctl --user daemon-reload
nohup bash -c 'sleep 2 && systemctl --user restart openclaw-gateway' > /tmp/gw-restart.log 2>&1 &

Expected result: Service: systemd (enabled)


Issue 2: Safe gateway restart

Problem: openclaw gateway restart and systemctl --user restart openclaw-gateway send SIGTERM to the gateway, which also kills the entire process tree — including the agent turn that triggered the restart. Any work scheduled after the restart command in the same process will never execute.

Root cause: The agent runs as a child process of the gateway. SIGTERM propagates down the tree on shutdown.

Step 1: Schedule post-restart work via cron

Since the agent dies with the gateway, any follow-up work must be pre-scheduled before the restart using openclaw cron add --at.

The cron scheduler runs inside the gateway and fires independently once the gateway comes back up (~5–7 seconds). Schedule it ~15 seconds ahead to be safe.

AT=$(date -u +"%Y-%m-%dT%H:%M:%S+00:00" --date="+15 seconds") && \
openclaw cron add \
  --name "gw-restart-notify" \
  --at "$AT" \
  --message "Gateway was restarted. Check status: systemctl --user status openclaw-gateway --no-pager | head -4 and report back." \
  --announce \
  --to <TELEGRAM_CHAT_ID> \
  --channel telegram \
  --delete-after-run \
  --session isolated

Step 2: Trigger the restart

nohup bash -c 'sleep 2 && systemctl --user restart openclaw-gateway' > /tmp/gw-restart.log 2>&1 &

sleep 2 + & detaches the restart from the current process tree before the gateway shuts down.

⚠️ Do NOT chain status checks after this command (e.g. && sleep 5 && systemctl status) — they will be killed too.

Passing context across the restart

If the agent needs to continue a task after restart, save context to a file before restarting and reference it in the cron message:

echo "Was doing X, next step is Y, params: Z" > /tmp/restart-context.txt

AT=$(date -u +"%Y-%m-%dT%H:%M:%S+00:00" --date="+15 seconds") && \
openclaw cron add \
  --name "gw-restart-continue" \
  --at "$AT" \
  --message "Continue the task. Context is in /tmp/restart-context.txt — read it and proceed." \
  --announce \
  --to <TELEGRAM_CHAT_ID> \
  --channel telegram \
  --delete-after-run \
  --session isolated

The isolated agent spawned by cron will read the file and continue from where the previous agent left off.

What does NOT work

  • sleep N && systemctl status chained after restart — killed by SIGTERM
  • setsid / systemd-run for post-restart notification — process survives but cannot reach Telegram (direct API access blocked in some regions; gateway is the only working path)
  • curl directly to Telegram API — may time out if blocked at network level

Issue 3: openclaw gateway status shows "disabled" in SSH session

This is a separate issue from Issue 1 — the gateway itself works fine, but your shell session lacks XDG_RUNTIME_DIR.

Affected: sudo su (without -), non-login shells, cron, sudo openclaw.

Fix: Add to ~/.bashrc and /etc/profile.d/openclaw-env.sh:

export XDG_RUNTIME_DIR=/run/user/$(id -u)
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus

Shell escalation cheatsheet:

CommandResultWhy
---------
sudo su -✅ worksFull login shell, reads .bashrc
sudo -i✅ worksLogin shell (if vars in .bashrc)
sudo su❌ failsNon-login shell, env not loaded
sudo openclaw❌ failsClean env, vars stripped by sudo

Issue 4: Service not persisting after reboot

OpenClaw runs as a user-scope systemd service (~/.config/systemd/user/), not system-scope. Without linger, user services stop when the last session closes.

loginctl enable-linger $(whoami)   # persist after logout
systemctl --user enable openclaw-gateway  # auto-start on boot

See references/diagnosis.md for a full diagnostic checklist.

版本历史

共 1 个版本

  • v1.1.1 当前
    2026-05-03 09:34 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

it-ops-security

1password

steipete
设置和使用 1Password CLI (op)。适用于:安装 CLI、启用桌面应用集成、登录(单/多账户)、通过 op 读取/注入/运行密钥。
★ 53 📥 31,506
it-ops-security

Tmux

steipete
通过发送按键和抓取窗格输出,远程控制交互式 CLI 的 tmux 会话。
★ 45 📥 29,424
it-ops-security

MoltGuard - Security & Antivirus & Guardrails

thomaslwang
MoltGuard — OpenClaw 安全守卫,由 OpenGuardrails 提供。安装 MoltGuard,保护您和您的用户免受提示注入、数据泄露和恶意攻击。
★ 116 📥 30,842