launchd on macOS: How PID 1 Starts and Supervises Every Process
What launchd is and how it works: PID 1 on macOS, LaunchDaemons vs LaunchAgents, plist keys, launchctl domains (system, gui, user), on-demand launch, and debugging a job that won't start.
launchd is process 1 on macOS. The kernel execs it at the end of boot, and from then on it is the only thing that starts long-running work: system daemons, per-user agents, XPC services inside apps, periodic jobs, and the login window itself. When people ask "what is launchd on my Mac", the short answer is: it is init, cron, inetd and a service registry rolled into one process, configured entirely with property lists.
This article goes past the short answer. It covers how the kernel starts launchd, where job definitions live and who loads them, the plist keys that matter, the domain model that modern launchctl is built around, on-demand launching through Mach ports, and how to debug a job that refuses to start.
What launchd is
launchd shipped in Mac OS X 10.4 Tiger and replaced a pile of older mechanisms: BSD init and the rc scripts, SystemStarter, inetd/xinetd, mach_init, and (in practice) cron. It does four jobs:
- init. It is PID 1. Every userspace process descends from it, and orphaned processes are re-parented to it.
- Service manager. It reads job definitions (plists), starts processes, restarts them under the conditions you specify, and stops them at shutdown.
- Bootstrap server. It owns the Mach bootstrap namespace: the name → port registry that every process uses to find system services. This is what makes XPC work.
- Event activator. It can hold a socket, a Mach service name, a watched path or a calendar trigger, and only start the job when the event fires.
Since OS X 10.10 the implementation has been tied to libxpc, which is why its logs and state directories are named com.apple.xpc.launchd. Apple's original launchd source is still published, and the design described there holds, but the shipping binary is a newer, closed evolution of it.
How launchd starts
The hand-off happens at the very end of the kernel's boot path (walked stage by stage in How macOS boots). bsdinit_task calls load_init_program(), which execs /sbin/launchd into the process that becomes PID 1. On release kernels /sbin/launchd is the only candidate; development and debug kernels can pick an alternate launchd under /usr/appleinternal/sbin/ through the launchdsuffix boot-arg.
PID 1 is not allowed to die. If launchd exits, the kernel panics with an initproc exited message, because there is nothing left to reap orphans or keep the system's services alive.
Once running, launchd loads the system domain (every plist in the LaunchDaemons directories), starts the jobs that ask to run at load, and leaves the rest dormant until something needs them. When a user logs in, it creates that user's GUI domain and loads the LaunchAgents into it.
LaunchDaemons vs LaunchAgents
Both are launchd jobs with the same plist format. What differs is the domain they are loaded into, which decides who they run as and what they can reach.
| Directory | Kind | Owner | Loaded into | Runs as |
|---|---|---|---|---|
/System/Library/LaunchDaemons/ | Daemon | Apple (read-only, on the sealed system volume) | system at boot | root, or the plist's UserName |
/Library/LaunchDaemons/ | Daemon | Third-party installers, admins | system at boot | root, or the plist's UserName |
/System/Library/LaunchAgents/ | Agent | Apple | each user's gui/<uid> at login | the logged-in user |
/Library/LaunchAgents/ | Agent | Third-party, all users | each user's gui/<uid> at login | the logged-in user |
~/Library/LaunchAgents/ | Agent | One user | that user's gui/<uid> at login | that user |
The practical rules that follow from this table:
- A daemon has no user session. It starts before anyone logs in, cannot show UI, and cannot reach a user's pasteboard or login keychain. Use one for system-wide services: a VPN helper, a backup engine, an endpoint security agent.
- An agent lives inside a user's session. It can talk to the window server and the user's keychain, and it stops when the user logs out. Use one for menu-bar helpers, sync clients and anything that needs the user's context.
- Plists in
/Library/LaunchDaemons/must be owned byroot:wheeland must not be group- or world-writable. launchd refuses to load them otherwise, because a writable daemon plist would be a root privilege escalation. - Apps on macOS 13 and later should not drop plists by hand.
SMAppServiceregisters a daemon or agent that ships inside the app bundle, and the user approves it under System Settings → General → Login Items. Background Task Management tracks those registrations;sfltool dumpbtmprints the database.
Because these directories are the standard way to get code to run at every boot or login, they are also a classic persistence location (MITRE ATT&CK T1543.001 for Launch Agents, T1543.004 for Launch Daemons). Reviewing their contents is one of the first steps in any macOS incident response.
Anatomy of a launchd plist
A job is an XML (or binary) property list. Here is a complete, minimal daemon that runs a script every night at 03:15 and logs its output:
<?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.nightly-cleanup</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/libexec/nightly-cleanup.sh</string>
<string>--verbose</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>3</integer>
<key>Minute</key>
<integer>15</integer>
</dict>
<key>StandardOutPath</key>
<string>/var/log/nightly-cleanup.log</string>
<key>StandardErrorPath</key>
<string>/var/log/nightly-cleanup.log</string>
</dict>
</plist>
The keys you will use most, as documented in man launchd.plist:
| Key | What it does |
|---|---|
Label | Unique job name. Conventionally reverse-DNS and identical to the file name minus .plist. Required. |
Program / ProgramArguments | The executable, and its argv. ProgramArguments[0] is used as the path if Program is absent. launchd does not run a shell, so there is no ~, glob or $VAR expansion. |
RunAtLoad | Start the job as soon as it is loaded (at boot for daemons, at login for agents). |
KeepAlive | true restarts the job whenever it exits. A dictionary makes it conditional: SuccessfulExit (false = restart only after a non-zero exit), PathState, OtherJobEnabled, Crashed. |
StartInterval | Run every N seconds. |
StartCalendarInterval | Cron-style schedule with Minute, Hour, Day, Weekday, Month. Unlike cron, a run missed while the Mac was asleep fires on wake (several missed runs coalesce into one). |
WatchPaths / QueueDirectories | Start when a path changes, or while a directory is non-empty. |
MachServices | Mach service names launchd registers on the job's behalf. The first lookup or XPC connection to one of them starts the job. |
Sockets | Sockets launchd listens on and hands to the job at launch (inetd-style activation). |
UserName / GroupName | Drop privileges for a daemon. Ignored for agents, which always run as the session's user. |
ThrottleInterval | Minimum seconds between spawns. The default is 10, so a job that crashes instantly is respawned at most every 10 seconds. |
ProcessType | Background, Standard, Adaptive or Interactive: the resource and scheduling policy launchd applies. |
LimitLoadToSessionType | For agents: Aqua (GUI login), Background, LoginWindow, StandardIO, System. |
EnvironmentVariables, WorkingDirectory, StandardOutPath, StandardErrorPath | Process environment and output redirection. |
Validate a plist before loading it: plutil -lint com.example.nightly-cleanup.plist.
Domains: system, gui/<uid> and user/<uid>
The launchd that shipped with OS X 10.10 reorganised everything around domains, and the modern launchctl subcommands take a domain or a service in a domain as their target:
system: the root domain. Holds every LaunchDaemon. There is one.gui/<uid>: a user's graphical login session. LaunchAgents with the defaultAquasession type load here. Created at login, torn down at logout.user/<uid>: a per-user background domain that can exist without a GUI login (for example, for a user who only connects over SSH).login/<asid>: a domain tied to an audit session.pid/<pid>: a private domain for one process. An app's bundled XPC services (inContents/XPCServices/) live here, visible only to that app.
A service target is a domain plus a label: system/com.apple.mDNSResponder, gui/501/com.example.agent. Get your own UID with id -u, so gui/$(id -u)/... addresses your session.
launchctl in practice
launchctl is a client that sends requests to launchd. The modern verbs map directly onto the domain model:
# Load a job into a domain (and unload it)
sudo launchctl bootstrap system /Library/LaunchDaemons/com.example.helper.plist
sudo launchctl bootout system/com.example.helper
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.example.agent.plist
launchctl bootout gui/$(id -u)/com.example.agent
# Start (or restart with -k) a loaded job right now; -p prints the new PID
sudo launchctl kickstart -k -p system/com.example.helper
# Persistently enable / disable a label in a domain (survives reboots)
sudo launchctl disable system/com.example.helper
sudo launchctl enable system/com.example.helper
launchctl print-disabled system
# Inspect
launchctl list # jobs in your domain: PID, last status, label
launchctl print system/com.example.helper # full state of one service
launchctl print gui/$(id -u) # everything in your GUI session
launchctl blame system/com.example.helper # why launchd started it
The legacy verbs still work and still appear in older documentation:
| Legacy | Modern equivalent |
|---|---|
launchctl load file.plist | launchctl bootstrap <domain> file.plist |
launchctl load -w file.plist | launchctl enable <service> then bootstrap |
launchctl unload file.plist | launchctl bootout <domain>/<label> |
launchctl start <label> | launchctl kickstart <service> |
launchctl list <label> | launchctl print <service> |
The legacy forms guess the domain from who runs them (root → system, otherwise your session), which is the source of many "it loaded but nothing happened" reports: running sudo launchctl load ~/Library/LaunchAgents/x.plist loads a user agent into the system domain as root. The enable/disable state that -w used to write is kept by launchd in an overrides database under /private/var/db/com.apple.xpc.launchd/, not in your plist.
In launchctl list, the middle column is the last exit status. A negative number means the job was killed by that signal: -9 is SIGKILL, often from jetsam under memory pressure.
On-demand launch through Mach ports and XPC
launchd's most important trick is that a service does not need to be running for clients to find it. When a job declares MachServices, launchd creates the port, keeps the receive right, and publishes the name in the bootstrap namespace before the job's process exists.
Every process inherits a bootstrap port, a send right to launchd. A client that calls xpc_connection_create_mach_service("com.example.helper", ...) (or the lower-level bootstrap_look_up) asks launchd for that name and gets a send right back. The first message to the port is what triggers the launch:
- The client sends a message. It queues on the port, whose receive right launchd still holds.
- launchd sees pending traffic on a dormant job's port and spawns the job with
posix_spawn(see fork, exec and posix_spawn). - The job checks in (an XPC listener, or
bootstrap_check_in) and receives the receive right, with the client's message still queued on it. - When the job goes idle, it can exit. The name stays registered, launchd takes the receive right back, and the next message relaunches it.
Clients hold a name, never a PID, so this restart is invisible to them. The mechanics of ports, rights and queued messages are covered in Mach ports and messages; the XPC layer is a typed message protocol built on top of them.
This is also why so many macOS daemons show up in launchctl print system as loaded but have no PID: they are registered and waiting for their first message.
Debugging a job that won't start
Work through these in order.
1. Is the plist valid and in the right place?
plutil -lint /Library/LaunchDaemons/com.example.helper.plist
ls -l /Library/LaunchDaemons/com.example.helper.plist # must be root:wheel, not group/world writable
2. Is it loaded, and in which domain?
sudo launchctl print system/com.example.helper
If launchd answers that it could not find the service, the job is not loaded in that domain. Check that you bootstrapped it into the domain you are querying (agents are in gui/<uid>, not system). In the output, look at state, pid, runs, last exit code, program and arguments. A last exit code of 78: EX_CONFIG usually means launchd could not set up the spawn: wrong Program path, a missing executable bit, a working directory that does not exist, or a UserName that does not exist.
3. Is it disabled? A disabled label will not load even if the plist is correct.
launchctl print-disabled system | grep com.example.helper
sudo launchctl enable system/com.example.helper
4. Read launchd's own log. Spawn failures, throttling and ownership complaints are logged by launchd itself, not by your program:
log show --last 15m --predicate 'process == "launchd" AND eventMessage CONTAINS "com.example.helper"'
sudo tail -n 100 /private/var/log/com.apple.xpc.launchd/launchd.log
5. Run the exact command by hand. Copy ProgramArguments into a shell, as the same user, with no ~ or variables. launchd does not source your shell profile, so a PATH that works in Terminal may not exist for the job; set it in EnvironmentVariables or use absolute paths.
6. Check that it is not exiting too fast. A job that exits within seconds with KeepAlive set is respawned at most once per ThrottleInterval. If it keeps dying, the log shows it being throttled. A job that daemonizes (forks and exits the parent) confuses launchd too: launchd jobs should stay in the foreground.
launchd vs systemd
People coming from Linux usually want the mapping. systemd's author cited launchd's on-demand socket activation as a direct inspiration, so the two share a lot of ideas.
| Concern | launchd (macOS) | systemd (Linux) |
|---|---|---|
| Unit definition | XML/binary plist, one job per file | INI-style .service, .socket, .timer units |
| System vs user services | LaunchDaemons vs LaunchAgents (by directory) | system units vs systemctl --user units |
| Control tool | launchctl bootstrap / bootout / kickstart / print | systemctl start / stop / restart / status |
| Enable at boot | Loaded if the plist is present and not disabled | systemctl enable creates symlinks |
| Scheduled jobs | StartCalendarInterval, StartInterval | .timer units (OnCalendar=) |
| Socket activation | Sockets key | .socket units |
| IPC activation | MachServices (Mach ports, XPC) | D-Bus activation |
| Restart policy | KeepAlive (+ ThrottleInterval) | Restart= (+ RestartSec=) |
| Logs | Unified log (log show) | journald (journalctl) |
| Dependencies | None declared: jobs find each other on demand through names | Explicit Requires=, After= ordering |
The last row is the real design difference. launchd has no dependency graph: instead of ordering services, it makes every service name resolvable before its process exists, and lets the first message pull the provider up. No mainstream Linux distribution uses launchd.
What to read next
- How macOS boots for everything that happens before launchd runs.
- What happens when you launch an app for launchd spawning a GUI app on behalf of LaunchServices.
- Mach ports and messages for the port mechanics behind
MachServicesand XPC. - libdispatch internals for how XPC connections deliver messages onto dispatch queues inside a daemon.
For the man pages, man launchd.plist and man launchctl on any Mac are the authoritative key and verb lists for that macOS version.