Written by Sean Behan on Sun Jun 17th 2012

First off you'll need to edit your cron file. Normally, the cron files are kept under /etc/cron.daily or /etc/cron.hourly but we can just use the command line tool, crontab and pass it the -e flag, so that we can edit the file without any fuss.

sudo crontab -e
If this is the first cron you're installing you should see a blank file or something that looks like this
# m h  dom mon dow   command
Essentially this is telling you the order of the arguments you need to specify. Each asterisk represents an interval of time. In this order they are

Minute, Hour, Day of Month, Month, Day of Week and then the path to your command. So to set up our cron to run once every five minutes we'll write

*/5 * * * * /path/to/my/script
It's important to note that it is */5 and not just 5. The interval for minute is 0 - 59, meaning that if you just enter a 5, you'll run your cron job only once an hour but at the 5th minute of that hour. To specify that we want to run it every 5 minutes we need the */5.  Also, note that the cron will execute your command via the path. If you need an interpreter like ruby, rake or php, don't forget to put that as a part of your command. For instance this will set a rake task to run every five minutes on a Ubuntu box.
*/5 * * * * cd /var/www/my_ror_application  && /usr/bin/rake RAILS_ENV=development db:migrate
You can see that my command uses cd, changing directories to my application. I then specify the full path to the rake program, /usr/bin/rake and give it arguments, including the environment and then of course, the task I want to execute. In this case I'm migrating the database. Which is obviously pointless. Maybe you'll want to email forum news or send out activation emails.

For more information here is a great reference mkaz.com


Tagged with..
#cron #how to #Rails #rake #Programming #Ruby on Rails

Just finishing up brewing up some fresh ground comments...