We have a detailed instruction of using crontab on Linux system. This tutorial will help you to configure the cron job to run on every last day of the Month. As there is no direct option available to do it.
Identify Last Day of Month
So, first of all, we will schedule cron on the 28,28,29 and 31’st of each month. Now find out if today is the last day of the month. To find it check if the next day is 01’st of the next day and then only execute any command.
Below command will return the date of the next day.
date +%d -d tomorrow
Now check if tomorrow is 01.
[ "$(date +%d -d tomorrow)" = "01" ] && echo "True"
If the next day is 01 then above command will print “True” on screen. Here you can use the above script in crontab and change echo with your command.
Schedule Cron to Run Last Day of Month
Use the following cron format to run a cron at last day of every month. Here you can change the hours, minutes and script to be executed by cron.
59 23 28-31 * * [ "$(date +%d -d tomorrow)" = "01" ] && /root/script.sh
2 Comments
All correct except that you have to escape the %d.
59 23 28-31 * * [ “$(date +\%d -d tomorrow)” = “01” ] && /root/script.sh
Yeah I was wondering why my jobs weren’t running. Ringo is correct.