macos-service-management
Manage and troubleshoot macOS launchd services — plist authoring, exit code diagnosis, variable expansion rules, and service lifecycle
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 4
- Forks
- 1
01 前言 Front Matter
- name
- macos-service-management
- version
- 1.0.0
- category
- apple
- description
- Manage and troubleshoot macOS launchd services — plist authoring, exit code diagnosis, variable expansion rules, and service lifecycle
03 SKILL.md 正文
macOS Service Management (launchd)
Manage and debug launchd-based daemons and user agents on macOS. Launchd is macOS's system-wide service manager — analogous to systemd on Linux but with very different rules for paths, variables, and service configuration.
Key Differences from systemd
| Concept | systemd (Linux) | launchd (macOS) |
|---|---|---|
| Unit file | .service |
.plist (XML property list) |
| User services | ~/.config/systemd/user/ |
~/Library/LaunchAgents/ |
| System services | /etc/systemd/system/ |
/Library/LaunchDaemons/ |
| User home variable | %h (expanded) |
$HOME — only in specific fields |
| Shell variable expansion | Full (all env vars) | None — only $HOME, $USER, tilde ~ |
| Exit code 78 | N/A | EX_CONFIG — config error, launchd refuses to load |
| Reload | systemctl daemon-reload |
launchctl unload + load (or bootout/bootstrap) |
Service Lifecycle
# Load / start
launchctl load ~/Library/LaunchAgents/com.example.service.plist
# Modern form:
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.example.service.plist
# Stop / unload
launchctl unload ~/Library/LaunchAgents/com.example.service.plist
# or
launchctl bootout gui/$(id -u)/com.example.service
# Check status
launchctl list | grep example
# OR status with exit code: launchctl print gui/$(id -u)/com.example.service
Plist Authoring
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key><string>com.example.service</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/my-service</string>
<string>--flag</string>
</array>
<key>RunAtLoad</key><true/>
<key>KeepAlive</key><true/>
<key>WorkingDirectory</key><string>/Users/luke/project</string>
<key>StandardOutPath</key><string>/tmp/my-service.log</string>
<key>StandardErrorPath</key><string>/tmp/my-service.err</string>
</dict>
</plist>
Validate: plutil -lint com.example.service.plist
Variable Expansion Rules
$HOMEand$USERare expanded — but only in select keys (ProgramArguments, WorkingDirectory, paths).~tilde is expanded at the start of a path.- No other shell variables or
$(...)are expanded. A plist is NOT a shell script — write the full path. - Environment variables for the child process go in an explicit
<key>EnvironmentVariables</key>dict.
Exit Code Diagnosis
| Exit code | Meaning | Action |
|---|---|---|
| 0 | Clean exit | Normal (if KeepAlive, it restarts) |
| 1 | Generic error | Check the program's own logs |
| 78 | EX_CONFIG — bad plist | plutil -lint the plist; fix syntax |
| 126/127 | Command not found / not executable | Fix ProgramArguments path or chmod +x |
A service that exits immediately with 78 means launchd rejected the plist config itself — the process never ran. Fix the plist, not the program.
Restart-on-exit: the exit code must match the KeepAlive condition
launchd defines exactly five KeepAlive conditions — SuccessfulExit,
NetworkState, PathState, OtherJobEnabled, Crashed. Read them from the man
page (man 5 launchd.plist); anything else in a KeepAlive dict is not a
condition, so launchd finds no reason to restart the job and falls back to
demand-only launching.
| KeepAlive condition | Relaunches after | A deliberate self-restart must exit |
|---|---|---|
<true/> |
any exit | 0 |
SuccessfulExit true |
exit status 0 only | 0 |
SuccessfulExit false |
the INVERSE: exit status non-zero | non-zero (75, EX_TEMPFAIL) |
Crashed true |
a signal-terminated death only | n/a — a clean exit is not restarted |
SuccessfulExit=falsedoes NOT mean "restart after a clean exit". Apple's wording is "restarted in the inverse condition": the job comes back on a NON-zero status and a cleanexit 0PARKS it. A restart command written for systemd (os._exit(0)underRestart=always) therefore stops the service silently — the exact failure the policy was added to fix.systemctl-shaped restart logic ported to launchd without changing the exit code is the trap.CrashedOrAbnormalExitis not a launchd key. It appears in no man page, so a KeepAlive dict carrying only it gives launchd no reason to restart: the job stays down after any exit. Replace it withSuccessfulExit=false+ a non-zero exit code, or<true/>+ exit 0.- Derive the exit code from the supervisor; never assume 0. One binary may run
under systemd (
Restart=always→ 0) or launchd (SuccessfulExit=false→ 75). Prove which job owns the process withlaunchctl list: its PID column showing OUR pid is the test, becauseXPC_SERVICE_NAMEis inherited by every child exactly as systemd'sINVOCATION_IDis. Then read the job's OWN plist (plutil -porplistlib) and exit with the code that policy relaunches on; when no recognised condition covers a deliberate exit, refuse and name the plist fix rather than exiting into silence. - Confirm a relaunch with the job, not the chat:
launchctl list <label>shows a NEW pid, andLastExitStatusis recorded asexit_code << 8(75 → 19200).
Pitfalls
- ❌ KeepAlive with unknown keys — a dict of keys launchd does not define (e.g.
CrashedOrAbnormalExit) is the same as no KeepAlive at all: no restart, and the supervisor looks configured.plutil -lintpasses; only the man page can tell you. - ❌ Shell variables in ProgramArguments —
$(which python3)does NOT expand. Use the absolute path. - ❌ Unloaded vs stopped —
launchctl unloadremoves the service definition; a stopped-but-loaded service stays defined and can be restarted. - ❌ Per-user vs system — a plist in
~/Library/LaunchAgentsruns as you;/Library/LaunchDaemonsruns as root. Don't put a user's credentials in a daemon. - ❌ KeepAlive=true with a crash-looping process — launchd will restart it
forever, hammering the CPU. Add a
ThrottleInterval(default 10s) or fix the crash.
Related
macos-computer-use— macOS desktop automationlinux-server-hardening— the Linux-side equivalent (systemd)
04 同仓库更多 Skills
macbook-linux-maintenance
Legacy MacBook Linux fixes: GPU wedge, reboot hang, thermal.
governance-regression-triage
Triage a host's governance issues: what changed.
enforcement-immutability
Use when enforcement files lose their immutable flag.
steadfaste-fleet-repo-deploy
Use when committing to the steadfaste-fleet repo.
steadfaste-fleet-maintenance
Maintain an installed Steadfaste Fleet instance — update both the upstream Hermes Agent and the fleet repo layer, merge diverged histories, sync skills safely, and troubleshoot common issues. Covers the dual-update workflow (hermes update + fleet-update.sh), encrypted brain data backup, mycortex import, and recovery techniques.
fleet-preflight
DEPRECATED alias — merged into survey-before-action (2026-08-20). Load survey-before-action instead; its "Repo-Specific Pre-Flight" section covers git search, Hermes boundary, and deployment verification.