On macOS, the first process the kernel starts, PID 1, is called launchd. Apple introduced it with Mac OS X 10.4 Tiger in April 2005 to merge into a single system what Unix used to spread across several separate pieces: init and its boot scripts, cron for scheduled jobs, inetd for services launched on incoming connections, and SystemStarter, the boot manager of early Mac OS X releases. Its core idea, start things only when they are needed and watch them for as long as they live, went on to influence systemd, the init system most Linux distributions use today.
Key points on launchd vs systemd and cron in 20 seconds
- launchd has been macOS’s PID 1 since Tiger (2005), replacing init, cron, inetd and SystemStarter.
- Jobs are described in plist files; launchd decides when to start them and supervises them.
- It separates system daemons from per-user agents.
- systemd borrowed socket activation from launchd, according to its own author.
For an admin used to Linux, launchd is often “the plist thing you copy from Stack Overflow”. It deserves more attention, both for managing Macs and for understanding where many systemd ideas came from. This article covers how it works, how to write and manage a job with current launchctl syntax, and how it compares with systemd and good old cron.
The problem it set out to solve
Before 2005, booting a typical Unix relied on numbered scripts (S20network, S80httpd and so on) run in order, with no real dependencies beyond the file name. cron fired jobs at a fixed time without knowing whether the previous run had finished, and inetd listened on ports to spawn a process per connection. None of these pieces talked to each other, and when a service crashed it stayed down until somebody noticed.
launchd unified that work around two principles. The first is on-demand activation: launchd creates a service’s sockets or communication ports in advance and only starts the service when someone connects, so boot order stops mattering. The second is supervision: launchd is the parent of the processes it launches, knows when they exit and can restart them.
Lennart Poettering said so openly in “Rethinking PID 1”, the April 2010 post that introduced systemd: he pointed to launchd as the model for pulling socket listening out of daemons and into the init system, and noted that the idea was even older, since inetd already worked in a similar way.
Daemons, agents and where they live
launchd separates two kinds of jobs. Daemons run system-wide with no user session required, usually as root or a service account. Agents belong to a user’s session and can interact with that user’s desktop. Each kind has its own folders:
/System/Library/LaunchDaemonsand/System/Library/LaunchAgents: Apple’s own, on the protected system volume. Leave them alone./Library/LaunchDaemons: daemons installed by the admin or third-party software./Library/LaunchAgents: agents for every user.~/Library/LaunchAgents: agents for one user, no admin rights needed.
Since OS X 10.10 Yosemite, launchd organizes jobs into domains: system for daemons, gui/UID for agents in a user’s graphical session and user/UID for that user’s background session. Modern launchctl subcommands use that naming. And since macOS 13 Ventura, the system notifies users when an app adds background items and lists them in System Settings under login items, where they can be switched off.
Writing and managing a job
A launchd job is an XML property list (plist) describing what to run and under what conditions. This user agent runs a script every day at 03:15 and writes its output to a file:
<?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.backup</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/backup.sh</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key><integer>3</integer>
<key>Minute</key><integer>15</integer>
</dict>
<key>StandardOutPath</key>
<string>/tmp/backup.log</string>
<key>StandardErrorPath</key>
<string>/tmp/backup.err</string>
</dict>
</plist>Code language: HTML, XML (xml)
Save it as ~/Library/LaunchAgents/com.example.backup.plist and manage it with launchctl. Plenty of tutorials still use launchctl load and unload, but the man page has listed them as legacy subcommands for years. The current way looks like this:
# Check the plist is valid
plutil -lint ~/Library/LaunchAgents/com.example.backup.plist
# Load the agent into the current user's GUI session
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.example.backup.plist
# Show state, PID, last exit code and triggers
launchctl print gui/$(id -u)/com.example.backup
# Run it right now, off schedule (-k kills a running instance first)
launchctl kickstart -k gui/$(id -u)/com.example.backup
# Unload it
launchctl bootout gui/$(id -u)/com.example.backup
# For a system daemon the domain is system and sudo is required
sudo launchctl bootstrap system /Library/LaunchDaemons/com.example.daemon.plistCode language: PHP (php)
The most useful keys are RunAtLoad (start on load), KeepAlive (keep the process running, with conditions such as SuccessfulExit to restart only on failure), ThrottleInterval (minimum seconds between restarts, 10 by default), WatchPaths (launch when a file changes), Sockets (activation on connection) and StartInterval or StartCalendarInterval for scheduling.
There is one scheduling detail many articles get wrong. Apple’s documentation says that if a Mac is asleep when a StartCalendarInterval job should have run, it runs on wake, and several missed runs are coalesced into one. With StartInterval, by contrast, the current man page warns that a firing that falls during sleep is missed. For jobs that must not be skipped, such as backups, use the former.
Comparison: launchd, systemd and cron
launchd and systemd share the philosophy of supervising processes and activating them on demand, but differ in many details. cron, meanwhile, is still available on both, although Apple calls it a non-recommended option on macOS.
| Aspect | launchd (macOS) | systemd (Linux) | cron + classic init |
|---|---|---|---|
| Configuration | One XML plist per job | INI-style unit files (.service, .timer, .socket) | One-line crontab and shell scripts |
| Socket activation | Yes (Sockets key) | Yes (.socket units) | Only via inetd or xinetd |
| Restart on failure | KeepAlive and ThrottleInterval | Restart= and RestartSec= | No |
| Explicit dependencies | No; resolved through on-demand activation | Yes (After=, Requires=, Wants=) | Ordering by script name |
| Runs missed during sleep | StartCalendarInterval coalesces them on wake | Persistent=true on the timer | Lost (unless anacron) |
| Per-user services | LaunchAgents in the gui/UID domain | systemctl –user | Per-user crontab |
| Logging | Unified log (log show) and own files | journald (journalctl) | Mail or redirection to files |
| Resource control | Basic limits (SoftResourceLimits, Nice) | Full cgroups (MemoryMax=, CPUQuota=) | ulimit inside the script |
The deepest difference is dependencies. launchd deliberately does not declare them: if a service needs another, it connects to its socket and launchd starts it at that moment. systemd uses socket activation too, but adds an explicit dependency graph and also manages mounts, devices, networking and control groups (cgroups), which makes it a much broader system. For reference, the same daily job written as a systemd user service would look like this:
# ~/.config/systemd/user/backup.service
[Unit]
Description=Daily backup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
# ~/.config/systemd/user/backup.timer
[Unit]
Description=Run the backup daily
[Timer]
OnCalendar=*-*-* 03:15:00
Persistent=true
[Install]
WantedBy=timers.target
# Enable and inspect
systemctl --user daemon-reload
systemctl --user enable --now backup.timer
systemctl --user list-timers
journalctl --user -u backup.serviceCode language: PHP (php)
In cron it is a single line, 15 3 * * * /usr/local/bin/backup.sh, in exchange for no restarts, no integrated logging and no catch-up if the machine was off.
When to use each, and how to debug
On a Mac, launchd is the right choice for anything that must survive reboots, recover from failures or react to events. cron still works for a simple personal script, but on recent macOS it needs Full Disk Access to touch many protected folders, and it adds nothing launchd does not do better. On Linux, systemd timers have displaced cron on most distributions for the same reasons, although cron remains handy for its simplicity and portability.
When a launchd job will not start, the usual suspects are a malformed plist (caught by plutil -lint), a relative path in ProgramArguments, a script without execute permission, or an environment different from your terminal’s: launchd does not load your shell’s PATH, so use absolute paths or set the EnvironmentVariables key. launchctl print shows the last exit code, and the unified log helps you see why it failed:
# launchd messages from the last hour mentioning the job
log show --last 1h --predicate 'process == "launchd"' | grep com.example.backup
# List the user's loaded jobs and filter
launchctl list | grep com.exampleCode language: PHP (php)
Twenty years after it arrived, launchd remains one of the least visible and most important parts of macOS. And for anyone who runs Linux, knowing it helps explain why systemd looks the way it does.
FAQ
What is launchd on macOS?
macOS’s PID 1 since Mac OS X 10.4 Tiger: it starts and supervises services, scheduled jobs and user agents, replacing init, cron, inetd and SystemStarter.
What is the difference between a LaunchDaemon and a LaunchAgent?
A daemon runs system-wide without a logged-in session; an agent belongs to a user’s session and can interact with that user’s desktop.
Is launchctl load deprecated?
It still works, but the man page lists it as legacy. The recommended commands are launchctl bootstrap, bootout, kickstart and print with the right domain.
Is systemd based on launchd?
It is not a copy, but its author cited launchd as the inspiration for socket activation. systemd adds explicit dependencies, cgroups and many other features.
Sources:
