aof8cw
Last Updated: February 25, 2016
·
2.112K
· mockra
6819148037d8894cf577537bc1d6a071

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'
Say Thanks
Respond