I'm looking around how to schedule a task for the last day of the month. But I find very twisted solutions like this:
[code language="bash"]59 23 28-31 * * [ "$(date +%d -d tomorrow)" = "01" ] && execute_this[/code]
There has to be a more readable solution!!!
You could break it down to make it a bit more readable like this:
You could also pass the date validation to a small script and run:
Some crons, like Quartz, also implement the "L" flag, which represents the last day of the month, and is used such that:
Quartz Font :Special-characters
I hope this helps you.
It is not a very twisted solution, in fact it is one of the most simple, complete, concrete and elegant.
Which means:
It's simple, it's
And the sequence of instructions is made up of two: the first is a validation that asks "Tomorrow is the day such that it is 01?", since if tomorrow is day 01, then today is the last day of the month
If it passes that instruction then it will return true and the following instructions will be executed.
That way you don't have to write everything @JoseVelasco did for the
cron
traditional part.The command
date +%d -d tomorrow
uses the commanddate
to get the "day after tomorrow" (-d tomorrow
) but with the numeric format of the number of the day (+%d
), so comparing that result with "01", we would be asking if " Tomorrow is the first of the next month ? " which is equivalent to asking "Is today the last day of the month ?".