Repository navigation
Studio: optional systemd user service for Linux auto-start - #9308
Merged
shimmyshimmer merged 16 commits intoOct 2, 2026
Merged
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Souravrajvi0
force-pushed
the
feat/systemd-service-9258-b25b
branch
from
August 22, 2026 07:58
15afe34 to
c7ceb8e
Compare
This comment has been minimized.
This comment has been minimized.
Souravrajvi0
force-pushed
the
feat/systemd-service-9258-b25b
branch
from
September 4, 2026 18:41
c7ceb8e to
aa4aebf
Compare
Souravrajvi0
requested review from
LeoBorcherding and
oobabooga
as code owners
September 4, 2026 18:41
This comment has been minimized.
This comment has been minimized.
…#9258) Add a managed user-level systemd unit (Restart=on-failure) and an opt-in install.sh prompt on Linux. Includes uninstall cleanup, packaging for the wheel, and regression tests. Fixes unslothai#9258
Match `unsloth studio` so opting into the user service does not quietly expose the server on every interface. Keep all-interfaces bind as an explicit opt-in via UNSLOTH_SYSTEMD_HOST=0.0.0.0 or --host.
Approve the opt-in systemd prompt and scan install_user_service.sh so cross-platform parity CI covers the helper install.sh invokes. Also document the loopback default in the post-install systemd hints.
Probe systemd user availability before the opt-in question so non-systemd Linux installs still reach the usual autostart prompt unchanged. Explicit UNSLOTH_INSTALL_SYSTEMD still attempts setup and reports unavailability. Add regression tests for the guard and autostart ordering.
Souravrajvi0
force-pushed
the
feat/systemd-service-9258-b25b
branch
from
September 14, 2026 07:40
aa4aebf to
74edf1e
Compare
This comment has been minimized.
This comment has been minimized.
…ecStop, never run a cwd helper, escape unit values
This was referenced Oct 2, 2026
…ing values, keep user-written units, sturdier uninstall
…unit without a bus
This was referenced Nov 15, 2026
danielhanchen
pushed a commit
that referenced
this pull request
Nov 16, 2026
…stalls (#17879) Closes #17877 ## Problem On macOS, Studio installed with `install.sh` stops when the terminal closes, so its scheduled tasks stop too. Linux has an opt-in systemd user service (#9308) and the desktop app has "Run at login", but macOS CLI installs had no way to keep Studio running. ## Change Opt-in only: without `UNSLOTH_INSTALL_LAUNCHD=1`, nothing changes. With it, `install.sh` writes and loads `~/Library/LaunchAgents/ai.unsloth.studio.server.plist`, mirroring the systemd service file for file: | systemd (#9308) | launchd (this PR) | |---|---| | `studio/systemd/install_user_service.sh` | `studio/launchd/install_user_agent.sh` | | `unsloth-studio.service.in` | `ai.unsloth.studio.server.plist.in` | | `UNSLOTH_INSTALL_SYSTEMD` / `_HOST` / `_PORT` | `UNSLOTH_INSTALL_LAUNCHD` / `_HOST` / `_PORT` | | `Restart=on-failure` | `KeepAlive` → `SuccessfulExit = false` | | `_remove_systemd_user_service` | `_remove_launchd_user_agent` | Behaviour: - Binds `127.0.0.1:8888` by default. `UNSLOTH_LAUNCHD_HOST=0.0.0.0` opts into LAN. - Restarts only after a crash. `unsloth studio stop` sends SIGTERM, Studio exits 0, and launchd leaves it stopped. - Skipped with a note when the desktop app's `~/Library/LaunchAgents/Unsloth.plist` exists, so two servers don't fight over the port. - Never overwrites a plist it didn't write (marker comment on line 2). - Clear error when there's no GUI session (SSH only, nobody logged in at the Mac). - `scripts/uninstall.sh` boots the agent out **before** the kill sweep, so launchd can't restart Studio mid-uninstall. - launchd's PATH is only `/usr/bin:/bin:/usr/sbin:/sbin`, so the agent sets one that includes the venv, `~/.local/bin` and Homebrew. - stdout/stderr go to `/dev/null`. Studio already tees both streams to `logs/server/` (0600, newest 20 kept). A launchd log file would never rotate, would be world-readable by default, and would contain the startup banner's API key. Startup errors from before that tee starts can be seen by running the command in a terminal; the installer prints that command. - The helper is bash 3.2 compatible (macOS `/bin/bash`). XML escaping uses `sed`, because `${v//&/&}` behaves differently in bash 3.2 and 5.2. Also: packaged via `"launchd/*"` in `pyproject.toml`, added to the cross-platform parity CI path filters and test list, and registered in `tests/test_installer_interactive_prompts.py` so the helper can never add a prompt. ## Testing - `tests/test_installer_launchd.py`: 40 tests using stand-in `launchctl` / `uname` / `id`, so they run on the Linux CI runners as well as macOS (they skip on Windows). Installer and related suites locally: 215 passed, 56 skipped (the systemd tests skip on macOS); the launchd tests also pass with `sh` resolved to dash, as on Ubuntu. - `pre-commit`, `bash -n` / `sh -n`, `plutil -lint` on a generated plist. - On a real Mac (macOS, Apple Silicon), `UNSLOTH_INSTALL_LAUNCHD=1 UNSLOTH_LAUNCHD_PORT=8899 ./install.sh --local --no-torch`: | Check | Result | |---|---| | After install | `state = running`, HTTP 200; ~340 MB RSS, same as a manual run | | `kill -9` | restarted by launchd; new PID owns the port, no orphans | | `unsloth studio stop` | `last exit code = 0`, stays stopped | | `launchctl kickstart` | back up in 3s | | Log out, log back in | Studio started by launchd 7s after Finder, no terminal open | | Uninstall's agent step | unloaded, plist removed, no process left | Known behaviour, same as systemd: if another Studio already holds the port, the agent's copy exits 1 and launchd retries every 10s until the port frees up. Failure paths (second commit, from review): - Uninstall falls back to `launchctl remove <label>` if `bootout` fails, so KeepAlive can't respawn Studio after the kill sweep. Checked against real launchd. - The install helper waits up to 130s for an old agent to unload, past the plist's 120s `ExitTimeOut`. - If `enable` or `bootstrap` fails, the helper removes the new plist, so a failed install can't start a second server at next login. ## Out of scope Windows Task Scheduler and the scheduled-tasks UI redesign from the ideas list. ## Follow-ups folded in - Real-launchd end-to-end test on CI Macs (bootstrap, crash restart, clean exit stays stopped, uninstall unloads); passes on macos-15 and macos-26. - Uninstall unloads from the HOME owner's gui domain and finds the agent under the login account's home. - A redirected-HOME install writes the plist to the login account's LaunchAgents and keeps the redirected HOME for the job. - The installer's Node (e.g. nvm) stays on the agent PATH.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Addresses #9258: an optional Linux systemd user service so Studio starts on boot and restarts after a crash (
Restart=on-failure).Opt-in only. Nothing changes unless
UNSLOTH_INSTALL_SYSTEMD=1is set at install time: no new prompt, no new output, same "Start Unsloth Studio now?" flow.curl -fsSL https://unsloth.ai/install.sh | UNSLOTH_INSTALL_SYSTEMD=1 shstudio/systemd/unsloth-studio.service.in+install_user_service.sh(writes~/.config/systemd/user/unsloth-studio.service, enables and starts it)127.0.0.1likeunsloth studio;UNSLOTH_SYSTEMD_HOST=0.0.0.0for LAN,UNSLOTH_SYSTEMD_PORTfor the portloginctl enable-linger "$USER"once (printed after install)Test plan
tests/test_installer_systemd.py: unit contents, escaping, systemctl calls, flag off leaves the install unchanged, no helper run from the caller's cwdtests/test_installer_interactive_prompts.py: no new installer question