View Tutorial Metadata Edit Content Revision History Add to Watchlist Add New Tutorial How to set a cron job to run at the end of the month

How to set a cron job to run at the end of the month

There are times when you want to run a script/application in the last day of every month (for example, you need to generate a report for the month passing by). To do this in Linux you can use the crontab application. A short introduction for crontab and a tutorial on how to edit the crontab file can be read here : Introduction to crontab .

So, the last day of the month can be 28 (February), 29, 30 or 31. What do they all have in common?

Simple .. after the last day of the month ( no matter what the month is ) comes 01 as the first day of the next month. So, the easiest solution is to check if the next day is 01 :)

Also, we take in consideration how the && (AND) operator is working .. if we have an expression like (exp1) && (exp2) && (exp3) then if the first expression is false exp2 and exp3 aren't evaluated anymore.

The algorithm of setting the cron job became simpler knowing this information :

59 23 * * * [ `date -d tomorrow +%d` -eq '01' ] && php
/var/www/crons/monthly_report.php

which translates in this : run every day at 23:59 and if tomorrow is the first day of a month execute our script too.

Only plain text supported.

Optional

Required - will be kept private

Optional

 
 

Rating: (0+, 0-) In: Linux