On a headless Linux server, why does the OpenClaw Gateway service stop when you log out of SSH, and what fixes it?
answer
- whose session owns the service?
- user units die with the session
- one loginctl setting
- onboarding tries, scripts only log
- doctor's opt-in lint check
basics
~20 sOpenClaw installs its Gateway as a systemd user unit, and without lingering systemd stops a user's services when that user's last session ends. Enable lingering with sudo loginctl enable-linger for the account that owns the service, then confirm with openclaw gateway status.
solid answer
~40 sOn Linux, `openclaw gateway install` and `openclaw onboard --install-daemon` create a systemd **user** unit, `openclaw-gateway.service`, which runs under that user's systemd manager. Without **lingering**, systemd stops the user manager when the user's last session ends, so the Gateway dies when you leave SSH and does not come back after a reboot until someone logs in. The fix is `sudo loginctl enable-linger <user>` for the account that owns the OpenClaw state; check it with `loginctl show-user <user> -p Linger`. Interactive onboarding, `openclaw configure` and the guided `openclaw doctor` offer to enable it, trying without sudo first. A non-interactive onboarding run cannot prompt, so on failure it only logs the command to run. For a multi-user or always-on host, OpenClaw's runbook also allows a system unit with `User=` set to the non-root owner.
code
bash · 4 linesloginctl show-user "$USER" -p Linger # Linger=no explains the symptom
sudo loginctl enable-linger "$USER"
openclaw gateway status
openclaw doctor --lint --all # includes the opt-in linger checkgo deeper
Remember that on Linux the OpenClaw Gateway is a systemd user service, and that sudo loginctl enable-linger keeps it running after you log out.
Explain what lingering changes: the per-user systemd manager stays up without a session and starts at boot, so the openclaw-gateway.service user unit outlives SSH logouts and reboots.
Show where the safety net has holes: non-interactive onboarding only logs the fix, the doctor lint check is opt-in, and a hand-written system unit must name a non-root User= and keep Doctor from adding a rival user unit.
Decide per host whether OpenClaw's managed user unit with lingering or an operator-owned system unit fits your fleet, and make that choice checkable in provisioning rather than discovered after the first silent night.
## The symptom You rent a small Linux server, SSH in, install OpenClaw and install its background service. While your SSH session is open, everything works: the dashboard loads through your tunnel and the chat apps answer. You log out, and within moments the assistant goes silent. After a reboot it stays silent until you SSH in again, at which point it is suddenly back. Nothing is wrong with the config or the unit file. The service is running exactly where OpenClaw put it: inside **your user's** systemd manager. ## Why a user unit stops at logout On Linux and WSL2, OpenClaw's managed service is a **systemd user unit**, `openclaw-gateway.service` for the default profile, rather than a system unit. User units run under a per-user instance of systemd that starts when the user first logs in. By default that instance is torn down when the user's last session ends, taking every user service with it. The OpenClaw source puts it plainly: without lingering, systemd stops the user session on logout or idle and kills the Gateway. **Lingering** is the systemd setting that keeps a user's manager alive with no login session, and starts it at boot. With lingering on, a user unit behaves like an always-on service. ## How OpenClaw handles it OpenClaw knows about this trap and tries to close it, but not on every path: - **Interactive onboarding** with a daemon install tries to enable lingering for the service account, first without sudo and then with a sudo prompt (it writes `/var/lib/systemd/linger`). - **`openclaw configure`** does the same when you configure the service there. - **Non-interactive onboarding** (`openclaw onboard --non-interactive --accept-risk --install-daemon`) cannot prompt. It tries non-interactive sudo; if that fails it only logs `Systemd lingering is disabled for <user>. Run: sudo loginctl enable-linger <user>` and carries on, so a scripted VPS install can finish green with lingering still off. - **Guided `openclaw doctor`** checks a loaded user service on a local Linux install and offers to enable lingering. - **`openclaw doctor --lint`** has a `core/doctor/systemd-linger` check, but it is **opt-in**: it is excluded from the default set, so run `openclaw doctor --lint --all` to include it in a CI or monitoring gate. For the fix itself: 1. Check: `loginctl show-user "$USER" -p Linger`; `Linger=no` explains the symptom. 2. Enable: `sudo loginctl enable-linger "$USER"`, for the account that owns `~/.openclaw`, not root. 3. Verify: log out, wait, and check from a new session with `openclaw gateway status`. On a headless server, `systemctl --user` itself can fail over SSH if `XDG_RUNTIME_DIR` is not set; the runbook suggests `export XDG_RUNTIME_DIR=/run/user/$(id -u)` before retrying. ## User unit with lingering, or a system unit? | | User unit + lingering | System unit with `User=` | |---|---|---| | Installed by | `openclaw gateway install` | you, by hand, under `/etc/systemd/system` | | Survives logout and reboot | yes, once lingering is on | yes | | Managed by OpenClaw's install, update and Doctor | yes | Doctor verifies but you own the lifecycle | | Fits | a single-user host | multi-user or always-on hosts | The runbook calls the user unit with lingering the supported way to run without a login session on a single-user host, and suggests a system unit for multi-user or always-on machines. Two rules apply to the system unit: set `User=` to the **non-root** account that owns the OpenClaw state (a unit without `User=` runs as root, which is unsafe and unsupported), and do not let `openclaw doctor --fix` also install a user unit for the same profile and port. Doctor refuses that automatic install when it finds a system-level OpenClaw service; run it with `OPENCLAW_SERVICE_REPAIR_POLICY=external` when the system unit owns the lifecycle. ## The macOS counterpart macOS has the same shape with a different mechanism. OpenClaw installs a per-user **LaunchAgent**, which runs only in a logged-in user's session; it does not install or manage system LaunchDaemons. An unattended Mac therefore needs that user logged in, or a supervisor you own and declare to Doctor with the same external repair policy.
- A provisioning script ran OpenClaw's non-interactive onboarding with `--install-daemon` and reported success, yet the Gateway dies at logout. What happened?Non-interactive onboarding cannot answer a sudo prompt. It tries to enable lingering with non-interactive sudo, and when that fails it only logs the `sudo loginctl enable-linger <user>` command and continues, so the run still succeeds. Give the script passwordless sudo for that step, run the command yourself, or add `openclaw doctor --lint --all` to the provisioning checks so the warning fails the build.
- Why does OpenClaw's runbook insist on `User=` when you choose a system unit instead?A system unit without `User=` runs the Gateway, and every command its agents execute, as root, which the runbook calls unsafe and unsupported. `User=` also supplies that account's `HOME`, so the Gateway finds its normal `~/.openclaw` state and config. Run Doctor from that same account, never as root with another account's home.
- The unit file is broken, so reinstalling the service will fix logouts
- Run the Gateway as root so it survives without a login session
- systemctl --user enable alone keeps the service running after logout
- Default openclaw doctor --lint always reports disabled lingering
- A successful non-interactive onboarding run proves lingering is on