Skip to content

Studio: optional systemd user service for Linux auto-start - #9308

Merged
shimmyshimmer merged 16 commits into
unslothai:mainfrom
Souravrajvi0:feat/systemd-service-9258-b25b
Oct 2, 2026
Merged

shimmyshimmer merged 16 commits into
unslothai:mainfrom
Souravrajvi0:feat/systemd-service-9258-b25b

Conversation

@Souravrajvi0

@Souravrajvi0 Souravrajvi0 commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

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=1 is 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 sh
  • studio/systemd/unsloth-studio.service.in + install_user_service.sh (writes ~/.config/systemd/user/unsloth-studio.service, enables and starts it)
  • Binds 127.0.0.1 like unsloth studio; UNSLOTH_SYSTEMD_HOST=0.0.0.0 for LAN, UNSLOTH_SYSTEMD_PORT for the port
  • Boot without a login session needs loginctl enable-linger "$USER" once (printed after install)
  • Uninstall disables and removes the unit only if it carries the installer's marker

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 cwd
  • tests/test_installer_interactive_prompts.py: no new installer question

@danielhanchen

This comment has been minimized.

@Souravrajvi0

This comment has been minimized.

@Souravrajvi0

This comment has been minimized.

@danielhanchen

This comment has been minimized.

@Souravrajvi0
Souravrajvi0 force-pushed the feat/systemd-service-9258-b25b branch from 15afe34 to c7ceb8e Compare August 22, 2026 07:58
@Souravrajvi0

This comment has been minimized.

@danielhanchen

This comment has been minimized.

Souravrajvi0 and others added 6 commits September 14, 2026 07:40
…#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
Souravrajvi0 force-pushed the feat/systemd-service-9258-b25b branch from aa4aebf to 74edf1e Compare September 14, 2026 07:40
@Souravrajvi0

This comment has been minimized.

@shimmyshimmer
shimmyshimmer merged commit fcd16f7 into unslothai:main Oct 2, 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants