Setting a Cron Job to Run at the End of the Month

Posted on Updated on

This is a method to be able to run a cron job on the last day of month, as this is not easily supported.  This method will support any month (from 28 days to 31 days), although you could run this every day with no consequence.  It utilizes an expression that takes the current day (plus one day forward in the future) as a number minus the current day as a number.  If the value is less than or greater than 0, then the command will run.

For example, if today is the 31st of the month, the expression will add one day in the future, so the first number would be “1”, the second number would be “31” – therefore the value is -le 0, so the command will run.

0 23 28-31 * * [ $(expr $(date +\%d -d '1 days') - $(date +\%d) ) -le 0 ] && echo true
Advertisements

One thought on “Setting a Cron Job to Run at the End of the Month

    Rodnee said:
    June 23, 2014 at 5:37 am

    I once had a similar issue, but failed to come up with a solution for the last day of the month. There was a very simple answer for my particular case as all I needed to do was run the scripts on the first of the month. But your solution looks pretty nifty! Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s