launchd, not cron: scheduling jobs the way macOS wants
A laptop that sleeps through 3 a.m. never runs a 3 a.m. cron job. A launchd agent with StartCalendarInterval runs it as soon as the Mac wakes.
Agents and daemons
User agents live in ~/Library/LaunchAgents and run as you when you are logged in. Daemons live in /Library/LaunchDaemons and run as root at boot. Almost everything personal belongs in an agent.
Loading the job
launchctl bootstrap gui/$(id -u) path/to/plist loads it for the current session; bootout removes it. The older load and unload verbs still work but print deprecation warnings.
Logs
Point StandardOutPath and StandardErrorPath at files in ~/Library/Logs and Console.app will show them next to system logs.
- Label jobs with reverse-DNS names.
- Use absolute paths; launchd has a minimal PATH.
- Test with launchctl kickstart -k.
<key>Label</key>
<string>dev.brewlog.brew-update</string>
<key>ProgramArguments</key>
<array><string>/opt/homebrew/bin/brew</string><string>update</string></array>
<key>StartCalendarInterval</key>
<dict><key>Hour</key><integer>9</integer></dict>
There are only two hard things in Computer Science: cache invalidation and naming things.
Phil Karlton, as quoted by Tim Bray
plutil -lint on it before loading.
$ cat /comments 10
Finally someone explains this without assuming I already know it.
Nice pick. Already set it on both displays.
Printed this out and stuck it next to my desk. Old habits.
Would this work with fish instead of zsh?
Small typo in the second paragraph, but otherwise perfect.
Yes, it works the same way. I will add a note about it.
This is exactly the write-up I needed this week. Bookmarked.
Glad it helped. Let me know if anything breaks after the next update.
Good question, I would like to know too.
Tried this on an M1 Air and got the same numbers within a few percent.