[Ruby] Last day of month in whenever gem



I'm using whenever gem for scheduling my own rake tasks:
https://github.com/javan/whenever

Sometimes it's useful to schedule a task the last day of the month.
The gem does not provide such alias.

The solution is the following:

job_type :only_last_day, "[ \"$(/bin/date +%d -d tomorrow)\" = \"01\" ] && cd :path && :environment_variable=:environment bundle exec rake :custom_rake_task"

every '30 10 28-31 * *' do
  only_last_day "custom_task", :custom_rake_task => "your_rake_task_name"
end


  • 30 10 28-31 * * means: try to launch the script every month at 28th,29th,30th,31th day at 10:30 am
  • if current date plus one equals 01 it means today is the last day of the month therefore the script is eligible to be run

Nessun commento: