May 09, 2013

I think cron is a wonderful creation. It is simple in what it does while being extremely useful. Allow me to present a short introduction to it for those unfamiliar, then I will show you a handy trick you may need someday. Keep reading this blog to learn about cron and the crontab command — and why you should run crontab at the end of the month.

Brief Introduction to Cron & Crontab

You can set any command to run at any time (or repeatedly at a set interval) by using cron. The name comes from “chronos” — the Greek word for time. If you have Linux, you’re going to have cron. Run the command “crontab” to edit or create the jobs for your user.

Related reading: Cron Job for Every Three Days

The syntax seems a little cryptic at first, but it is very straightforward. You enter a series of numbers and symbols followed by the crontab command you wish to run. The numbers and symbols state the desired time and day to run the command. They are separated by a space as follows:
minute hour day month day-of-week

So this entry:

5 13 * 3 * execute_this

will run the crontab command “execute_this” at 1:05 pm every day in March only. Changing it slightly:

5 13 * 3-5 * execute_this

will run the same crontab command at the same time but now it will run every day in March, April, and May.

As I said, this was a short introduction to cron and the crontab command. If you don’t know what you’re doing here, you may not want to go testing on any important machine. Go research and play around (it’s the best way to learn).

Try optimized Magento hosting from Nexcess

Setting a Crontab For the Last Day of the Month

So, how do you make a crontab to run on the last day of the month? To get us started, here’s how it would look to run on the first of each month (at 1:05 pm again):

5 13 1 * * execute_this

That “1” in there means when the date is “1”, which is the first day of the month. The problem is we don’t have a number to put there which means the last day of the month. It could be 30, 31, 28, or sometimes even 29. But one thing is always true about the last day of the month, tomorrow is the first!

Let’s use that information to create this entry:

59 23 28-31 * * execute_this

I’ve made this one run just before the day was over (11:59 pm) but it could be any time you need. And it will run on each of our possible end of the month dates (28-31), every month.

Obviously, we don’t want our code to execute on October 28. That’s not the last day of the month. The date should be 31 if it’s October, but more importantly (and more reliably) tomorrow’s date should be 1. It’s time to pull out our shell scripting superpowers.

[bash]date +%d[/bash]

will give you the current date as a two character string. But since we want to see what tomorrow is, we ask for that instead

[bash]date +%d -d tomorrow[/bash]

If that matches “01” then we know tomorrow is the first so today must be the last.

[bash][ "$(date +%d -d tomorrow)" = "01" ] && execute_this[/bash]

Our test (the square brackets) will result in either true or false. It will run that test AND (&&) the next command which is the command we set out to run in the first place. But due to the way computers operate, if the test comes back false, it won’t bother running the second command.

So only when that test comes back as true will it run our second crontab command. This is exactly what we wanted and we have just tricked the computer into doing our evil work. Congratulations!

Here is what we enter in crontab to have our command run at the very end of each month:

[code language="bash"]59 23 28-31 * * [ "$(date +%d -d tomorrow)" = "01" ] && execute_this[/code]

Explore the possibilities of Magento with Magento Hosting From Nexcess.

Robert
We use cookies to understand how you interact with our site, to personalize and streamline your experience, and to tailor advertising. By continuing to use our site, you accept our use of cookies and accept our Privacy Policy.