Using the same PHP in cron as your Joomla does

Hello Joomla administrators.

You might want to use a cron job to automatically import the listings you deposit in your REAXML Import input folder. The REAXML Import can be invoked as a PHP script. This is a better approach to using curl in the cron job.

The problem is that the default php5 or php command available to the cron command may not be the right PHP version we need to use to run the REAXML Import PHP script.

If we assume that Joomla is using the right version of PHP for REAXML Import then what we need to do is find out where this version of PHP is located on your hosting server so we can explicity use it in the cron command.

If we're lucky the PHP Configure command we see in Joomla will help us work out where the binary directory for the PHP it's using is located.

To see the PHP configure command connect to your Joomla administrator and select System -> System Information. Select the PHP Information tab. See screenshot:

{ariimagepopup thumbWidth="250" thumbCount="1" single="true" close="true"} screenshot showing php information {/ariimagepopup}

What we're looking for is a parameter or parameters that let us work out where the php interpreter that the web server is using to run Joomla is likely located. In the sample above there are a number parameters who's values contain "php" and specify directories that have similar values. In the sample the directory "/opt/alt/php55" appears a fair bit. This is likely the root directory for the PHP installation being used. You may see other values such as "/usr/local/php55" appearing a lot instead. There are a number of possibilities.

The directory where the PHP excutable we're after is located in is usually the "bin" directory under this root directory. If you want to check this directory but you don't have command line access to your server (SSH etc) you can try creating a temporary cron job to issue a command to list the content of that directory. Remember to delete this cron job when your done.

Create a cron job setting all the scheduling fields except minute to *. Set the minute to a couple of minutes hence. Set the command to:

ls -l <directory value>

...Replace <directory value> with the directory you want a listing of. Based on the PHP Configure command shown in the sample that whole command would be:

ls -l /opt/alt/php55/usr/bin

Add the cron job and wait for the email.

Remember to delete that cron job after it's run.

The email should contain something like:

total 17708
-rwxr-xr-x 1 root root    2227 Apr  9  2014 bytekit
-rwxr-xr-x 1 root root    2110 Apr  9  2014 dbunit
-rwxr-xr-x 1 root root    2256 Apr  9  2014 hphpa
-rwxr-xr-x 1 root root 4934624 Apr 20 20:33 lsphp
-rwxr-xr-x 1 root root     404 Aug 29  2014 pear
-rwxr-xr-x 1 root root     430 Aug 29  2014 peardev
-rwxr-xr-x 1 root root     320 Aug 29  2014 pecl
lrwxrwxrwx 1 root root       9 Apr 30 05:11 phar -> phar.phar
-rwxr-xr-x 1 root root   14837 Apr 20 20:33 phar.phar
-rwxr-xr-x 1 root root 4954240 Apr 20 20:33 php
-rwxr-xr-x 1 root root    2282 Apr  9  2014 phpcb
-rwxr-xr-x 1 root root 4933952 Apr 20 20:33 php-cgi
-rwxr-xr-x 1 root root    5142 Apr 20 20:33 php-config
-rwxr-xr-x 1 root root    2284 Apr  9  2014 phpcov
-rwxr-xr-x 1 root root    2415 Apr  9  2014 phpcpd
-rwxr-xr-x 1 root root    4544 Apr 20 20:33 phpize
-rwxr-xr-x 1 root root    2269 Apr  9  2014 phploc
-rwxr-xr-x 1 root root 3210678 Apr  9  2014 phpunit
-rwxr-xr-x 1 root root    2017 Apr  9  2014 phpunit-skelgen
-rwxr-xr-x 1 root root    2183 Apr  9  2014 ppw

or

total 21752
-rwxr-xr-x 1 root root 838 Jan 30 04:59 pear
-rw-r--r-- 1 root root 859 Jan 30 04:59 peardev
-rw-r--r-- 1 root root 775 Jan 30 04:59 pecl
lrwxrwxrwx 1 root root 30 Nov 26 01:33 phar -> /usr/local/php55/bin/phar.phar
-rw-r--r-- 1 root root 14829 Jan 30 04:59 phar.phar
-rwxr-xr-x 1 root root 11075200 Jan 30 05:01 php
-rwxr-xr-x 1 root root 11095744 Jan 30 05:01 php-cli
-rwxr-xr-x 1 root root 4240 Jan 30 04:59 php-config
-rwxr-xr-x 1 root root 4533 Jan 30 04:59 phpize 

The directory should contain some executable files. The x on the end of the values in the first column tells us that the file is an executable.

OK if we've got this far we can be pretty sure that the command to run the same PHP iterpreter as the web server will be to invoke one of the the php executables we see in this directory.

Now, which one to use? If there is a php-cli executable in that directory, that is the one we have to use, otherwise we use the just php one.

So the php command would be the directory value with /php or /php-cli tacked on the end. Something like (based on the above screen shot):

/opt/alt/php55/usr/bin/php

For the cron command string to invoke the REAXML Import it would be:

/opt/alt/php55/usr/bin/php <your joomla root directory>/cli/reaxml-importer.php

The cron command on my REAXML Import demo site would be:

/opt/alt/php55/usr/bin/php /home/cliftonw/reaxml_html/cli/reaxml-importer.php

Hopefully this will be enough to allow you to run the REAXML Import CLI script from a cron job.