Scheduling PHP URLs With Cron
PHP scripts generally can be accessed with a browser by typing the script's URL. When the page loads, the PHP script runs.
When a PHP URL has to run every so often, the URL can be scheduled with cron.
Here are examples of pages that may be set up to run on a schedule. In each instance, there is PHP code in the page that accomplishes the desired task.
-
Data in a certain MySQL table sent hourly to the site owner's email address.
-
Grab data from a remote website on a specific schedule, such as weather data twice daily or a stock report at specific hours.
-
Run site monitoring software like Files Monitor.
-
Every day, run a PHP URL to calculate the sizes of certain log files and email it to you.
-
Load a certain web page every 5 minutes to test a page load counter.
-
Daily website statistics compilation.
Scheduling a URL with cron requires a special technique. Instead of specifying the PHP script's directory location on the server, its URL is specified.
Further below, after the cron instructions, is the source code of a PHP web page to determine the size of a log file and email the number to the site owner. It can be used as a working example of how useful scheduling URLs can be.
When it is scheduled to do so, cron retrieves the page at the URL, like a browser would. Any PHP code within the page is run at that time.
I'll show you 3 ways to set up a PHP URL for a schedule with cron.
The 3 ways are different ways to specify the cron command.
- GET http://...
- wget http://...
- curl http://...
Most reliable hosting companies provide at least one of the 3 ways. Some provide all three. In following instructions, we'll test your hosting account to see which of the 3 you can use.
The instructions are for cPanel.
If your hosting account does not have cPanel, the directions for the "Command" text field in cPanel may be applied to however the cron command is specified within the system you are using.
Cron Scheduling Instructions for http://... URLs
In these scheduling instructions, we'll test to determine which of the 3 methods work for your hosting account.
Log into cPanel and click "Cron Jobs".
There are a lot of icons on the cPanel home page. If the Cron Jobs icon is hard to find, type "cron" in Find box.
Temporarily receiving email confirmations
While testing the three methods, have cron send you an email every time it runs a scheduled command. At the Cron Jobs page, in the Cron Email section, specify an email address in the "Email:" field and click the "Update Email" button.
After testing is done, the email address can be removed for a less cluttered email box. Simply click the "Update Email" button with a blank "Email:" field.
Setting up cron schedule tests
Let's first test to see which of the the 3 methods work for your hosting account.
Decide on 3 URLs for testing, one for each method to be tested. They can be any web page URLs. If you wish to create special testing pages, go ahead and do that now (they don't have to be .php pages, .html works just as well for the test).
As an example, let us suppose these are the URLs associated with each method:
- GET http://example.com/page4GET.php
- wget http://example.com/page4wget.php
- curl http://example.com/page4curl.php
At the Cron Jobs page, in the Add New Cron Job section, do this for each of the above three methods:
-
Click the "Common Settings:" dropdown and select "Every 5
minutes ...". -
In the "Command:" field, type the command for one of the three methods. For example: GET http://example.com/page4GET.php
-
Click the "Add New Cron Job" button.
You'll need to return to the Cron Jobs page after adding each new cron job.
Monitoring the test
The emails you receive every 5 minutes let you know which of the three schedule URL cron methods work for your hosting account.
If more than one method works, select the one you prefer. I generally use the GET http://... method.
After the test
Now that the test is completed, remove the 3 cron schedules used for the testing.
At the Cron Jobs page, in the Current Cron Jobs section, click the "Delete" link for each of the testing schedules.
Then, remove the email address at the Cron Email section. Unless you wish to keep it there to continue to be notified whenever a scheduled cron command kicks in.
Scheduling Real Cron Commands
To schedule real cron commands, decide which of the methods to use – one of the methods that work for your hosting account.
Set up the cron job like you did the test, except use the real http://... URL instead of a testing URL.
An Example
As a working example of how useful scheduling a URL can be, this PHP web page determines the size of a log file and emails the number to the site owner.
<!DOCTYPE html> <html> <body> <?php // Two places to customize. // // 1. Specify the location of the log file. $LocationOfLogFile = "/directory/log.txt"; // 2. Specify the email address to send the log file size to. $SiteOwnerEmailAddress = "name@example.com"; // End of customization. // $FileSize = filesize( $_SERVER['DOCUMENT_ROOT'].preg_replace('!^/*!','/',$LocationOfLogFile) ); mail($SiteOwnerEmailAddress,"File size [$FileSize]","File size of $LocationOfLogFile is $FileSize bytes","From: $SiteOwnerEmailAddress"); echo "DONE (file size $FileSize)"; ?> </body> </html>
Copy the above and save it as file filesize.php, then customize it. Specify the location of the log file to obtain the size of and the email address to send the size to.
Upload filesize.php to the server and make a note of its URL.
Type its URL into your browser. When the page loads, the PHP coded in the page is run. Verify the email arrives with the size information. (Email generally is pretty fast, but sometimes is slow and might even take up to 30 minutes to arrive.)
Now, specify the URL of filesize.php to run every day, every hour, or any other schedule possible with cron.
An Alternative
Instead of specifying many cron jobs, an alternative is Scheduled Hourly URL Launch (a WebSite's Secret exclusive).
Scheduled Hourly URL Launch is a CGI script that accepts any http://... URLs to be launched. There is a box for every hour where one or more URLs may be specified.
Set up one cron job to run Scheduled Hourly URL Launch every hour. Thereafter, simply update the Scheduled Hourly URL Launch control panel to add or remove URLs.
Will Bontrager