#11147: fix(daemon): stop gateway by port when no daemon service is active
cli
stale
Cluster:
Gateway and macOS Improvements
**PR title:** `fix(daemon): stop gateway by port when no daemon service is active`
**Base:** `main` | **Compare:** `fix/gateway-stop-port-fallback`
---
#### Summary
`daemon stop` now detects and kills gateway processes by port when no daemon service (systemd/launchd) is loaded, and also cleans up orphan gateway processes after a daemon stop.
lobster-biscuit
#### Repro Steps
1. Stop the systemd service: `systemctl --user stop openclaw-gateway.service`
2. Start gateway manually: `nohup openclaw gateway run --port 11235 --force &`
3. Run `openclaw daemon stop`
4. Gateway keeps running — `daemon stop` only checked the service, not the port
#### Root Cause
`runDaemonStop()` only used `service.stop()` via systemd/launchd. When the gateway was started outside the daemon (e.g. `nohup`), there was no fallback to detect and stop it. Additionally, `service.isLoaded()` checks whether the unit file is *enabled*, not whether the service is *active* — so even when systemd was stopped, `isLoaded()` returned `true`, and the daemon stop path ran without actually killing the standalone process.
#### Behavior Changes
- When no daemon service is loaded and a gateway process is listening on the configured port, `daemon stop` now sends SIGTERM to that process and waits up to 5s for it to exit.
- After a successful `service.stop()`, orphan gateway processes on the configured port are also cleaned up.
- JSON output includes the stop result in both paths.
#### Tests
- `pnpm build` — pass
- `pnpm test src/cli/daemon-cli.coverage.test.ts` — 9 tests pass (2 new)
- `pnpm test` — 952/961 test files pass; 9 failures are pre-existing (memory/lobster tests, unrelated)
New tests:
- "stops gateway by port when service is not loaded" — verifies SIGTERM is sent to the PID found on the port
- "reports not-loaded when no service and no gateway on port" — verifies original behavior when port is free
#### Manual Testing
##### Prerequisites
- Linux server with systemd and OpenClaw installed
##### Steps
1. Stop systemd service, start gateway via `nohup openclaw gateway run --port 11235 --force &`
2. Run `openclaw daemon stop` — gateway process killed successfully, logs "Stopped gateway process (pid XXXXX) on port 11235."
3. Restore systemd service, verify `openclaw daemon stop` still works normally (no regression)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates the `daemon stop` lifecycle to fall back to stopping a running gateway process by inspecting the configured gateway port when no system service (systemd/launchd) is loaded, and it also attempts a best-effort cleanup of orphan gateway processes after a successful service stop. It adds CLI coverage tests for the new “stop by port” fallback and for the unchanged “not-loaded” behavior when the port is free.
<h3>Confidence Score: 3/5</h3>
- Mostly safe to merge, but stop-status reporting is currently inaccurate and one success message can be incorrect in real environments.
- Core behavior change is contained and tests cover the new fallback path, but `runDaemonStop()` still uses `service.isLoaded()` as a post-stop verification (which doesn’t reflect active state), and the port-based stopper can report success without having a PID to signal when listener details are unavailable.
- src/cli/daemon-cli/lifecycle.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#13084: fix(daemon): multi-layer defense against zombie gateway processes
by openperf · 2026-02-10
84.4%
#22304: Gateway: fix launchd start after stop
by apethree · 2026-02-21
81.6%
#9460: fix(gateway): clean up lock file on service stop
by zenchantlive · 2026-02-05
81.3%
#7155: fix(gateway): use kill SIGTERM instead of bootout for stop
by rafaelreis-r · 2026-02-02
80.7%
#23584: fix(daemon): improve gateway service detection to avoid false posit...
by mohandshamada · 2026-02-22
79.6%
#17835: Fix misleading gateway stop hints for standalone listeners
by ConnorCallison · 2026-02-16
79.6%
#8745: fix(gateway): respect gateway.port config and --port CLI flag
by revenuestack · 2026-02-04
79.3%
#18236: macOS daemon: bootstrap LaunchAgent on gateway start after stop
by agisilaos · 2026-02-16
79.1%
#16845: fix(daemon): gateway auto-restart on SIGTERM + agent restart guidel...
by kiminbean · 2026-02-15
78.7%
#21459: fix(gateway): resolve port from profile config, not inherited env
by kkeeling · 2026-02-19
78.5%