feat: add --run-as-service for background daemon mode (linux/macos only)#72
feat: add --run-as-service for background daemon mode (linux/macos only)#72mforce wants to merge 1 commit into
Conversation
Adds a `service` module and wires `launcher.main()` to support running
Thoth detached from the terminal so closing the shell does not shut the
app down.
New flags:
--run-as-service POSIX double-fork; writes PID to
$THOTH_DATA_DIR/service.pid and redirects
stdout/stderr to $THOTH_DATA_DIR/service.log.
Forces --server --no-tray --no-open --no-splash.
--service-stop SIGTERM (then SIGKILL after 10s) the daemon.
--service-status Print running/stopped + PID.
--service-restart Stop existing daemon, then start a new one.
--install-systemd-service
Write ~/.config/systemd/user/thoth.service
(Type=simple) for a cleaner long-term setup.
--pid-file / --service-log
Override default paths.
Windows is not supported by --run-as-service; users get a message
pointing them to Task Scheduler / NSSM. The existing bin/thoth wrapper
already forwards extra args, so no installer change is needed.
7c7347d to
89ca085
Compare
siddsachar
left a comment
There was a problem hiding this comment.
Thanks for this. The POSIX daemon approach looks promising: I verified tests/test_service.py in WSL and did a real service.daemonize smoke where the PID file/log/stop path worked.
Before merge, could you please address two platform-safety issues?
-
Windows/non-POSIX service management should be hard-gated before calling PID liveness logic. Locally on Windows,
tests/test_service.py::test_is_alive_for_self_and_invalidraisesKeyboardInterruptbecauseos.kill(pid, 0)is not a safe liveness probe there. Sincelauncher.pyexposes--service-status/--service-stopon all platforms, those commands can currently reach unsafe logic even though daemon mode is Linux/macOS only. -
--install-systemd-serviceshould be Linux-only. Right nowinstall_systemd_unit()rejects Windows but would write~/.config/systemd/user/thoth.serviceon macOS, which is not useful and contradicts the systemd-only behavior.
I can take care of wiring tests/test_service.py into our CI/release focused test commands once those feature-level fixes are in.
Summary
Adds a
--run-as-serviceflag (and friends) tothothso the app can run detached from the terminal — closing the shell no longer shuts it down. Implemented as a smallservicemodule that handles POSIX daemonization, PID-file management, and systemd unit generation.New flags
--run-as-service$THOTH_DATA_DIR/service.pid, redirects stdout/stderr to$THOTH_DATA_DIR/service.log. Forces--server --no-tray --no-open --no-splash.--service-stop--service-status--service-restart--install-systemd-service~/.config/systemd/user/thoth.servicewithType=simple— recommended long-term setup.--pid-file/--service-logNotes
bin/thothalready forwards extra args (exec "$PYTHON" launcher.py "$@"), so no installer change was required.THOTH_DATA_DIR(matches the existing convention inlogging_config.py).Usage
Test plan
tests/test_service.pycover read/write/remove PID,is_alive, status messages, stop on stale PID, stop on a real child process, systemd unit generation, default-path resolution, and CLI routing for--service-status/--service-stop/--install-systemd-service.service.daemonize, verified the PID file is written,is_alivereports true, stdout is captured to the log file, andstop_servicecleanly terminates and removes the PID file.--install-systemd-serviceproduces a working unit when used end-to-end on a system withsystemctl --user.