I need to create a CRON job that will run on the last day of every month.
I will create it using cPanel.
Any help is appreciated.
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Possibly the easiest way is to simply do three separate jobs:
That will run on the 28th of February though, even on leap years so, if that’s a problem, you’ll need to find another way.
However, it’s usually both substantially easier and correct to run the job as soon as possible on the first day of each month, with something like:
and modify the script to process the previous month’s data.
This removes any hassles you may encounter with figuring out which day is the last of the month, and also ensures that all data for that month is available, assuming you’re processing data. Running at five minutes to midnight on the last day of the month may see you missing anything that happens between then and midnight.
This is the usual way to do it anyway, for most end-of-month jobs.
If you still really want to run it on the last day of the month, one option is to simply detect if tomorrow is the first (either as part of your script, or in the crontab itself).
So, something like:
should be a good start, assuming you have a relatively intelligent
dateprogram.If your
dateprogram isn’t quite advanced enough to give you relative dates, you can just put together a very simple program to give you tomorrow’s day of the month (you don’t need the full power ofdate), such as:and then use (assuming you’ve called it
tomdomfor “tomorrow’s day of month”):Though you may want to consider adding error checking since both
time()andmktime()can return-1if something goes wrong. The code above, for reasons of simplicity, does not take that into account.