Last Updated: February 25, 2016
· mockra

Using Rake for Cron Jobs

Rake is a great tool in general, and can even be used for running cron jobs. You can use Ruby’s Time class to create rules for when you want your tasks to be run. Here’s an example of how you can setup your lib/tasks/cron.rake file.

task :cron => [ :environment, :email ] do

task :email do
  if % 6 == 0
    # Send Email

You can then setup crontab to run your rake file every hour, or however often you choose. Here’s an example for setting up crontab to run your file every hour:

0 * * * * bash -c 'cd rails_path; bundle exec rake cron'