3

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)

2 Answers 2

5

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');
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, will this solution give me any problems when running the file in a browser?
Keep in mind that mws_products is outside the root of the browser. Depending on your configuration you may not be able to access the file in the browser.
1

Try with the absolute path for the file...

require_once('/var/www/mws_products.php');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.