fix(gateway): avoid restarting stopped systemd services

This commit is contained in:
chengyongru authored and chengyongru committed 2026-11-10 23:47:23 +08:00
1 parent 62c05e7f7e
commit 0ce335bf12
5 files changed
+16 -10

No files matched your search

+2 -1
View File
@@ -244,12 +244,13 @@ Common operations:
```bash
systemctl --user status nanobot-gateway # check status
systemctl --user stop nanobot-gateway # stop the service
systemctl --user restart nanobot-gateway # restart after config changes
journalctl --user -u nanobot-gateway -f # follow logs
nanobot gateway uninstall-service --manager systemd
```
The installer writes `~/.config/systemd/user/nanobot-gateway.service`, runs `systemctl --user daemon-reload`, enables the unit, and restarts it. It uses the current Python executable with `python -m nanobot gateway --foreground`, so the service runs in the same environment you used to install nanobot.
The installer writes `~/.config/systemd/user/nanobot-gateway.service`, runs `systemctl --user daemon-reload`, enables the unit, and restarts it. It uses the current Python executable with `python -m nanobot gateway --foreground`, so the service runs in the same environment you used to install nanobot. To update an existing unit after upgrading, rerun the install command with the same options; this overwrites the unit and restarts the service. Custom systemd overrides remain in effect.
> **Note:** User services only run while you are logged in. To keep the gateway running after logout, enable lingering:
>
+6 -6
View File
@@ -180,11 +180,11 @@ def create_gateway_app(
):
console.print(
"[yellow]Gateway is already running in the foreground; "
"an attached process cannot be detached in place.[/yellow]"
"it cannot be detached in place.[/yellow]"
)
console.print(
"[dim]Stop it in its current terminal, then run "
"`nanobot gateway --background`.[/dim]"
"[dim]Stop it with its service manager if supervised, or use "
"`nanobot gateway stop`; then run `nanobot gateway --background`.[/dim]"
)
print_status(result.status)
raise typer.Exit(1)
@@ -329,12 +329,12 @@ def create_gateway_app(
raise typer.Exit(1)
if result.message == "gateway_foreground_restart_required":
console.print(
"[yellow]Gateway is attached to a foreground terminal and cannot "
"[yellow]Gateway is running in the foreground and cannot "
"be restarted as a background process.[/yellow]"
)
console.print(
"[dim]Restart it in that terminal, or stop it and run "
"`nanobot gateway --background`.[/dim]"
"[dim]Restart it with its service manager if supervised, or stop it "
"and run `nanobot gateway --background`.[/dim]"
)
print_status(result.status)
raise typer.Exit(1)
+2 -1
View File
@@ -266,7 +266,8 @@ def _systemd_unit_content(
"Type=simple",
f"WorkingDirectory={_systemd_quote(str(working_directory))}",
f"ExecStart={quoted_command}",
"Restart=always",
"Restart=on-failure",
"SuccessExitStatus=SIGTERM",
"RestartSec=10",
"Environment=PYTHONUNBUFFERED=1",
"NoNewPrivileges=yes",
+4 -2
View File
@@ -337,7 +337,8 @@ def test_gateway_background_does_not_claim_a_foreground_gateway(tmp_path):
assert result.exit_code == 1
assert "cannot be" in result.stdout
assert "detached in place" in result.stdout
assert "Stop it in its current terminal" in result.stdout
assert "service manager" in result.stdout
assert "nanobot gateway stop" in result.stdout
def test_gateway_rejects_conflicting_modes(tmp_path):
@@ -522,7 +523,8 @@ def test_gateway_restart_explains_foreground_lifecycle(tmp_path):
result = runner.invoke(app, ["gateway", "restart"])
assert result.exit_code == 1
assert "attached to a foreground terminal" in result.stdout
assert "running in the foreground" in result.stdout
assert "service manager" in result.stdout
def test_gateway_install_service_uses_service_installer(tmp_path):
+2
View File
@@ -38,6 +38,8 @@ def test_systemd_install_dry_run_renders_user_unit(tmp_path):
assert 'WorkingDirectory="/tmp/nanobot workspace"' in result.content
assert 'ExecStart=/venv/bin/python -m nanobot gateway --foreground --port 18790 --verbose' in result.content
assert '--workspace "/tmp/nanobot workspace" --config /tmp/nanobot/config.json' in result.content
assert 'Restart=on-failure' in result.content
assert 'SuccessExitStatus=SIGTERM' in result.content
def test_systemd_install_writes_unit_and_runs_commands(tmp_path):