now available

now available
Want fresh tech tips in your inbox?

Set Cron Job to Run Every Five Minutes for a Ruby on Rails Rake Task


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 w/ #cron #how to #rails #rakeprogramming #ruby on rails

Apps I've Built

XPath Expression Editor
Practice and improve your XPath skills with XPath Editor
Click to buy on the App Store
Photo Location Changer
Easily change the location on your photos and videos
Click here for more info about the app
Photo Date Changer
Easily change the dates and times on your photos and videos
Click here for more info about the app
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app