macOS Launchd Supervision
Author launchd jobs that actually stay supervised, and build the external check that notices when they don't.
Use This For
- Writing a LaunchAgent or LaunchDaemon plist for a long-lived local daemon (Label, ProgramArguments, RunAtLoad, KeepAlive, ThrottleInterval, log paths).
- Deciding LaunchAgent (
~/Library/LaunchAgents, per-user GUI session) vs LaunchDaemon (/Library/LaunchDaemons, system-wide, no user session).
- Debugging "the daemon silently died and nothing restarted it" — almost always a supervisor that got unloaded, not a KeepAlive bug.
- Designing an EXTERNAL supervision-integrity check, modeled on
pd doctor, that verifies the supervisor is loaded — something KeepAlive cannot verify about itself.
- Fixing launchd's minimal-PATH environment causing
command not found for tools that work fine in a terminal.
Do Not Use This For
- systemd unit files, Windows service registration, or any non-Darwin service manager — different lifecycle model entirely.
- Docker/container healthchecks and restart policies.
- The daemon's own business logic, protocol, or graceful-shutdown handling — see
daemon-development.
Process
flowchart TD
A[Define job: Label + ProgramArguments] --> B{GUI session needed?}
B -->|per-user, login items, Aqua| C[LaunchAgent: ~/Library/LaunchAgents/label.plist]
B -->|system-wide, boots before login| D[LaunchDaemon: /Library/LaunchDaemons/label.plist]
C --> E[RunAtLoad true + KeepAlive tuned + ThrottleInterval >= 10s]
D --> E
E --> F[StandardOutPath/StandardErrorPath to a durable log dir, never /tmp]
F --> G["Pin PATH: absolute ProgramArguments[0] or EnvironmentVariables.PATH"]
G --> H[launchctl bootstrap gui/UID or system label.plist]
H --> I{External supervision-integrity check}
I -->|supervisor loaded, daemon reachable| J[ok]
I -->|supervisor loaded, daemon unreachable| K[warn: kickstart the job]
I -->|supervisor label absent: brew upgrade, logout, bootout| L[critical: nothing will resurrect it]
L --> M[Re-bootstrap the supervisor, then verify again]
- Decide Agent vs Daemon first: does the job need a logged-in GUI session (Aqua, notifications, keychain UI) or must it run before/without any user session? That answer decides the install location, not preference.
- Write the plist: unique reverse-DNS
Label, absolute-path ProgramArguments, RunAtLoad=true so it survives reboot/login, KeepAlive shaped to intent (bare true, or {SuccessfulExit:false, Crashed:true} to avoid restart-looping a clean exit), ThrottleInterval >= 10 to stop thrash.
- Route
StandardOutPath/StandardErrorPath to a durable directory — ~/Library/Logs/<app>/ for an Agent, /Library/Logs/<app>/ or /var/log/<app>/ for a Daemon. Never /tmp or /private/tmp.
- Pin the runtime environment: launchd jobs start with
PATH=/usr/bin:/bin:/usr/sbin:/sbin and no shell profile. Use absolute paths in ProgramArguments or set EnvironmentVariables.PATH explicitly.
- Lint the plan with
scripts/plist_lint.mjs before you ever run launchctl bootstrap.
- Load it with the modern
launchctl bootstrap gui/$(id -u) <plist> (Agent) or launchctl bootstrap system <plist> (Daemon) — not the deprecated load/unload.
- Add an EXTERNAL supervision-integrity check, separate from both the daemon and its KeepAlive stanza, that runs
launchctl list <label> for every legitimate supervisor label on a schedule or on doctor/health-check invocation, and classifies severity (ok / warn / critical) — because KeepAlive only acts while the job is loaded, and it cannot detect its own absence.
Output Contract
scripts/plist_lint.mjs returns:
pass — boolean, true only when zero critical findings remain.
findings — array of { severity: "critical"|"warning", code, message }, one per detected defect.
recommendations — array of concrete fix strings, one per finding, in the same order.
Use scripts/plist_lint.mjs to lint a launchd job plan (JSON matching schemas/launchd-plan.schema.json) and get a deterministic pass/fail with actionable findings before you ship a plist.
Anti-Patterns
KeepAlive Means It'll Always Restart
Novice: Sets KeepAlive: true and treats the daemon as permanently self-healing — "launchd's got it."
Expert: KeepAlive only supervises while the job is loaded. A brew upgrade, a user logout, a launchctl bootout, or a corrupted plist unloads the job entirely, and nothing resurrects the daemon until something re-bootstraps the supervisor itself. Port Daddy hit this for real: Homebrew's KeepAlive on homebrew.mxcl.port-daddy supervised the daemon perfectly — until brew upgrade churn unloaded the job, and the daemon died silently with zero supervisors watching. The fix (PR #607) was pd doctor supervision-integrity: an external, 3-tier severity check (ok / warn / critical) that queries launchctl list <label> for every legitimate supervisor label, independent of the daemon's own health.
Detection: A plist with KeepAlive set and no separate process, cron, or doctor command that periodically confirms the supervisor label itself is still loaded — grep for KeepAlive with no companion launchctl list call anywhere in the codebase.
Logging To /tmp
Novice: StandardOutPath and StandardErrorPath point at /tmp/mydaemon.log "because logs are temporary anyway."
Expert: macOS purges /tmp and /private/tmp on a schedule and on every reboot. The one log that would explain a boot-time crash is gone before anyone reads it — especially cruel because boot-time failures are exactly when you need durable logs. Use ~/Library/Logs/<app>/ for a LaunchAgent or /Library/Logs/<app>/ / /var/log/<app>/ for a LaunchDaemon.
Detection: grep -E 'StandardOutPath|StandardErrorPath' *.plist shows a value starting with /tmp or /private/tmp.
Trusting Shell PATH Inside A launchd Job
Novice: ProgramArguments: ["node", "server.js"], assuming node resolves the way it does in Terminal.
Expert: launchd jobs start with a minimal environment (PATH=/usr/bin:/bin:/usr/sbin:/sbin) and never source .zshrc/.bash_profile. Homebrew-installed binaries, nvm shims, and anything on a custom PATH all 404 as "command not found" even though which node works fine interactively. Use an absolute path (/opt/homebrew/bin/node) as ProgramArguments[0], or set EnvironmentVariables.PATH explicitly in the plist.
Detection: ProgramArguments[0] is a bare command name (no leading /) and the plist has no EnvironmentVariables.PATH key.
References
| File |
Load When |
references/plist-field-reference.md |
Need the exact meaning and failure semantics of each plist key before writing one. |
references/agent-vs-daemon-and-integrity.md |
Need to decide LaunchAgent vs LaunchDaemon, or design a supervision-integrity check. |
examples/expected-output.md |
Need a worked example: a real annotated plist plus its lint report. |
templates/output-template.md |
Need a reusable skeleton for a plist-plus-lint deliverable. |
schemas/launchd-plan.schema.json |
Need to validate a launchd job plan before running the linter. |
scripts/plist_lint.mjs |
Need deterministic linting of a launchd job plan. |
agents/openai.yaml |
Need a subagent descriptor for delegated launchd plist authoring/review. |
Skill Bundle Index
Every file in this skill, and when to open it. Auto-generated; run scripts/index_references.py --fix.
root
CHANGELOG.md — macOS Launchd Supervision — Changelog — - Initial skill creation - Core process defined - Reference files and deterministic plist_lint script added
README.md — macOS Launchd Supervision — Author correct launchd plists for long-lived local daemons, and design the external supervision-integrity check that catches what `KeepAlive
agents/
agents/openai.yaml — openai (data/schema)
examples/
examples/expected-output.md — Example Output: macOS Launchd Supervision — Scenario: Port Daddy's own daemon (pd start --foreground) needs a LaunchAgent that survives brew upgrade churn, logs durably, and is che
examples/sample-input.json — sample input (data/schema)
references/
references/agent-vs-daemon-and-integrity.md — LaunchAgent vs LaunchDaemon, and the Supervision-Integrity Pattern — Use this when deciding where a job lives, or when designing a check that verifies the supervisor itself.
references/plist-field-reference.md — Plist Field Reference — Use this when writing or reviewing the actual .plist keys, not just the high-level plan.
schemas/
schemas/launchd-plan.schema.json — launchd plan.schema (data/schema)
scripts/
templates/
templates/output-template.md — Launchd Supervision Plan — - Label: [reverse-DNS label] - Placement: [agent | daemon] — because [GUI-session need / boot-time need] Run: Paste the lint report an
1---2name: macos-launchd-supervision3description: >-4 Author correct macOS launchd plists for long-lived local daemons and design the EXTERNAL5 supervision-integrity check that catches what KeepAlive cannot self-heal: the supervising job6 itself getting unloaded. Use when writing or reviewing a LaunchAgent/LaunchDaemon plist,7 debugging a daemon that "silently died" after a brew upgrade or logout, choosing LaunchAgent vs8 LaunchDaemon, or fixing launchd's minimal-PATH "command not found" failures. NOT for9 systemd/Linux unit files, Windows services, Docker healthchecks, or the daemon's own business10 logic (see daemon-development).11license: Apache-2.012allowed-tools: Read,Write,Edit,Bash,Grep,Glob13metadata:14 source: https://skillmd.com/skills/curiositech/macos-launchd-supervision15 category: Infrastructure & DevOps16 tags:17 - launchd18 - macos19 - plist20 - daemon-supervision21 - keepalive22 provenance:23 kind: first-party24 owners:25 - port-daddy26 pairs-with:27 - skill: daemon-development28 reason: Supplies the daemon's own lifecycle, graceful shutdown, and IPC once launchd supervises it correctly.29 - skill: macos-host-security30 reason: Supplies entitlements, code signing, and sandboxing concerns adjacent to a launchd job.31 - skill: port-daddy-internal-dev32 reason: Home of the real `pd doctor supervision-integrity` precedent (PR #607) this skill generalizes.33 io-contract:34 kind: deliverable35 consumes:36 - kind: launchd-job-plan37 format: json38 produces:39 - kind: plist-lint-report40 format: json41 - kind: annotated-plist42 format: markdown43---4445# macOS Launchd Supervision4647Author launchd jobs that actually stay supervised, and build the external check that notices when they don't.4849## Use This For5051- Writing a LaunchAgent or LaunchDaemon plist for a long-lived local daemon (Label, ProgramArguments, RunAtLoad, KeepAlive, ThrottleInterval, log paths).52- Deciding LaunchAgent (`~/Library/LaunchAgents`, per-user GUI session) vs LaunchDaemon (`/Library/LaunchDaemons`, system-wide, no user session).53- Debugging "the daemon silently died and nothing restarted it" — almost always a supervisor that got unloaded, not a KeepAlive bug.54- Designing an EXTERNAL supervision-integrity check, modeled on `pd doctor`, that verifies the *supervisor* is loaded — something KeepAlive cannot verify about itself.55- Fixing launchd's minimal-PATH environment causing `command not found` for tools that work fine in a terminal.5657## Do Not Use This For5859- systemd unit files, Windows service registration, or any non-Darwin service manager — different lifecycle model entirely.60- Docker/container healthchecks and restart policies.61- The daemon's own business logic, protocol, or graceful-shutdown handling — see `daemon-development`.6263## Process6465```mermaid66flowchart TD67 A[Define job: Label + ProgramArguments] --> B{GUI session needed?}68 B -->|per-user, login items, Aqua| C[LaunchAgent: ~/Library/LaunchAgents/label.plist]69 B -->|system-wide, boots before login| D[LaunchDaemon: /Library/LaunchDaemons/label.plist]70 C --> E[RunAtLoad true + KeepAlive tuned + ThrottleInterval >= 10s]71 D --> E72 E --> F[StandardOutPath/StandardErrorPath to a durable log dir, never /tmp]73 F --> G["Pin PATH: absolute ProgramArguments[0] or EnvironmentVariables.PATH"]74 G --> H[launchctl bootstrap gui/UID or system label.plist]75 H --> I{External supervision-integrity check}76 I -->|supervisor loaded, daemon reachable| J[ok]77 I -->|supervisor loaded, daemon unreachable| K[warn: kickstart the job]78 I -->|supervisor label absent: brew upgrade, logout, bootout| L[critical: nothing will resurrect it]79 L --> M[Re-bootstrap the supervisor, then verify again]80```81821. Decide Agent vs Daemon first: does the job need a logged-in GUI session (Aqua, notifications, keychain UI) or must it run before/without any user session? That answer decides the install location, not preference.832. Write the plist: unique reverse-DNS `Label`, absolute-path `ProgramArguments`, `RunAtLoad=true` so it survives reboot/login, `KeepAlive` shaped to intent (bare `true`, or `{SuccessfulExit:false, Crashed:true}` to avoid restart-looping a clean exit), `ThrottleInterval >= 10` to stop thrash.843. Route `StandardOutPath`/`StandardErrorPath` to a durable directory — `~/Library/Logs/<app>/` for an Agent, `/Library/Logs/<app>/` or `/var/log/<app>/` for a Daemon. Never `/tmp` or `/private/tmp`.854. Pin the runtime environment: launchd jobs start with `PATH=/usr/bin:/bin:/usr/sbin:/sbin` and no shell profile. Use absolute paths in `ProgramArguments` or set `EnvironmentVariables.PATH` explicitly.865. Lint the plan with `scripts/plist_lint.mjs` before you ever run `launchctl bootstrap`.876. Load it with the modern `launchctl bootstrap gui/$(id -u) <plist>` (Agent) or `launchctl bootstrap system <plist>` (Daemon) — not the deprecated `load`/`unload`.887. Add an EXTERNAL supervision-integrity check, separate from both the daemon and its KeepAlive stanza, that runs `launchctl list <label>` for every legitimate supervisor label on a schedule or on `doctor`/health-check invocation, and classifies severity (ok / warn / critical) — because KeepAlive only acts while the job is loaded, and it cannot detect its own absence.8990## Output Contract9192`scripts/plist_lint.mjs` returns:9394- `pass` — boolean, true only when zero `critical` findings remain.95- `findings` — array of `{ severity: "critical"|"warning", code, message }`, one per detected defect.96- `recommendations` — array of concrete fix strings, one per finding, in the same order.9798Use `scripts/plist_lint.mjs` to lint a launchd job plan (JSON matching `schemas/launchd-plan.schema.json`) and get a deterministic pass/fail with actionable findings before you ship a plist.99100## Anti-Patterns101102### KeepAlive Means It'll Always Restart103104**Novice**: Sets `KeepAlive: true` and treats the daemon as permanently self-healing — "launchd's got it."105**Expert**: `KeepAlive` only supervises *while the job is loaded*. A `brew upgrade`, a user logout, a `launchctl bootout`, or a corrupted plist unloads the job entirely, and nothing resurrects the daemon until something re-bootstraps the supervisor itself. Port Daddy hit this for real: Homebrew's `KeepAlive` on `homebrew.mxcl.port-daddy` supervised the daemon perfectly — until `brew upgrade` churn unloaded the job, and the daemon died silently with zero supervisors watching. The fix (PR #607) was `pd doctor supervision-integrity`: an external, 3-tier severity check (`ok` / `warn` / `critical`) that queries `launchctl list <label>` for every legitimate supervisor label, independent of the daemon's own health.106**Detection**: A plist with `KeepAlive` set and no separate process, cron, or `doctor` command that periodically confirms the supervisor label itself is still loaded — grep for `KeepAlive` with no companion `launchctl list` call anywhere in the codebase.107108### Logging To /tmp109110**Novice**: `StandardOutPath` and `StandardErrorPath` point at `/tmp/mydaemon.log` "because logs are temporary anyway."111**Expert**: macOS purges `/tmp` and `/private/tmp` on a schedule and on every reboot. The one log that would explain a boot-time crash is gone before anyone reads it — especially cruel because boot-time failures are exactly when you need durable logs. Use `~/Library/Logs/<app>/` for a LaunchAgent or `/Library/Logs/<app>/` / `/var/log/<app>/` for a LaunchDaemon.112**Detection**: `grep -E 'StandardOutPath|StandardErrorPath' *.plist` shows a value starting with `/tmp` or `/private/tmp`.113114### Trusting Shell PATH Inside A launchd Job115116**Novice**: `ProgramArguments: ["node", "server.js"]`, assuming `node` resolves the way it does in Terminal.117**Expert**: launchd jobs start with a minimal environment (`PATH=/usr/bin:/bin:/usr/sbin:/sbin`) and never source `.zshrc`/`.bash_profile`. Homebrew-installed binaries, `nvm` shims, and anything on a custom `PATH` all 404 as "command not found" even though `which node` works fine interactively. Use an absolute path (`/opt/homebrew/bin/node`) as `ProgramArguments[0]`, or set `EnvironmentVariables.PATH` explicitly in the plist.118**Detection**: `ProgramArguments[0]` is a bare command name (no leading `/`) and the plist has no `EnvironmentVariables.PATH` key.119120## References121122| File | Load When |123| --- | --- |124| `references/plist-field-reference.md` | Need the exact meaning and failure semantics of each plist key before writing one. |125| `references/agent-vs-daemon-and-integrity.md` | Need to decide LaunchAgent vs LaunchDaemon, or design a supervision-integrity check. |126| `examples/expected-output.md` | Need a worked example: a real annotated plist plus its lint report. |127| `templates/output-template.md` | Need a reusable skeleton for a plist-plus-lint deliverable. |128| `schemas/launchd-plan.schema.json` | Need to validate a launchd job plan before running the linter. |129| `scripts/plist_lint.mjs` | Need deterministic linting of a launchd job plan. |130| `agents/openai.yaml` | Need a subagent descriptor for delegated launchd plist authoring/review. |131132<!-- BEGIN BUNDLE INDEX (auto: index_references.py) -->133134## Skill Bundle Index135136*Every file in this skill, and when to open it. Auto-generated; run `scripts/index_references.py --fix`.*137138**root**139- [`CHANGELOG.md`](CHANGELOG.md) — macOS Launchd Supervision — Changelog — - Initial skill creation - Core process defined - Reference files and deterministic plist_lint script added140- [`README.md`](README.md) — macOS Launchd Supervision — Author correct launchd plists for long-lived local daemons, and design the external supervision-integrity check that catches what `KeepAlive141142**`agents/`**143- [`agents/openai.yaml`](agents/openai.yaml) — openai (data/schema)144145**`examples/`**146- [`examples/expected-output.md`](examples/expected-output.md) — Example Output: macOS Launchd Supervision — Scenario: Port Daddy's own daemon (`pd start --foreground`) needs a LaunchAgent that survives `brew upgrade` churn, logs durably, and is che147- [`examples/sample-input.json`](examples/sample-input.json) — sample input (data/schema)148149**`references/`**150- [`references/agent-vs-daemon-and-integrity.md`](references/agent-vs-daemon-and-integrity.md) — LaunchAgent vs LaunchDaemon, and the Supervision-Integrity Pattern — Use this when deciding where a job lives, or when designing a check that verifies the supervisor itself.151- [`references/plist-field-reference.md`](references/plist-field-reference.md) — Plist Field Reference — Use this when writing or reviewing the actual `.plist` keys, not just the high-level plan.152153**`schemas/`**154- [`schemas/launchd-plan.schema.json`](schemas/launchd-plan.schema.json) — launchd plan.schema (data/schema)155156**`scripts/`**157- [`scripts/plist_lint.mjs`](scripts/plist_lint.mjs)158159**`templates/`**160- [`templates/output-template.md`](templates/output-template.md) — Launchd Supervision Plan — - Label: `[reverse-DNS label]` - Placement: `[agent | daemon]` — because `[GUI-session need / boot-time need]` Run: Paste the lint report an161162<!-- END BUNDLE INDEX -->