How To Run Cronjob Script On The Last Day Of a Month

In Linux and Unix-like operating systems, you can run a cron job on the last day of the month by using the following syntax in your crontab file:

59 23 28-31 * * [ "$(date +\%d -d tomorrow)" == "01" ] && /path/to/script

This cron job will run on the 28th, 29th, 30th, and 31st of the month at 11:59 PM. The [ "$(date +\%d -d tomorrow)" == "01" ] part of the expression checks if the next day is the 1st of the month, which would indicate that today is the last day of the month. If this condition is true, the script at /path/to/script will be executed.

To add this cron job to your crontab file, you can use the following command:

crontab -e

Then paste the above line at the end of the file and save the changes. The cron job will take effect immediately.

Note: The crontab file is specific to each user. If you want to run a cron job as a different user, you’ll need to log in as that user and add the cron job to their crontab file.

Leave a Comment