Whenever : Cron tab : Environment selection

For whenever gem Default Rails environment is production. However, you sometime would like to set environment manually for testing purposes.

If you try

$ whenever -h
Usage: whenever [options]
 -s, --set [variables] Example: --set 'environment=staging&path=/my/sweet/path'

if you need to set environment to development then try this

$ whenever --update-crontab --set 'environment=development'

 

# In schedule.rb
# run this task only on servers with the :app role in Capistrano
# see Capistrano roles section below

# variable `environment` is accessible here; you set from CLI
set :whenever_environment, environment || 'production'

every :day, :at => '12:20am', :roles => [:app] do 
  rake "app_server:task" 
end

You can list installed cron jobs using crontab -l.

 

Cron jobs in Rails : Whenever gem or Scheduler in Heroku

To use autotriggered background processes in Ruby On Rails, we normally user gem like ‘Whenever‘. Its very easy to use.

Using single command like `whenever -i` will update your cron tab. To see your current Cron status you can simply use command like `whenever -l` or `crontab -l`.

Cron In Heroku

Continue reading

Mostly used cron expressions with Meaning

Expression Meaning
“0 0 12 * * ?” Fire at 12pm (noon) every day
“0 15 10 ? * *” Fire at 10:15am every day
“0 15 10 * * ?” Fire at 10:15am every day
“0 15 10 * * ? *” Fire at 10:15am every day
“0 15 10 * * ? 2005” Fire at 10:15am every day during the year 2005
“0 * 14 * * ?” Fire every minute starting at 2pm and ending at 2:59pm, every day
“0 0/5 14 * * ?” Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
“0 0/5 14,18 * * ?” Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
“0 0-5 14 * * ?” Fire every minute starting at 2pm and ending at 2:05pm, every day
“0 10,44 14 ? 3 WED” Fire at 2:10pm and at 2:44pm every Wednesday in the month of March.
“0 15 10 ? * MON-FRI” Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
“0 15 10 15 * ?” Fire at 10:15am on the 15th day of every month
“0 15 10 L * ?” Fire at 10:15am on the last day of every month
“0 15 10 ? * 6L” Fire at 10:15am on the last Friday of every month
“0 15 10 ? * 6L” Fire at 10:15am on the last Friday of every month
“0 15 10 ? * 6L 2002-2005” Fire at 10:15am on every last Friday of every month during the years 2002, 2003, 2004 and 2005
“0 15 10 ? * 6#3” Fire at 10:15am on the third Friday of every month
“0 0/5 * * * ?” Fires every 5 minutes
“10 0/5 * * * ?” Fires every 5 minutes, at 10 seconds after the minute (i.e. 10:00:10 am, 10:05:10 am, etc.).
“0 30 10-13 ? * WED,FRI” Fires at 10:30, 11:30, 12:30, and 13:30, on every Wednesday and Friday.
“0 0/30 8-9 5,20 * ?” Fires every half hour between the hours of 8 am and 10 am on the 5th and 20th of every month. Note that the trigger will NOT fire at 10:00 am, just at 8:00, 8:30, 9:00 and 9:30.