Subhashmohan's Blog

Rails crontab with “whenever” gem

Posted on: March 27, 2010

Introduction

gem “whenever“  provides you with the valid cronjob syntax and also writes / updates the crontab file for you.

Installation

1.  Install the gem

gem install whenever

2. Add below line to your config/environment.rb

config.gem ‘whenever’, :lib=>false, :source=>’http://gemcutter.org’

3.  Go to your application root directory and run the below command

wheneverize .

   This will create a “config/schedule.rb” file for you.

  EXAMPLE “config/schedule.rb”

  set :environment, :development

  set :path, "/var/www/MyApp"

  # Scheduled Hourly

   every 4.hours do
   command "/usr/bin/your_command"
   end

  # Scheduled Daily

   every 1.day, :at => '12:00 am' do
   runner "MyModel.yourmethod"
   end

   # Scheduled Weekly

   every :monday, :at => '11:00 pm' do
   rake   "your:rake:task"
   end

Consider the above as the contents of your config/schedule.rb file.

4.  Now from application root directory, run the below command

whenever

The above command will output valid syntax for the crontab as below.

 0 0 * * * /var/www/MyApp/script/runner -e development “Model.your_method”

 0 0,4,8,12,16,20 * * * /usr/bin/your_command

 0 23 * * 1 cd /var/www/MyApp && RAILS_ENV=development /usr/bin/env rake your:rake_task
 

5. Now, To write the above content in your Crontab file run the below command.

whenever –update-crontab

you can see the above entries in your crontab file by running “crontab -e”.

That’s it, you are ready to go.

 




4 Responses to "Rails crontab with “whenever” gem"

Hi,
Where can we check log errors if there are occured any erros in our ruby program

You can add the below line to top of your whenever file, Please refer this link http://lostechies.com/derickbailey/2011/04/27/the-whenever-gem-making-cron-easy/ for more information.

set :output, { :standard => “/var/logs/my_app.log”, :error => “/var/logs/my_app.errors.log” }

hi subhash i am trying to run crontab using wheneverize but my requirement to run a ruby file which is there in app/controllers/cronjob.rb
i want to run the about file called cronjob.rb every morning 11 am. i could not run the cronjob properly.
i have written this way
every 1.minutes do
runner “cronjob.rb”, :environment => “development”
command “echo ‘you can use raw cron syntax too'” #, :environment => “development”
end
but with your syntax runner “MyModel.yourmethod” what does this line do. how can i run a ruby file which i mention above which is a path.

thanks man , this is the most perfect blog to get whenver gem running.
no one mentions whenever –update-crontab step and without that nothing gets working. Thanks again man.

Leave a comment

Blog Stats

  • 4,305 hits
March 2010
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
293031  

Categories

Pages