Solved

PHP Cron Job not Including

Posted on 2011-03-09
2
380 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now