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.plistIf 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.plist4. 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.plist5. 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
For those that wonder what
For those that wonder what the 300 means. It means every 5 minute (e.g. 5 * 60 == 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.
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.