PHP Scripts and Cron
(Note: See Scheduling PHP URLs With Cron for more information on the subject.)
There are gotchas for the unwary when trying to run a PHP script with cron.
Some hosting companies make it easy to set up cron to run the URL of a script (URL being the script's absolute http://... URL.) Others don't.
Note: If you are a WebSite's Secret member, instead of reading and applying the information in this article, consider installing Scheduled Hourly URL Launch. Set it up once and schedule many scripts with it.
Here are some of the gotchas.
Then, I'll provide a solution that will work regardless how your PHP is implemented or what extensions it has or does not have. Any PHP script that can be run in a browser can also be run with cron using this method.
One gotcha is that the PHP script needs to have 755 permissions (on Unix/Linux servers) to run it with cron. Then, something like this might work:
30 16 * * * /usr/bin/php -q /path/to/script.php
Verify the path to the the PHP installation is correct (/usr/bin/php in the example).
(The -q flag removes any page headers script.php responds with.)
If the above works for you, you won't need the solution later in this article.
The environment variables one expects to have available when script.php is run from HTTP (like a browser would) may not be available when run with cron. One of those is
If script.php uses $_SERVER['DOCUMENT_ROOT'], then the script will need to be modified to specify the full server path instead. Check also any included files, and files they include, and modify them if they use
An alternative to modifying scripts to accommodate absence of certain environment variables is to use wget or curl, provided one or the other is available on your system. Either of those run the script via HTTP. Thus, the environment variables are available to script.php just like they would be when the script is run in a browser.
Example cron entries:
30 16 * * * /usr/bin/wget http://example.com/script.php 30 16 * * * /usr/bin/curl http://example.com/script.php
Verify the path to wget or the path to curl are correct.
Another gotcha is if PHP is installed as an Apache module (instead of installed as CGI).
An Apache module installation of PHP can not be run with cron in the usual way. If you have wget or curl available on your system, it can be used. See above for examples.
A Solution That Will Work
This solution will run any PHP script as-is that already runs correctly in a browser.
It uses this Perl CGI script:
#!/usr/bin/perl # Specify URL of script to run. my $URL = "http://example.com/script.php"; ##### No other customization needed. ##### use strict; use LWP::Simple; get $URL; print "Content-type:text/plain\n\nDONE" if $ENV{REQUEST_METHOD}=~/(POST|GET)/i; exit;
Save the Perl CGI script as cronscript.cgi with a plain text processor like NotePad or TextWrangler. Modify cronscript.cgi for the URL of the PHP script you want to run (line 3, between the quotes).
Upload cronscript.cgi to your cgi-bin or other directory that can run Perl CGI scripts. When uploading, use FTP and upload as a plain text file, not as a binary file. After uploading, give it 755 permissions.
Type the URL of cronscript.cgi into your browser to verify it runs without errors.
Now, set up cron to run the Perl CGI script.
30 16 * * * /usr/bin/perl /path/to/cronscript.cgi
Verify the path to Perl is correct. And verify the path to cronscript.cgi is correct. If uncertain about the path to cronscript.cgi, install Master Pre-Installation Tester in the same directory, then run it to find the path.
When implemented, the above will work regardless how PHP is installed on your server. It will work with any PHP script that works in a regular browser.
Will Bontrager