launchd
notes to self (mostly)
Since Apple has deprecated cron
on macOS, I've decided to get my feet wet with its replacement, launchd
. I used to used cron
to automate post publishing. One downside of static site generators is that you can't SELECT * FROM
. You have to find another solution. For me, that was posts
where pub_date
< NOW()cron
, until it wasn't.
launchd
— specifically, the .plist
file syntax — is easy to learn. There's a whole man page for launchd.plist
that will help you write your own agents. I also found launchd.info helpful.
Status codes, on the other hand, can be opaque and hard to search. Running launchctl list
gives you a list of running agents. Its output looks a bit like the example below.
693 0 com.apple.CoreAuthentication.agent
632 0 com.apple.syncdefaultsd
- 0 com.apple.sidecar-display-agent
652 0 com.apple.accessibility.heard
653 0 com.apple.corespeechd
- 0 com.apple.AMPSystemPlayerAgent
777 0 com.apple.itunescloudd
- 0 com.apple.scrod
- 0 com.myusername.publishblog
The first column indicates the process identifier (PID). Not every agent will have one. The second column is the status code, if any, that occurs when this agent runs. The last column reflects the agent label, as specified by the <Label>
field in the corresponding .plist
file.
A 0
in the second column indicates that launchd
was able to run that file without any issues. Other values may indicate a problem.
This is my cheat sheet for those values. I may be wrong about the details, but they should help you (and future me) figure out where to begin. I'm sure there's a list of these values somewhere.
Error code | Possible cause |
---|---|
0 | Everything is okay. There are no issues with your .plist file, or any commands it calls. |
29 | Your command wrote something to stdout. The command ran just fine, but you may wish to stop writing to stdout. Alternatively, add a StandardOutPath property key to your .plist and write to a file instead. |
78 | Function not implemented. Check that your .plist file is calling the correct command or referencing the correct script. |
255 | Your script has a runtime error and can't be executed. |
Possibly helpful things written by other people
- MacOS launchd plist StartInterval and StartCalendarInterval examples
- A quick introduction to
launchd.plist
with code examples that you can adapt to your needs. - Mac crontab: Creating macOS startup jobs with crontab, er, launchd
- A post detailing how to write your own
launchd
agent. - Creating Launch Daemons and Agents
- How to use
launchd
, straight from Apple's developer documentation.