Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Automation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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 $_SERVER['DOCUMENT_ROOT'], a variable often used when accessing files or including other PHP scripts.

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 $_SERVER['DOCUMENT_ROOT'] or other $_SERVER or $ENV variables that are not available when the script is run with cron.

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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

Support Area – Ask Your Question Here

The "Code in articles help" forum at the Willmaster.com support area is the place to get information about implementing JavaScript and other software code found on Willmaster.com

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List
software index page

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC