cron icon indicating copy to clipboard operation
cron copied to clipboard

Day zero in day-of-the-month field

Open markkulix opened this issue 4 years ago • 3 comments

If the day-of-the-month field values could be 0-31 instead of 1-31 you could use day zero to express the last day of the previous month. This would simplify a lot some tasks because like this you don't have to know (or calculate) how many days there are in the months.

So the entry 00 08 00 * * /usr/local/bin/budget-summary would execute 'budget-summary' on last day of every month at eight o'clock.

markkulix avatar Oct 30 '21 11:10 markkulix

the standard C library offers no way to learn the number of days in a month, though it has to know this information for its date/time functions. the number of days per month would be hard-coded inside of cron, along with leap year detection for february. this is not difficult but it runs contrary to the UNIX philosophy. offsetting this, we'd gain the ability to detect the error of referencing a day that's not present in some month, like February 31 or similar.

for month-end tasks, i always use day 1, since i want to include in my processing all log files from the previous month, including those still in progress on the last day of the month. therefore i have never wished for a feature like "day 0". do you have a real example of how this feature might be used? i ask, because 23:59 seems like a more useful time of day to run a monthly budget summary than 08:00. this may mean your example was contrived.

vixie avatar Nov 12 '21 16:11 vixie

Ok, maybe the example was contrived. But I'm not the only one who has interest on this. Searching the web I found several websites with solutions like

00 06 30 4,6,9,11 * myjob.sh 00 06 31 1,3,5,7,8,10,12 * myjob.sh 00 06 28,29 2 * myjob.sh

and the simplest way is propably this:

00 06 28-31 * * [ "$(date +%d -d tomorrow)" = "01" ] && ~/bin/payday

And there here seems to be implementations those accept "L" as last day (or "LW" for last weekday) but I'm not going to touch them :-)

But there certainly are things you must do on the last day of the month. Pay your rent, mortgage, visit your mother or parole officer, or check if you should end your Netflix subscription, calculate paychecks for your empoyees (ok, last business day of the month, but that's an other story...).

00 06 00 * * ~/bin/payday

This just would be neat, easy, beautiful, simple and nice way.

markkulix avatar Nov 19 '21 13:11 markkulix

markkulix wrote on 2021-11-19 05:56:

...

But there certainly are things you must do on the last day of the month. Pay your rent, mortgage, visit your mother or parole officer, or check if you should end your Netflix subscription, calculate paychecks for your empoyees (ok, last business day of the month, but that's an other story...).

00 06 00 * * ~/bin/payday

This just would be neat, easy, beautiful, simple and nice way.

if we don't expand on the proposal to include -1 and -2 and so on as the next-to-last day and so on, then it's simple and beautiful.

implementation would be hard to do simply of beautifully. internally, cron uses a bitmap to represent the job timings. this logic would have to change in order to allow a floating value. i'll study the matter.

vixie avatar Nov 19 '21 16:11 vixie

see [https://github.com/vixie/cron/pull/20]

vixie avatar May 29 '23 20:05 vixie

Ok, great! Thank you!

markkulix avatar Jun 03 '23 09:06 markkulix