views:

764

answers:

5

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 exactly do I find the Crontab? How should I reference the paths to scripts?

What I'm trying to do is hit a php script on a remote machine (http://...) - Is that possible at all?

+1  A: 

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.

Topher Fangio
cron isn't running by default on 10.4 and above, you'll have to enable it first. From memory the crond manpage shows how to do this.
sascha
+1 Good to know. The thought that it would be disabled didn't even cross my mind.
Topher Fangio
Last I read, cronjobs in OS X are implemented *via* launchd, so the difference might be moot...
Benjamin Oakes
@Benjamin Oakes - Interesting, I haven't dealt with it a whole lot, so I may have used the wrong terms in my answer (i.e. "replaced by"). Thanks for the clarification.
Topher Fangio
A: 

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
Ryan Bright
A: 

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"&gt;
<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&lt;/string&gt;
</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.

sascha
Thanks guys, all your answers were very helpful.I ended up using Launchd manually (Lingon was actually helfull with the syntax as well).Note that you can't use wget on a mac out-of-the-box, so I used curl instead:<array> <string>/usr/bin/curl</string> <string>-s</string> <string>http://myserver.com/myscript.php</string></array>
Adam
+1  A: 

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.

Ben
And while Lingon is still very useful and usable, note that the developer has announced that he has given up on further development of it.
Ned Deily
A: 

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.

ericgorr