Instructions and Examples
The Basics
(1) Use the set command to define the path to your application. This path will be prefixed to your script/runner and rake tasks that you define. If you’re using Whenever with a Rails application, the RAILS_ROOT global will automatically be used as your path so you only need to set a path if you wish to override it.
(2) Define an every block with the frequency that you wish the enclosed tasks to run. Technically, the frequency is defined in seconds, but for clarity it’s easier to define them with .minutes, .hours, .days, and .months.
(3) Inside your every block, define your command, rake, and runner tasks.
set :path, '/var/www/apps/my_app'
every 10.minutes do
runner "MyModel.some_process"
rake "my:rake:task"
command "/usr/bin/my_great_command"
end
Will get translated to:
0,10,20,30,40,50 * * * * /var/www/apps/my_app/script/runner -e production "MyModel.some_process"
0,10,20,30,40,50 * * * * cd /var/www/apps/my_app && RAILS_ENV=production /usr/bin/env rake my:rake:task
0,10,20,30,40,50 * * * * /usr/bin/my_great_command
Whenever defaults to production environment, but can be set like:
set :environment, :whatever
The environment can also be set for each individual task:
runner "MyModel.some_process", :environment => :whatever
Specific Times
With every blocks with a frequency >= 1.hour you can also supply an :at option to specify when, during that time period, the enclosed tasks should run. For example:
every 2.hours, :at => 23 do
command "/usr/bin/my_great_command"
end
Will get translated to:
23 0,2,4,6,8,10,12,14,16,18,20,22 * * * /usr/bin/my_great_command
Which means the command will run every 2 hours on the 23rd minute of each hour.
You can also set :at to a time in quotes. This is practical for tasks running >= 1.day. For example:
every 2.days, :at => '4:30am' do
command "/usr/bin/my_great_command"
end
WIll get translated to:
30 4 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31 * * /usr/bin/my_great_command
Which means the command will run every 2 days on the 4th hour at the 30th minute (4:30am).
