_systemctl
_systemctl
¶
Shared systemctl --user invocation helpers.
Three flavours, all targeting the user session bus:
-
run— the authoritative variant. RaisesSystemExitwith captured stderr on failure so setup phases that depend on the call succeeding (e.g. enabling a freshly rendered unit) surface the realFailed to connect to bus/Unit X not loadedline rather than the bare exit codesubprocess.CalledProcessErrorprints by default. -
run_best_effort— the idempotent variant. Swallows every error, including a missingsystemctlbinary and asubprocess.TimeoutExpiredon a wedged unit, so cleanup passes (stop, disable, sweep orphans) can't turn a non-failure into a raised exception. -
query— the read-only variant. Returns the capturedsubprocess.CompletedProcessso callers can inspectreturncodeandstdout(is-active,is-system-running, …). A missingsystemctlbinary or a timeout is normalised into a synthetic non-zero result so callers never need their own try/except for the cross-platform "systemd absent" path.
Pick run when the call sits on the critical install path;
run_best_effort when the call is cleanup-shaped; query when
the caller cares about the output, not the success/failure outcome.
run(verb, *args)
¶
Run systemctl --user <verb> <args…>; raise on failure with captured stderr.
Every known failure mode is normalised to SystemExit with a
human-readable message, so the setup aggregator's error row points
the operator at the real cause rather than a raw Python traceback:
CalledProcessError— captured stderr is attached (defaultstr()only includes the exit code).TimeoutExpired— include the timeout value and any captured stdout/stderr soFailed to connect to bussurfaces through even a wedged call.FileNotFoundError— name the missing binary rather than leak a[Errno 2] No such file or directory: 'systemctl'line.
Source code in src/terok_sandbox/_util/_systemctl.py
query(verb, *args, timeout=5.0)
¶
Run systemctl --user <verb> <args…> and return the captured result.
Read-only variant for status probes (is-active,
is-system-running, …) where the caller inspects returncode
and stdout rather than treating any non-zero exit as fatal.
A missing systemctl binary (containerised host, CI) and a
subprocess.TimeoutExpired are normalised into a synthetic
non-zero CompletedProcess (returncodes 127 and
124 respectively, mirroring the shell convention) so callers
never need their own try/except for the cross-platform "systemd
absent" path.
Source code in src/terok_sandbox/_util/_systemctl.py
run_best_effort(verb, *args)
¶
Run systemctl --user <verb> <args…>, swallowing every error path.
Returns silently when systemctl isn't on PATH (containerised
hosts, CI), when the unit doesn't exist, or when the call times
out against a wedged unit. Suitable for stop / disable / reload
passes where the absence of state is the expected shape.