Last updated September 18, 2009. Created by Panchobook on April 15, 2009.
Edited by bekasu, add1sun. Log in to edit this page.
These are instructions for setting up a cron job on your localhost using MAMP on a Mac.
- In Terminal type
crontab -e. This allows you to add a cron job, using the vim editor. - Press
ion your keyboard to go into vim's insert mode. - Type in you cron command. For example, to run at minute 5 of every hour using curl, use:
5 * * * * /usr/bin/curl --silent --compressed http://localhost:8888/cron.php
But to run every 5 minutes, use:*/5 * * * * /usr/bin/curl --silent --compressed http://localhost:8888/cron.php
Note that if you are not using MAMP's default 8888 port, you should leave that off. - While in insert mode you can use the arrows and delete keys as you would normally.
- Press escape key to exit vim's insert mode.
- Type
ZZ(Must be capital letters -- saves the file and exits crontab). - Verify the cron job details by typing
crontab -l(that's a lower case L) at the terminal prompt.
I recommend that you first try setting the job to run every 2 minutes (with */2), so that you can check, reasonably quickly, that it is running. Then edit crontab again and change the curl command to your preferred time.
Non-Vim althernative
For those with a reservation against Vim (or any other command-line editor), you can do it like this:
- Create a document with your editor of choice.
- Add the cronjobs you want to add, like described above.
- Save the file as "mycron.txt" on your desktop (or any other place you like).
- Open up a Terminal window, and go to the desktop ( cd ~/Desktop ).
- Type in crontab mycron.txt
- Type in crontab -l to verify that your commands were added to cron.
Note: This method will overwrite the entire crontab, so keep the mycron.txt file available. When you need to add or change cronjobs, just edit that file and repeat the crontab mycron.txt command to update your cronjobs.
Comments
MacOS X >10.5 uses launchd instead of cron
MacOS X after 10.5 no longer seems to use cron and has replaced it by launchd. This uses plist files to register jobs to run. To access cron.php using launchd place the following file in /Users/your_account/Library/LaunchAgents/com.your_site.drupal_cron (create the directory if necessary):
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.your_site.drupal_cron</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/curl</string>
<string>--silent</string>
<string>--compressed</string>
<string>http://localhost:8888/cron.php</string>
</array>
<key>StartInterval</key>
<integer>300</integer>
</dict>
</plist>
This will then get loaded when you login and will visit the cron.php page every 300 seconds (5 minutes). To make it take affect you need to logout/login or use the terminal to run launchctl to load it immediately:
launchctl load /Users/your_account/Library/LaunchAgents/com.your_site.drupal_cronIf you need to change the plist file you can unload and reload it again with:
launchctl unload /Users/your_account/Library/LaunchAgents/com.your_site.drupal_cronlaunchctl load /Users/your_account/Library/LaunchAgents/com.your_site.drupal_cron
(If you have XTools installed you can use /Developer/Applications/Utilities/Property List Editor to create plist files.)
Help creating your plist file
This works great for me thanks. For others new to this. I had to dig around for this. Hopefully it will save someone time.
If you're using Text Edit, make sure you 'Save As' and use plain text encoding > UTF-8. then follow the instructions.
http://www.macosxhints.com/article.php?story=20050430105126392
MAMP - CRON - virtual hosts
Hi there,
I've set up different hosts on my MAMP server, so that I can access my Drupal sites at these adresses:
http://mysite1.localhttp://mysite2.local
When trying to use this plist file, I get no result. Any idea ?
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.mysite1.drupal_cron</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/curl</string>
<string>--silent</string>
<string>--compressed</string>
<string>http://mysite1.local/cron.php</string>
</array>
<key>StartInterval</key>
<integer>300</integer>
</dict>
</plist>
Thank you in advance.
Excellent!
But what is the = for crontab -l now in 10.6.4? cheers!
From www.phoenixstudios.com.np, A Small A/V Studio in Kathmandu Nepal
OSX 10.6.6 and MAMP PRO
The crontab instructions above work for me using OSX 10.6.6, but I've made one modification that works with virtual hosts on MAMP.
Using MAMP PRO, I create virtual hosts for every dev site, each of which runs as part of a multisite installation of drupal. So, for example, the virtual hosts http://dev_site1 and http://dev_tsite2 both point to something like /Applications/MAMP/htdocs/drupal6.
I then follow the instructions above to run cron on each site using these commands:
*/20 * * * * /usr/bin/curl --silent --compressed http://dev_site1/cron.php
*/20 * * * * /usr/bin/curl --silent --compressed http://dev_site2/cron.php
This runs cron every 20 minutes. You can change that to whatever you want, of course.