Web Developer Class: Cron and PHP
How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017
PHP is a powerful language by any standard, and it provides many tools that are very distinct and suited to specific tasks. Today we are going to look at the CRON TOOL - and more specifically how to use it. There are many cron tools to help with the scheduling of tasks, but as this article is developer oriented, we'll investigate from a command-line \ crontabs perspective.
If you have never heard of CRON before, it's a very powerful LINUX module that allows you to run specific tasks or scripts at specific times or intervals. As we all know, you cannot usually run a PHP script without actually browsing to a specific URL that will trigger that script. Cron, however, is a lot smarter. You tell it when to run something (6PM everyday, perhaps) or how often (every 15 minutes, for example), and at those times it will run the script until it's done, then it will stop, and sleep until the next time that it has been told to do something.
What you actually do in the script that runs does not really matter, although there are a few guidelines to adhere to as the cron module does not behave like a browser.
Crons are very good at sending reminders to your clients, for example. Imagine your clients buy something on your website that only lasts 14 days. So, every day the cron runs a script that scans through your orders, checks which customers bought something at least 12 days ago, and then if needed, runs a script that sends them an email or SMS reminding them that whatever they bought is about to run-out and they need to refill it. I'm sure that you can think of at least ten more applications where this nifty little module would be handy.
Let's pretend you own a website that is very busy and sells a lot of stock every day. Seeing as you are a very busy person, you would like the website to provide you with an daily report of what you sold, how many items you sold, what your total sales and estimated GP was for the day.
To do this, we will need to run a cron - perhaps every day at 8AM, 1PM and 4PM, to tell us this info. Obviously the first thing we need is a script to run, so let's do the following:
<?php //sales_report.php include 'check_sales.php'; mail('firstname.lastname@example.org','Daily Sales', $html_data, 'From:email@example.com');
In the code above, the variable $html_data will be the HTML report that is generated by the check_sales.php script, and its content is not in the scope of this article.
Now we can setup an automated running of the check_sales.php script. We set the intervals from within four directories that sort the jobs into hourly, daily, weekly, and monthly intervals.
The easiest way to run crontabs is with the crontabs command:
# crontab -e
This command sets the crontab up for editing. After entering this command on your command-line, you will be able to enter the commands that you wish to run. Be very weary of the syntax - it is very specific and if you get it wrong nothing will work: the syntax should be:
minutes hours day_of_month month day_of_week command
The variables above are all numerical constants - except for the command itself. There is also an asterisk (*) allowed - which, as in most programming languages, denotes the WILDCARD that allows any value. We can also separate multiple values with a comma. See the associated values below:
Minutes: 0-59 Hours: 0-23 Day_of_month: 1-31 Month: 1-12 Day_of_week: 0-6
So, if we want to run a script every Monday morning at 9:25 AM, our cronjob file will contain the following content on a single line:
25 9 * * 1 /path/to/scriptname
Simple enough? Well, not so fast. PHP will not run like this unless it has been specifically compiled to do so.
What we need to do is tell PHP to run the cron script, so, we need to say:
25 9 * * 1 php /path/to/scriptname
or, if you have wget installed on your server
25 9 * * 1 wget /path/to/scriptname
And now we can finally wrap up our crontab file by saying:
0 6,11,16 * * 1,2,3,4,5,6,7 php http://www.example.com/check_sales.php
In this article we have seen the basics of CRONTABS. These are definitely useful tools that a professional developer will find extremely useful as his development work becomes more complex and requires more functionality that cannot always be provided "on the fly".
Until next time.