Solved

PHP Cron Job not Including

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
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 …

777 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