--- name: mac-launchd description: Schedule a script on a Mac with launchd — a LaunchAgent that runs every morning, every hour, at login or on a file change — and debug the one that silently never ran. Use when you say "run this every day at 7", "cron on my mac", "make it start at login", "my launch agent isn't running", "launchctl load doesn't work", "Bootstrap failed: 5", "why does my job say command not found". Covers the plist template, StartCalendarInterval vs StartInterval, bootstrap/bootout/kickstart, reading last exit code, and the PATH, sleep and permission traps. compatibility: >- macOS (tested on macOS 26, Apple silicon). Built-in tools only: launchctl, plutil. No network. Per-user agents need no sudo. metadata: grounded: "2026-11-03 — template bootstrapped, kicked, inspected and booted out live on macOS 26.6" --- # launchd, the Mac's scheduler Use a **LaunchAgent** in `~/Library/LaunchAgents/` (runs as you, in your login session, no sudo). LaunchDaemons in `/Library/LaunchDaemons/` run as root and need an admin; you almost never want one. Cron still exists but runs without your session's permissions and is deprecated; don't. ## Template ```xml Labellocal.example.morning-job ProgramArguments /bin/zsh-lc exec "$HOME/bin/morning-job.sh" EnvironmentVariables PATH/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin StartCalendarInterval Hour7Minute0 StandardOutPath/tmp/morning-job.out.log StandardErrorPath/tmp/morning-job.err.log ``` - File name must equal `Label` + `.plist`. - Paths in the plist are literal: **no `~`, no `$HOME`** outside a shell string. Write the absolute path, or do as above and let `zsh -c` expand `$HOME`. - Schedules: `StartCalendarInterval` (wall clock; an array of dicts for several times; omitted keys are wildcards, so `{Minute:0}` is hourly), `StartInterval` (every N seconds), `RunAtLoad` (at login / on bootstrap), `WatchPaths` (when a file changes), `KeepAlive` (restart if it exits; for daemons, not jobs). - `plutil -lint file.plist` before loading. A malformed plist fails with an unhelpful error. ## Load, run, inspect, unload ```bash U=gui/$(id -u); L=local.example.morning-job; P=~/Library/LaunchAgents/$L.plist plutil -lint "$P" launchctl bootstrap $U "$P" # load (replaces the deprecated `launchctl load`) launchctl kickstart -p $U/$L # run it NOW, prints the pid — test before waiting for 7am launchctl print $U/$L | grep -E 'state|runs|last exit code|path' launchctl bootout $U/$L # unload (replaces `launchctl unload`) ``` After editing the plist you must `bootout` then `bootstrap` again; launchd caches it. `launchctl list | grep