Or, since last day of the month never occurs before the 28th, you
could run the script only on days which might be the last of the
month,

Also, since crontab does support a month column, you could have three
crontab entries: one for months with 31 days (month: 1,3,5,7,8,10,12),
another for months with 30 days (month:4,6,9,11), and another for
February. Then, you could either neglect the 29th of February, or you
could have your script do a year test (and the next year evenly
divisible by 4 which is not a leap year is 2100).

FYI,

-- 
Raul

On Wed, Sep 1, 2021 at 8:04 AM Nick Holland <n...@holland-consulting.net> wrote:
>
> On 9/1/21 5:50 AM, Joel Carnat wrote:
> > Hello,
> >
> > I would like to run a command on "the last day of each month".
> >
> >   From what I understood reading the crontab(5) manpage, the simplest way
> > would be setting day-of-month to "28-31". But this would mean running
> > the command 4 times for months that have 31 days.
> >
> > Is there a simpler/better way to configure crontab(1) to run a command
> > on "the last day of month" only ?
> >
> > Thank you,
> > Joel C.
> >
>
> Just run your script every day, and first thing in the script, check to see
> if it is the last day of the month -- and quickly exit if it isn't.  Very
> cheap to do and relatively easy if you know a good trick to do it.
>
> http://holland-consulting.net/scripts/endofmonth.html
>
> Find the last day of the month:
>     $ set $(cal)
>     $ shift $(($# - 1))
>     $ echo $1
>     30
>
> Compare to today:
>     $ date "+%d"
>     1
>
> rather easy, and fairly portable.
> You could probably stuff it into a one-liner in a crontab, but I would not
> recommend it.
>
>
> Nick.
>

Reply via email to