Solved

PHP Cron Job not Including

Posted on 2011-03-09
2
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

752 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