Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP Cron Job not Including

Posted on 2011-03-09
2
Medium Priority
?
392 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 2000 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

Plug and play, no additional software required!

The ATEN UE3310 USB3.1 Gen1 Extender Cable allows users to extend the distance between the computer and USB devices up to 10 m (33 ft). The UE3310 is a high-quality, cost-effective solution for professional environments such as hospitals, factories and business facilities.

Question has a verified solution.

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

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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…

688 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