Last Updated: February 25, 2016
·
3.587K
· 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
end

task :email do
  if Time.now.hour % 6 == 0
    # Send Email
  end
end

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'