It tooks hours to figure out why PHP cron jobs were not working on the godaddy web hosting. Even though the ‘cron job manager’ indicates it will email you errors, it doesn’t. After verifying that the configured jobs were actually running (using a simple echo and pipe to a file) I set out to find out why the php scripts seemed to not be working.

The bottom line is, that an important piece of required information was missing that would make things work correctly. Well, at least for my applications. I use the RPCL (VCL) for PHP (Delphi).

And the answer is…a missing environment variable. SCRIPT_FILENAME is required by the RPCL to make the ‘use_unit()’ function work correctly, and find all the include files.

First, I created a BASH SCRIPT to make it easier to resuse for the cron jobs. The script sets the environment variable, then launches the command line PHP.

#!/bin/bash
cd ~/html
export SCRIPT_FILENAME=~/html/$1
/web/cgi-bin/php5 $1

Since godaddy hosting cron jobs launch in the home directory, the script first changes to the html directory, then sets the environment variable, then calls php5. I saved this script as runphp.cmd in the html directory on the host. I set the execute permission on the file so linux would know it is OK to execute.

The cron job command line to run TEST.PHP looks like this (I’m not showing the timing parameters):

~/html/runphp.cmd TEST.PHP

This assumes that TEST.PHP is in html directory. If it was in a subdirectory, say such as TESTCODE, it would look like this:

~/html/runphp.cmd TESTCODE/TEST.PHP

The run script not only changes to the html directory, it also assumes the given php file is in that directory. That’s why the html is prepended to the given script name in the environment variable.

If you want to save the output of the job, simply pipe it to a file. For example:

~/html/runphp.cmd TEST.PHP >> testrun.log

The log file will grow with every execution, so you’ll want to check it regularly.

I hope this saves you some time and aggravation…as I mentioned, it tooks hours to find the information, experiment, troubleshoot and get it all working.

Print This Article Print This ArticleEmail This Article Email This Article • 1,669 views •

Related Posts