Solved

PHP Cron Job not Including

Posted on 2011-03-09
2
383 Views
Last Modified: 2012-05-11
I have a php scrip that works fine on the web

www.mysite.com/somescript.php

The first part of that script includes another set of files (which includes even more etc)

<?php
include $_SERVER['DOCUMENT_ROOT'] . "/myinclude.php";
. . .
?>

However when I run this through cron
php /home/mysite/public_html/somescript.php

It fails with this message

<br />
<b>Warning</b>:  include(/myinclude.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in <b>/home/mysite/public_html/somescript.php</b> on line <b>2</b><br />

And other error messages related to the fact that it can't connect to the db etc as all the connection data is in the myinclude.php file.

Having done some reading around this it seems that the cron jobs run from a different server / instance / something than the web so $_SERVER['DOCUMENT_ROOT'] doesn't work in the same way.

How do I get around this? The server is on third party hosting / cpanel so I have limited access to command line and php.ini etc files.

Thanks

Mike
0
Comment
Question by:hungoveragain
2 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 35081884
PHP can run in two modes - via a web server and via a command line (CLI). There are usually two php.ini files for this in /etc/php5

TBH I find it simpler to use the lynx browser which is text based and get it to call the webserver. That way I know my PHP will execute on the server if it executes in the browser. The command (in Cron or elsewhere) is

lynx -dump http://example.com/path/to/your/script.php

Alternatively inspect the other php.ini file and alter it to be similar to the "standard" one and find what user/group the CLI runs under and set access modes accordingly.
0
 
LVL 14

Expert Comment

by:Scott Madeira
ID: 35082306
$_SERVER['DOCUMENT_ROOT'] has no meaning when you are running a script from the command line because the script isn't being processed through the web server.

One thing that you could do is manually set $_SERVER['DOCUMENT_ROOT'] in your script to the path you want it to be if you are running from the CLI.  If you invoke your script like this:


php /home/mysite/public_html/somescript.php CLI

you would have this available to you:

$_SERVER["argv"][0] =  /home/mysite/public_html/somescript.php
$_SERVER["argv"][1] = CLI
$_SERVER["argc"] = 2


In your file you could check these variables and either use the DOCUMENT_ROOT as set by the server or fill it yourself.

if ($_SERVER["argv"][1] == 'CLI') {
      $_SERVER["DOCUMENT_ROOT"] = 'path/to/your/files/include.php';
}

There may be a more elegant solution to the problem than this but this should work for you.







0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question