Launchd is basically OSX’s version of cron, and it’s taken me years to get a launchd job actually working (not of regular effort, of course, but still). My main issue was not really knowing how to debug my plist file. Instead, all I’d see were cryptic error messages like the following:
Jan 18 01:29:27 thisbemymachine com.apple.launchd.peruser.501[268] (com.medialog.app[86862]): Exited with code: 1
No longer! Here is my definitive solution to getting a plist file to run with launchd.
-
First off, let’s create a dummy python file. Below, I assume this file is at
/Users/mobeets/test.py
. This can do something like write something to disk, or send an email–whatever you want. I also assume that this file might needs an environment variable set, likePYTHONPATH
, which I’ve set below arbitrarily to/Users/mobeets/Box Sync/bin
. (Note the absense of quotations in any of the<string>
variables below, even when the string contains a space.) -
Now create the following dummy plist file (let’s call it
filename.plist
, where the job name isexample
) that runs on load.
-
Run
plutil filename.plist
to verify your plist file is valid. -
We want to be able to continuously monitor the outputs and errors of running our plist file. Open up a terminal and run
tail -F /var/log/system.log /tmp/test.stdout /tmp/test.stderr | grep example
. -
Now in another terminal we can load, unload, and start the job, all while monitoring the error logs to see if anything goes wrong. Load the plist file with
launchctl filename.plist
. Unload withlaunchctl filename.plist
. Run withlaunchctl start example
. -
If everything works as planned, you can customize your plist file to make it do what you actually want, iterating on the above steps until everything works. Launchd.info is a great resource for figuring out how to do various things.