Lesson 5 of 9 · 28m
launchd Agents, Daemons, and the Bootstrapping Discipline
Model persistent jobs correctly with launchd plist keys, service domains, lifecycle commands, logs, and failure recovery.
launchd is one of the best service managers ever shipped in a consumer operating system, and also one of the easiest to misread. Jobs silently fail because they are in the wrong domain, a plist key is misspelled, a user session does not exist, or the process exits too quickly and launchd throttles it. Learn the model once and most mystery disappears.
Agents are not daemons
LaunchDaemons run in system context, typically as root or another service account, whether or not anyone logs in. LaunchAgents run inside a user session. The distinction is security architecture, not just a directory name:
/Library/LaunchDaemons system context, root-owned plist, bootstrap system
/Library/LaunchAgents every user's login session, bootstrap gui/<uid>
~/Library/LaunchAgents one user's session, user-ownedIf a job needs a window server, keychain access, or user preferences, a system daemon is usually wrong. If it must run before login, a user agent is impossible.
A launchd job is desired state plus event triggers
A minimal daemon plist:
<dict>
<key>Label</key><string>org.example.inventory</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/inventory-agent</string>
<string>--submit</string>
</array>
<key>StartInterval</key><integer>900</integer>
<key>RunAtLoad</key><true/>
<key>StandardOutPath</key><string>/var/log/inventory-agent.out</string>
<key>StandardErrorPath</key><string>/var/log/inventory-agent.err</string>
</dict>Validate with plutil -lint before loading. ProgramArguments is preferred over shell strings because it avoids quoting accidents and implicit shell behavior.
Use modern bootstrapping commands
The modern interface is explicit about service domains:
sudo launchctl bootstrap system /Library/LaunchDaemons/org.example.inventory.plist
sudo launchctl bootout system/org.example.inventory
sudo launchctl kickstart -k system/org.example.inventory
launchctl print system/org.example.inventory | lessFor a logged-in user agent, the domain is gui/$(id -u username). Loading into the wrong domain produces the classic "plist is correct but nothing runs" failure.
Read failure state, not just process existence
launchctl print exposes last exit status, PID, throttling, and event state. A zero-length log is information. A job exiting within seconds repeatedly may be throttled. KeepAlive is not a substitute for fixing a crash loop; it can amplify damage if the process mutates state every restart.
Field notes
- Plist files in
/Library/LaunchDaemonsmust be owned by root and not writable by group or others. launchctl loadandunloadare legacy habits; usebootstrapandbootoutfor clarity.StartCalendarIntervaluses local time. Fleet-wide jobs scheduled at the same minute create synchronized network storms.- If a script needs network reachability, do not assume boot order guarantees it. Implement retry inside the worker.
Was this lesson helpful?