crontab cannot find required php file when running a php script

I am trying to get crontab to run a php file, here is the cronjob

10 * * * * /usr/bin/php  /var/www/update/ranks.php >> /var/www/update/log/ranks.txt

But I keep getting an error saying the required file does not exist

PHP Warning:  require_once(../mws_products.php): failed to open stream: No such file or directory in /var/www/update/ranks.php on line 2
PHP Fatal error:  require_once(): Failed opening required '../mws_products.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/update/ranks.php on line 2

I do not get this problem when I run the file from a browser or when I go into the directory of the file and execute the file e.g. cd /var/www/update/

ranks.php

<?php 
require_once('../mws_products.php');

echo "-------------------------------------------------------------\n";
echo date('d-M-Y H:i:s',time())."\n";
echo "Update Ranks\n";
$products->updateRanks();
$database->addUpdate("ranks", time());
echo "\n\n\n";
?>

folder structure

[folder] update
   |____ [file] ranks.php

[file]   mws_products.php

What could be causing this problem? (note: I have tried restarting apache and the server)

Answers


As your are running in a crontab from the root directory ../mws_pruducts.php does not exists, relative to the given root.

There are multiple solutions, this is one of them:

define( 'ROOT', dirname(__FILE__) );  
require_once(ROOT . '/../mws_products.php');

Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.