Wednesday, September 01, 2021

Re: Run a command on "last day of month"

On 01/09/2021 13:02, Nick Holland 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.
>

I would go the other way and check tomorrows date. If it is "01", then I
know today is the last of this month:

date --date="tomorrow" +%d
02

--
Thanks and regards

Goetz R Schultz

Quis custodiet ipsos custodes?
---------------------------->8------------------------------
/"\
\ / ASCII Ribbon Campaign
X against HTML e-mail
/ \
---------------------------->8------------------------------

---------------------------->8------------------------------

/"\
\ / ASCII Ribbon Campaign
X against HTML e-mail
/ \

This message is transmitted on 100% recycled electrons.

---------------------------->8------------------------------

Please support these causes:

https://you.38degrees.org.uk/petitions/rescue-lanes-on-multi-lane-roads

https://www.justgiving.com/crowdfunding/fylde-cfr-team

----------------------------8<------------------------------
Unsigned message - no responsibillity that content is not altered

No comments:

Post a Comment