mirror of
https://git.srhtnin.garden/sarah/SaraSuite.git
synced 2026-10-13 18:18:12 +02:00
41 lines
1.5 KiB
Python
41 lines
1.5 KiB
Python
"""Runs a SaraSuite program as a background service on Windows, the stand-in
|
|
for a systemd --user service. Started windowless with pythonw.exe, both at
|
|
login (by the program's shortcut in the Startup folder) and when SaraSuite
|
|
or the program's own GUI (e.g. SaraSort for SaraSortd) starts it:
|
|
|
|
pythonw.exe ServiceRun.py <Name> <Script> (in the service's working folder)
|
|
|
|
It records its PID in %LOCALAPPDATA%\\SaraSuite\\Services\\<Name>.pid, so
|
|
anything can tell whether the service runs, and stop it (taskkill), without
|
|
a Windows service and without admin rights. Installed next to each program
|
|
that has a Windows service (Programs/<Name>/Manifest.toml, [[Services]]).
|
|
"""
|
|
|
|
import os
|
|
import runpy
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def Main() -> None:
|
|
Name, Script = sys.argv[1], Path(sys.argv[2])
|
|
LocalAppData = Path(os.environ.get("LOCALAPPDATA") or Path.home() / "AppData" / "Local")
|
|
PidFile = LocalAppData / "SaraSuite" / "Services" / f"{Name}.pid"
|
|
PidFile.parent.mkdir(parents=True, exist_ok=True)
|
|
PidFile.write_text(str(os.getpid()))
|
|
try:
|
|
sys.argv = [str(Script)]
|
|
sys.path.insert(0, str(Script.parent))
|
|
runpy.run_path(str(Script), run_name="__main__")
|
|
finally:
|
|
# Only remove it if it's still ours (a newer start may have replaced it).
|
|
try:
|
|
if PidFile.read_text().strip() == str(os.getpid()):
|
|
PidFile.unlink()
|
|
except OSError:
|
|
pass
|
|
|
|
|
|
if __name__ == "__main__":
|
|
Main()
|