diff --git a/docs/deployment.md b/docs/deployment.md index 03495a8e7..f0eb80811 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -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: > diff --git a/nanobot/cli/gateway.py b/nanobot/cli/gateway.py index 777c916af..3cebf108b 100644 --- a/nanobot/cli/gateway.py +++ b/nanobot/cli/gateway.py @@ -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) diff --git a/nanobot/gateway/service.py b/nanobot/gateway/service.py index 898db8f4d..45b6d8e49 100644 --- a/nanobot/gateway/service.py +++ b/nanobot/gateway/service.py @@ -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", diff --git a/tests/cli/test_gateway_commands.py b/tests/cli/test_gateway_commands.py index 2c02c605d..bd44c0e46 100644 --- a/tests/cli/test_gateway_commands.py +++ b/tests/cli/test_gateway_commands.py @@ -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): diff --git a/tests/gateway/test_gateway_service.py b/tests/gateway/test_gateway_service.py index 076ec4223..0f67b30f6 100644 --- a/tests/gateway/test_gateway_service.py +++ b/tests/gateway/test_gateway_service.py @@ -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):