Ruby Whenever and Day of Month Crontab Syntax
Whenever by Javan Makhmali is an excellent way to manage the cron tasks that relate to your ruby app. It provides a very powerful and readable ruby syntax for planning a job schedule.
I had a hard time figuring out how to recreate the case of running a job on a specific day per month, however. The following crontab line is an example of running a job on the first day of month every month at 7:13am:
# .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * command to be executed 13 7 1 * * my_funky_command
The key to understanding the possible syntax is understanding that chronic is whenever’s underlying natural date and time parser. So the whenever syntax for the above cron definition is most elegantly:
every 1.month, :at => 'start of the month at 7:13am' do runner 'my_funky_command' end
Running a job on days other than the beginning or ending of the month is not so straightforward. This GitHub issue explains it best. To running the same command every month at the same time, but on the 15th of the month:
every 1.month, :at => 'January 15th 7:13am' do runner 'my_funky_command' end
‘January’ will be ignored as whenever parses the monthly job into its pieces.