Skip to content

Latest commit

 

History

History
73 lines (56 loc) · 3.12 KB

multitrack_persistent_launch.md

File metadata and controls

73 lines (56 loc) · 3.12 KB

multitrack_persistent_launch.sh

Revision: 1 Last modified: 2026-12-04T17:00:00Z Authority: constitution §11.4.177 / §11.4.201 / §11.4.147 / §11.4.6 / §11.4.115 Scope: §11.4.18 companion doc for constitution/scripts/multitrack/multitrack_persistent_launch.sh

Overview

A sourced library, not a CLI — one function, mt_persistent_launch, that launches a background command into its own systemd user scope so it survives the teardown of the session or cron job that started it.

nohup <cmd> & disown does not do this. On a systemd host with KillUserProcesses=yes (a common logind default), logind stops the transient session-<N>.scope's whole cgroup when the session closes — signalling every process still resident in it, nohup'd children included, because nohup never moved them out of that cgroup. That is what silently killed a real cron-relaunched supervisor fleet on 11+ consecutive 4-minute cycles before this fix.

Caller contract

. "<this-file>"
mt_persistent_launch <slug> <logfile-or-empty-or-/dev/null> -- <cmd...>
echo "pid=$MT_PERSISTENT_LAUNCH_PID via $MT_PERSISTENT_LAUNCH_MECHANISM"
  • <slug> — short tag folded into the transient unit name (sanitised defensively).
  • <logfile> — appends the launched command's stdout+stderr here, or /dev/null (or empty) to discard.
  • -- — mandatory separator; everything after it is a plain argv list.

Globals set on every success path: MT_PERSISTENT_LAUNCH_PID and MT_PERSISTENT_LAUNCH_MECHANISM (systemd-scope or nohup-fallback).

Source it, never capture it with $( ). The function backgrounds the job in the current shell so $! is the real PID; inside a command substitution the job starts in a throwaway subshell and the caller never sees the pid.

Exit codes

Code Meaning
0 launched, via either mechanism
2 usage error (missing -- or missing command)
1 systemd-run itself failed to start after the availability probe passed (the nohup fallback is still attempted first)

Edge cases and failure modes

  • The fallback is loud, never silent. On a host with no reachable systemd user manager it degrades to nohup + disown and prints an explicit warning that the weaker guarantee is in effect. If you do not see the warning, you got the strong mechanism.
  • Availability is functionally probed, not inferred from command -v — a present-but-unreachable --user bus takes the fallback.
  • Units are uniquely named and --collect, so repeated calls do not leak unit entries.
  • The weak path is a real regression for cron-launched daemons even though exit 0 is returned. Check MT_PERSISTENT_LAUNCH_MECHANISM, not just the status.

Related scripts

  • test_multitrack_persistent_launch_survives_session_teardown.sh — the paired RED/GREEN proof (skips with reason on a host without systemd --user).
  • multitrack_bootstrap.sh — a caller that starts a daemon (step 6).

Last verified

2026-12-04 — the single exported function's signature, the two globals and the three exit codes re-read from the working tree.