Cron and launchctl on Mac OS X 10.5 Leopard

I was searching for a way to set up a cron job on my Mac, and couldn't find anything that spelled it out exactly how I needed it. http://arunxjacob.wordpress.com/ got me really really close though. I did finally get it working, so I thought I'd explain the crucial parts of my setup.

My goal is to port this cron command to my Mac:
*/5 * * * *   wget -O - -q -t 1 http://drupaldev1/cron.php

1. Create a .plist file. A new empty plain text file. I named mine local.drupaldev1.plist, since this is for a local development site on my computer. I placed it in ~/Library/LaunchAgents/ but you can put it anywhere on your computer.

2. Open that file in a plain text editor and paste in this starter code

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<false/>
<key>Label</key>
<string>local.drupaldev1</string>
<key>ProgramArguments</key>
<array>
<string>/opt/local/bin/wget</string>
<string>http://drupaldev1/cron.php</string>
</array>
<key>StartInterval</key>
<integer>300</integer>
<key>RunAtLoad</key>
<true />
<key>StandardErrorPath</key>
<string>/dev/null</string>
<key>StandardOutPath</key>
<string>/dev/null</string>
</dict>
</plist>

The first string listed under ProgramArguments is the program to execute. All strings that immediately follow it are its arguments.

3. Activate the file by doing this at the command line:

launchctl load /Users/my_username/Library/LaunchAgents/local.drupaldev1.plist

If you need to make changes to it, unload it and reload it.

launchctl unload /Users/my_username/Library/LaunchAgents/local.drupaldev1.plist
launchctl load /Users/my_username/Library/LaunchAgents/local.drupaldev1.plist

4. To make it load automatically when you startup your mac, create this file: ~/.launchd.conf and paste in the line above, minus launchctl:

load /Users/my_username/Library/LaunchAgents/local.drupaldev1.plist

5. The sites that I found useful...
http://arunxjacob.wordpress.com/2008/08/28/mac-launchd-and-launchctl-the...
http://www.wooblelab.com/command/show/106-scheduled-jobs-with-launch-dae...
http://en.wikipedia.org/wiki/Launchd
http://developer.apple.com/documentation/Darwin/Reference/ManPages/man5/...
http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/...
http://developer.apple.com/macosx/launchd.html

Comments

Well this is nice but why

Well this is nice but why reinvent the wheel, why not just install wget and set up a cronjob, mac is unix you know it has pretty much everything for it just like freebsd and linux

http://www.macports.org to get mac ports and install wget.

then type
crontab -e
and use the vi editor to put
*/5 * * * * wget -O - -q -t 1 http://drupaldev1/cron.php

your done.

While this works all dandy,

While this works all dandy, there is a Program key that you're supposed to use to specify the path to the program. the ProgramArguments key is purely just working by a fluke.

<key>Program</key>

<string>/opt/local/bin/wget</string>

<key>ProgramArguments</key>

<string>http://drupaldev1/cron.php</string>

http://developer.apple.com/library/mac/#documentation/Darwin/Reference/M...

That's not actually true.

That's not actually true. See the man page for launchd.plist
If the Program key doesn't exist, the first element of ProgramArguments is used.

Program
This key maps to the first argument of execvp(3). If this key is missing, then the first element of
the array of strings provided to the ProgramArguments will be used instead. This key is required in
the absence of the ProgramArguments key.

Multiple arguments... Thanks

Multiple arguments...

Thanks for the tips. I wanted to build a screensaver for my fancy new machine (4 cinema displays!) by grabbing images from various webcams around the net and having the screensaver display them. Your approach gets me most of the way there, but I learned one more important thing that others might find useful:

The arguments presented in the ProgramArguments stanza for Launchd cannot contain any spaces. So, if you want multiple arguments to wget, you have to put them each in their own string. Here's what I used:

<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/wget</string>
<string>-P~/Pictures/webcams</string>
<string>-nH</string>
<string>-q</string>
<string>--cut-dirs=9</string>
<string>-p</string>
<string>-i~/webcam-list</string>
</array>

A couple of other useful things: by default errors get logged to /var/log/system.log which is a good place to look if things aren't working right. And launchctl start your.service.name fires off the service immediately so that you can see if it is working correctly.

If you want a screensaver of updated webcam shots, you can use the above, along with a one-url-per-line list of target URLs in ~/webcam-list which creates a directory ~/Pictures/webcam that updates those files once per minute. Open the screensaver system preferences, hit + to add that directory to the list and select it, and you have current webcam pictures for your screensaver.

Thanks for the tips!

um, what's wrong with using

um, what's wrong with using "crontab -e"? Seems to work just fine on my 10.5 install. The cron man page is a bit misleading, as it says cron only runs when /etc/crontab exists, but that's not quite the case...

All I can say is when I first

All I can say is when I first tried using "crontab -e" it never took effect. If you use it and it takes effect, that's much preferred, and you can completely ignore this post.

Thanks for this tip!

Thanks for this tip! Unfortunately in 10.6 ~/.launchd.conf is unsupported (see man page for launchd.conf).

Luckily, placing the plist file in ~/Library/LaunchAgents seem to be enough, as it loads on startup.

For those that wonder what

For those that wonder what the 300 means. It means every 5 minute (e.g. 5 * 60 == 300).
StartInterval
300

Thank you for sharing this info.

Very useful post. I am not

Very useful post.

I am not affiliated to this in any way but also found a sourceforge app called "lingon" very useful. lingon.sourceforge.net

No longer supported by the developer I'm afraid, but very useful for the plist creation bit, if like me you find it a bit much when you've never done one before. Has a section that lets you view the xml results of the GUI options you choose.

Once you have created the plist you can just edit it with your plain text editor as mentioned above. Rememver to unload / load as described here (or reboot) after each edit.

My aim was to run an osascript and / or sh file via launchd. Make sure you download v2.1 for 10.5 which is 'not' the default download from the link above.

Found this v. helpful,

Found this v. helpful, thanks.

very helpful, thanks for

very helpful, thanks for posting this.