所有文章
用 systemd 運行 OpenClaw——Linux 常駐服務設定

用 systemd 運行 OpenClaw——Linux 常駐服務設定

|7 分鐘閱讀

用 systemd 運行 OpenClaw——Linux 常駐服務設定

OpenClaw 的 gateway 是一個長時間運行的 Node.js 程序。如果你只是在開發機上測試,手動在終端機啟動完全夠用。但當你想把它部署在伺服器上——24/7 不間斷運行、開機自動啟動、崩潰自動恢復——你就需要一個程序管理工具。

在 Linux 的世界裡,systemd 是事實上的標準。幾乎所有主流發行版都以它作為 init system,它不只是服務管理員,還整合了日誌系統(journald)、排程器(timer)、資源控管(cgroup)等功能。

本文將完整介紹如何用 systemd 管理 OpenClaw gateway 服務,從最基本的 service 檔案,到進階的資源限制與監控設定。


最快的方式:--install-daemon

OpenClaw CLI 提供了一鍵安裝 daemon 的選項:

openclaw onboard --install-daemon

或者如果你已經完成 onboard,可以單獨安裝 daemon:

openclaw daemon install

這條命令會自動:

  • 偵測你的 init system(systemd、launchd 等)
  • 建立對應的 service 檔案
  • 啟用開機自動啟動
  • 立即啟動服務

對大多數使用者來說,這樣就夠了。但如果你想要更精細的控制——自訂資源限制、調整重啟策略、整合監控——那就需要手動建立 service 檔案。


手動建立 systemd service

先決條件

確認你的系統使用 systemd:

systemctl --version

如果有正常輸出(例如 systemd 255),就可以繼續。

確認 OpenClaw 的安裝路徑:

which openclaw
# /usr/bin/openclaw 或 /home/你的帳號/.nvm/versions/node/v22.x.x/bin/openclaw

記下這個路徑,後面會用到。

User service vs System service

systemd 有兩種 service 模式,選擇哪種取決於你的使用情境:

User service System service
設定檔位置 ~/.config/systemd/user/ /etc/systemd/system/
運行身份 你的使用者帳號 root(或指定使用者)
需要 sudo 不需要 需要
開機自啟 需要 enable-linger 預設支援
適合場景 個人開發機、非 root 環境 正式伺服器部署

建議

  • 如果你是用個人帳號在開發機或 VPS 上跑 OpenClaw,用 user service
  • 如果你在管理多個使用者的伺服器上做統一部署,用 system service

方案一:User service

建立目錄和 service 檔案:

mkdir -p ~/.config/systemd/user/
cat > ~/.config/systemd/user/openclaw.service << 'EOF'
[Unit]
Description=OpenClaw Gateway Service
Documentation=https://docs.openclaw.ai
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=/usr/bin/openclaw gateway start
ExecStop=/usr/bin/openclaw gateway stop
Restart=on-failure
RestartSec=10
StartLimitIntervalSec=300
StartLimitBurst=5

# 環境變數
Environment=NODE_ENV=production
Environment=OPENCLAW_LOG_LEVEL=info

# 工作目錄(OpenClaw 設定檔所在位置)
WorkingDirectory=%h

# 標準輸出導向 journald
StandardOutput=journal
StandardError=journal
SyslogIdentifier=openclaw

[Install]
WantedBy=default.target
EOF

注意:如果你的 openclaw 不是裝在 /usr/bin/ 下(例如用 nvm 安裝的),需要改成完整路徑。用 which openclaw 確認。

載入新的 service 設定:

systemctl --user daemon-reload

啟動並設為開機自啟:

systemctl --user enable --now openclaw.service

--now 旗標等於同時執行 enablestart

啟用 linger

User service 有一個重要限制:預設只在使用者登入時運行。如果你 SSH 斷線或登出,service 就會停止[1]

要讓它在沒有登入 session 的情況下也持續運行:

sudo loginctl enable-linger $USER

驗證:

loginctl show-user $USER | grep Linger
# Linger=yes

方案二:System service

如果你需要 system-level 的 service:

sudo cat > /etc/systemd/system/openclaw.service << 'EOF'
[Unit]
Description=OpenClaw Gateway Service
Documentation=https://docs.openclaw.ai
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=openclaw
Group=openclaw
ExecStart=/usr/bin/openclaw gateway start
ExecStop=/usr/bin/openclaw gateway stop
Restart=on-failure
RestartSec=10
StartLimitIntervalSec=300
StartLimitBurst=5

# 環境變數
Environment=NODE_ENV=production
Environment=HOME=/home/openclaw

# 工作目錄
WorkingDirectory=/home/openclaw

# 安全性加固
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=read-only
ReadWritePaths=/home/openclaw/.config/openclaw
ReadWritePaths=/home/openclaw/.local/share/openclaw
PrivateTmp=yes

# 日誌
StandardOutput=journal
StandardError=journal
SyslogIdentifier=openclaw

[Install]
WantedBy=multi-user.target
EOF

建議為 OpenClaw 建立專屬的系統帳號:

sudo useradd --system --create-home --shell /usr/sbin/nologin openclaw

然後用該帳號執行初始化:

sudo -u openclaw openclaw onboard

載入並啟動:

sudo systemctl daemon-reload
sudo systemctl enable --now openclaw.service

Service 檔案詳解

讓我們逐一解析 service 檔案中的關鍵設定。

重啟策略

Restart=on-failure
RestartSec=10
StartLimitIntervalSec=300
StartLimitBurst=5

這段設定的意思是:

  • Restart=on-failure:只有在非正常退出時才重啟。如果你手動 systemctl stop,不會觸發重啟。
  • RestartSec=10:每次重啟前等 10 秒,避免快速循環。
  • StartLimitIntervalSec=300 + StartLimitBurst=5:在 300 秒(5 分鐘)內最多允許 5 次啟動嘗試。超過限制後 service 進入 failed 狀態,需要手動介入。

其他可用的 Restart 值:

行為
no 不自動重啟
on-success 只在正常退出(exit code 0)時重啟
on-failure 只在非正常退出時重啟(推薦)
on-abnormal 在 signal 或 timeout 退出時重啟
on-abort 只在 signal 退出時重啟
always 無論如何都重啟

安全性加固(System service)

NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=read-only
ReadWritePaths=/home/openclaw/.config/openclaw
ReadWritePaths=/home/openclaw/.local/share/openclaw
PrivateTmp=yes

這些是 systemd 提供的沙盒功能[2]

  • NoNewPrivileges:禁止程序透過 setuid 等方式提權
  • ProtectSystem=strict:整個檔案系統變成唯讀
  • ReadWritePaths:明確指定允許寫入的目錄
  • ProtectHome=read-only:其他使用者的 home 目錄唯讀
  • PrivateTmp:使用獨立的 /tmp,防止透過暫存檔攻擊

用 journalctl 查看日誌

systemd 整合了 journald 日誌系統,這是查看 OpenClaw 服務日誌最方便的方式。

基本查看

# User service
journalctl --user -u openclaw.service

# System service
journalctl -u openclaw.service

常用參數

# 即時追蹤(類似 tail -f)
journalctl --user -u openclaw.service -f

# 只看最近 100 行
journalctl --user -u openclaw.service -n 100

# 只看今天的日誌
journalctl --user -u openclaw.service --since today

# 只看錯誤等級以上
journalctl --user -u openclaw.service -p err

# 指定時間範圍
journalctl --user -u openclaw.service \
  --since "2026-02-24 08:00" \
  --until "2026-02-24 12:00"

# JSON 格式輸出(方便程式處理)
journalctl --user -u openclaw.service -o json-pretty

日誌容量管理

journald 的日誌預設會持續增長。你可以設定上限:

sudo mkdir -p /etc/systemd/journald.conf.d/
sudo cat > /etc/systemd/journald.conf.d/size.conf << 'EOF'
[Journal]
SystemMaxUse=500M
MaxFileSec=1month
EOF

sudo systemctl restart systemd-journald

資源限制

如果你在共用伺服器上運行,可能需要限制 OpenClaw 使用的資源。

記憶體限制

[Service]
MemoryMax=2G
MemoryHigh=1.5G
  • MemoryMax:硬上限,超過會被 OOM killer 終止
  • MemoryHigh:軟上限,超過後系統會嘗試回收記憶體

CPU 限制

[Service]
CPUQuota=200%

200% 表示最多使用 2 個 CPU 核心。在多核伺服器上,這個值可以按需調整。

檔案描述符限制

[Service]
LimitNOFILE=65536

OpenClaw 在處理多個並發 Agent 對話時可能需要較多的 file descriptors。

完整的帶資源限制的 service 範例

[Service]
Type=simple
ExecStart=/usr/bin/openclaw gateway start
Restart=on-failure
RestartSec=10

MemoryMax=2G
MemoryHigh=1.5G
CPUQuota=200%
LimitNOFILE=65536
IOWeight=50

IOWeight=50 讓 OpenClaw 的磁碟 I/O 優先權低於其他服務(預設 100),適合不希望 AI 推理影響其他服務的場景。


監控

基本健康檢查

用 systemd 的 ExecStartPost 做啟動後驗證:

[Service]
ExecStart=/usr/bin/openclaw gateway start
ExecStartPost=/bin/sh -c 'sleep 5 && curl -sf http://localhost:19080/health || exit 1'

如果 health check 失敗,service 會被視為啟動失敗,觸發重啟。

搭配 systemd timer 做定期檢查

建立一個健康檢查腳本:

cat > ~/openclaw-healthcheck.sh << 'SCRIPT'
#!/bin/bash
response=$(curl -sf -o /dev/null -w "%{http_code}" http://localhost:19080/health 2>/dev/null)

if [ "$response" != "200" ]; then
    echo "OpenClaw health check failed (HTTP $response), restarting..."
    systemctl --user restart openclaw.service
fi
SCRIPT

chmod +x ~/openclaw-healthcheck.sh

建立 timer:

cat > ~/.config/systemd/user/openclaw-healthcheck.timer << 'EOF'
[Unit]
Description=OpenClaw Health Check Timer

[Timer]
OnBootSec=2min
OnUnitActiveSec=5min
AccuracySec=30s

[Install]
WantedBy=timers.target
EOF

cat > ~/.config/systemd/user/openclaw-healthcheck.service << 'EOF'
[Unit]
Description=OpenClaw Health Check

[Service]
Type=oneshot
ExecStart=%h/openclaw-healthcheck.sh
EOF
systemctl --user daemon-reload
systemctl --user enable --now openclaw-healthcheck.timer

這會每 5 分鐘檢查一次 OpenClaw 是否正常運作。

用 systemd-notify 做 watchdog

更進階的做法是利用 systemd 的 watchdog 機制[3]。如果程序在指定時間內沒有發送 keepalive 信號,systemd 會自動重啟它:

[Service]
Type=notify
WatchdogSec=60

這需要 OpenClaw 本身支援 sd_notify 協定。目前 OpenClaw 尚未內建此功能,但你可以用 wrapper script 實現:

cat > ~/openclaw-watchdog.sh << 'SCRIPT'
#!/bin/bash
openclaw gateway start &
PID=$!

while kill -0 $PID 2>/dev/null; do
    if curl -sf http://localhost:19080/health > /dev/null 2>&1; then
        systemd-notify WATCHDOG=1
    fi
    sleep 30
done
SCRIPT

故障排除

Service 啟動失敗

# 查看詳細狀態
systemctl --user status openclaw.service

# 查看最近的日誌
journalctl --user -u openclaw.service -n 50 --no-pager

常見原因:

1. ExecStart 路徑錯誤

openclaw.service: Failed to execute /usr/local/bin/openclaw: No such file or directory

which openclaw 確認正確路徑,更新 service 檔案後執行 daemon-reload

2. 權限不足

Error: EACCES: permission denied

確認 service 的 User 有權限存取 OpenClaw 的設定目錄。

3. Port 被佔用

Error: listen EADDRINUSE :::19080
# 找出佔用 port 的程序
ss -tlnp | grep 19080
# 或
lsof -i :19080

Service 反覆重啟

如果你看到 start-limit-hit

# 檢視啟動次數限制的狀態
systemctl --user show openclaw.service | grep -E "Start|Result"

如果確認問題已修復,重置失敗計數器:

systemctl --user reset-failed openclaw.service
systemctl --user start openclaw.service

Linger 相關問題

# 確認 linger 是否啟用
ls /var/lib/systemd/linger/
# 應該有你的使用者名稱

# 如果 service 在 SSH 斷線後停止
loginctl show-user $USER
# 確認 Linger=yes

與 PM2 和 Docker 的比較

開發者常問:為什麼不用 PM2 或 Docker 來管理 OpenClaw?來比較一下。

PM2

PM2 是 Node.js 生態圈最流行的程序管理工具。

pm2 start "openclaw gateway start" --name openclaw
pm2 save
pm2 startup

PM2 的優點

  • 對 Node.js 開發者更熟悉
  • 內建 cluster mode(但 OpenClaw gateway 不需要)
  • 內建簡易的 CPU/記憶體監控

PM2 的缺點

  • 多一層抽象——PM2 本身也需要被管理(最終還是靠 systemd)
  • 日誌管理不如 journald 整合
  • 資源限制功能有限

Docker

docker run -d --restart=unless-stopped \
  --name openclaw \
  -p 19080:19080 \
  openclaw/openclaw-stack:latest

Docker 的優點

  • 環境完全隔離
  • 可重現的部署
  • 容易遷移

Docker 的缺點

  • 多一層虛擬化的開銷
  • 網路設定較複雜
  • 除錯時需要 docker exec 進入容器

建議

場景 推薦方案
Linux 伺服器直接部署 systemd
已經熟悉 PM2 的團隊 PM2 + systemd
需要環境隔離 Docker
不想管伺服器 ClawTank

systemd 的最大優勢在於它是作業系統的一部分,不需要額外安裝任何東西。它的日誌系統、資源控管、依賴管理都與 OS 深度整合,是最「原生」的做法。


進階:多實例部署

如果你需要在同一台機器上跑多個 OpenClaw 實例(例如不同的 Agent 設定),可以用 systemd template unit:

cat > ~/.config/systemd/user/openclaw@.service << 'EOF'
[Unit]
Description=OpenClaw Gateway - %i
After=network-online.target

[Service]
Type=simple
ExecStart=/usr/bin/openclaw gateway start --config %h/.config/openclaw/%i/config.json
Restart=on-failure
RestartSec=10
Environment=NODE_ENV=production

[Install]
WantedBy=default.target
EOF

使用方式:

# 啟動名為 "research" 的實例
systemctl --user start openclaw@research.service

# 啟動名為 "coding" 的實例
systemctl --user start openclaw@coding.service

# 查看所有 OpenClaw 實例
systemctl --user list-units 'openclaw@*'

總結

systemd 是在 Linux 上運行 OpenClaw 的最佳選擇。它提供了:

  • 開機自動啟動
  • 崩潰自動恢復
  • 完整的日誌管理
  • 精細的資源控制
  • 安全性沙盒

核心設定只需要一個 service 檔案和兩條命令:daemon-reloadenable --now。進階功能(timer、watchdog、template unit)則可以按需啟用。

如果你不想自己管理 Linux 伺服器和 systemd 設定,ClawTank 提供完全託管的 OpenClaw 環境,所有基礎設施維護都替你處理好了。


References

  1. systemd user services and lingering
  2. systemd sandboxing features
  3. systemd watchdog support
  4. journalctl manual
  5. systemd template units
  6. OpenClaw documentation - Deployment
  7. PM2 documentation
  8. systemd resource control

準備好部署 OpenClaw 了嗎?

不需要 Docker、SSH、DevOps。不到 1 分鐘即可部署。

免費開始使用