zacker330
11/30/2014 - 2:35 PM

How to use whenever gem

How to use whenever gem

#install whenever
gem install whenever

#init whenever to your code base, will generate "./config/schedule.rb"
whenever .

#example schedule.rb
every 3.hours do
  runner "MyModel.some_process"
  rake "my:rake:task"
  command "/usr/bin/my_great_command"
end

every 1.day, :at => '4:30 am' do
  runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
end

every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
  runner "SomeModel.ladeeda"
end

every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
  runner "Task.do_something_great"
end

every '0 0 27-31 * *' do
  command "echo 'you can use raw cron syntax too'"
end

# run this task only on servers with the :app role in Capistrano
# see Capistrano roles section below
every :day, :at => '12:20am', :roles => [:app] do
  rake "app_server:task"
end
#run whenever
whenever --write-crontab <your_cron_name>

#stop your cron
whenever --clear-crontab <your_cron_name>

#As you see whenever gem is using crontab as it's cron process
crontab -e
crontab -l