tags:

views:

48

answers:

3

Hello there,

I have no idea whats going on. But I have a script that looks like this. Cron job refuses to run it:

include_once 'class_lib/mime_mail/mimeDecode.php';
include_once 'class_lib/Mail/IMAPv2.php';
include_once 'inc-functions.php';
include_once "$_SERVER[DOCUMENT_ROOT]/class_lib/DbFuctioneer.php";

$dbFuctioneer = new DbFuctioneer();

Everything works well when I remove:

$dbFuctioneer = new DbFuctioneer();

Even when DbFuctioneer() looks like this:

<?php

class DbFuctioneer { 
    function dbCountMatches( $count) {
        return $count;
    }
}

Does Cron have a problem with Classes in his Jobs?

Thank you for your time.

Kind regards,
Marius

+1  A: 

Do the following (run your script from the command line) and check the output is correct:

print("$_SERVER[DOCUMENT_ROOT]/class_lib/DbFuctioneer.php");

Most likely its broken.

zaf
A: 

It seems

$_SERVER['DOCUMENT_ROOT']

is empty when cron is running its job.

Why is that?

Marius
Thats not an answer, it's a comment. Please move it if you can.
zaf
+2  A: 

There is no $_SERVER["DOCUMENT_ROOT"] present when you call the script from the command line.

That variable (along with many others like REQUEST_URI, SCRIPT_NAME, HTTP_HOST....) is set by Apache, which is not running in your case.

You need to set the root directory manually.

To find out whether you are running in the context of a web site or from the command line, use php_sapi_name().

You could set $_SERVER["DOCUMENT_ROOT"] manually when running on the command line, but I would rather use a completely new constant or variable to put the path into.

Pekka
More exactly, there is not DOCUMENT_ROOT in command line.
Álvaro G. Vicario
Legend! Thank you!M
Marius
@Álvaro you're right, I edited to make the wording clearer. @Marius you're welcome.
Pekka
Another option is to use curl to run the script via the command line. The benefit to that is the script is being run in the same manner it was designed, but unless it's a localhost script, lack of an internet connection may restrict it.
Josh