I\'m trying to get familiar with cron jobs, and I think I get the basic idea (scheduling, syntax, etc), But, I can\'t seem to get it right on my mac with Terminal - where ex
launchd is powerful, but you really don't want to write the plist yourself. Get Lingon. It's an open-source, really well-designed GUI for creating and managing your system's launchd tasks.
You no longer want to be using cron. As others have already stated, it has been replaced by launchd and launchd is clearly going to be the future on Mac OS X.
MacTech Magazine has recently been doing a series of articles on launchd and I would highly recommend reading them. I know I have certainly learned a lot.
September, 2009 (Volume 25, Issue 9) 25.09 MacEnterprise: launchd for Lunch
October 2009, (Volume 25, Issue 10) Snow Leopard, Launchd, and Lunch More launchd recipes, and a look at changes in Snow Leopard
There have been other articles in MacTech and I would suggest searching their site.
Cron has been replaced by launchd since 10.4. You should probably write your tasks using this unless you plan on porting them to Linux/Unix systems at some point.
If you do decide to go with cron anyway, try typing crontab -e
or sudo crontab -e
. These will give you different crontab files, the former for the user you're currently running as and the latter for the root user.
"Hitting" a URL can be accomplished a lot of ways. Depending on the local script that you are using to "hit" it, you could use some of the language's built-in methods/classes. For instance, a Ruby script would use net/http but you could try curl as well if you're just writing a bash script. Do man curl
to find out more, but the basic command is just curl http://google.com
.
To get started with launchd (instead of cron) you'll want to first create an empty .plist
file, for example local.mytask.plist
and put it somewhere. ~/Library/LaunchAgents
is probably a good place. Open that in text editor and copy in the code below
<?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.mytask</string>
<key>ProgramArguments</key>
<array>
<string>/opt/local/bin/wget</string>
<string>http://someserver/somepage.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>
Then "activate" the file from the command line:
sudo launchctl load /Users/my_username/Library/LaunchAgents/local.mytask.plist
To make it load automatically, create a ~/.launchd.conf
file with the same line (minus sudo launch
)
load /Users/my_username/Library/LaunchAgents/local.mytask.plist
The above instructions above have been copied from www.davidlanier.com and reposted here for your reference.
On the off chance that someone else fighting with cron on Snow Leopard stumbles across this, I will dredge up this old thread.
Yes, launchd is supposed to replace cron, but in fact it can't do certain things cron can.
Cron is not integrated well. If it sends a message, it ends up in /var/mail/user_name which of course Apple Mail knows nothing about.
crontab -e throws up saying 'temp file must be edited in place'. Apparently vim is not vi compatible. You can then do crontab "< /tmp/crontab.whatever" (look in /tmp and see what name is actually used) and it will end up in the right place and, assuming you didn't make a typo, will work.
Yes, it took a while to sort this all out :(
Type crontab -e
to edit your cron table and crontab -l
to list the current contents.. Type man 1 crontab
for more info on that command and man 5 crontab
for more info on the cron table file format.
For example, to download the stackoverflow page every day at 10:00a, run crontab -e
, enter this line, and then save/quit. The output will be written to a file in your home directory.
0 10 * * * /usr/bin/curl -s http://stackoverflow.com > ~/stackoverflow.html