diff --git a/scripts/run_client.py b/scripts/run_client.py index 71bad4d..a33a446 100644 --- a/scripts/run_client.py +++ b/scripts/run_client.py @@ -366,32 +366,53 @@ def install_service_systemd( content = f"[Unit]\nDescription=Medios-Macina Client\nAfter=network.target\n\n[Service]\nType=simple\nExecStart={exec_args}\nWorkingDirectory={str(repo_root)}\nRestart=on-failure\nEnvironment=PYTHONUNBUFFERED=1\n\n[Install]\nWantedBy=default.target\n" unit_file.write_text(content, encoding="utf-8") - subprocess.run([systemctl, "--user", "daemon-reload"], check=True) - subprocess.run( + def _run_systemctl(cmd: List[str]) -> subprocess.CompletedProcess: + try: + return subprocess.run( + cmd, + capture_output=True, + text=True, + check=False, + ) + except Exception as exc: + raise + + failed = False + for cmd in [ + [systemctl, "--user", "daemon-reload"], [systemctl, "--user", "enable", "--now", f"{service_name}.service"], - check=True - ) + ]: + result = _run_systemctl(cmd) + if result.returncode != 0: + stderr = (result.stderr or "").strip() + stdout = (result.stdout or "").strip() + err_lines = "\n".join([l for l in [stderr, stdout] if l]) + print("Failed to run systemctl", cmd, "exit", result.returncode) + if err_lines: + print(err_lines) + if "user scope bus" in err_lines.lower() or "xdg_runtime_dir" in err_lines.lower(): + print("systemd user bus unavailable; falling back to cron @reboot install.") + return install_service_cron( + service_name, + repo_root, + venv_py, + headless=headless, + detached=detached, + pull=pull, + workspace_root=workspace_root, + ) + failed = True + break + + if failed: + return False + print(f"systemd user service '{service_name}' installed and started.") return True - except subprocess.CalledProcessError as e: - err_msg = str(e) - print("Failed to create systemd user service:", err_msg) - if "user scope bus" in err_msg.lower(): - print("systemd user bus unavailable; falling back to cron @reboot install.") - return install_service_cron( - service_name, - repo_root, - venv_py, - headless=headless, - detached=detached, - pull=pull, - workspace_root=workspace_root, - ) - return False except Exception as exc: print("systemd install error:", exc) return False