Rails crontab with “whenever” gem
Posted March 27, 2010
on:- In: rails | ruby on rails
- 4 Comments
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 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.
1 | test87test
August 21, 2011 at 8:19 pm
Hi,
Where can we check log errors if there are occured any erros in our ruby program