1

I am using Ubuntu 14.04 X64.

I am setting up a basic Cron job with crontab -e:

* * * * * /usr/bin/php /var/www/html/test/test.php >> /var/www/cron/cron.log

This is /var/www/html/test/test.php:

<?php 
include "../test2.php"; 
?> 

And /var/www/html/test2.php:

<?php 
echo "hello world"; 
?> 

And yet nothing is being echoed. But when I go to [domain]/test/test.php, I do see "hello world" being echoed. Why am I not seeing it in /var/www/cron/cron.log?

vinsanity555
  • 241
  • 1
  • 11

4 Answers4

1

PHP and cronjobs can be a mess - especially when including other files. Try the following in your crontab:

* * * * * cd /var/www/html/test; /usr/bin/php test.php >> /var/www/cron/cron.log
Stefan P
  • 590
  • 4
  • 7
1

Try including the full path in the php include, and make it require instead of a include:

<?php 
  require_once("/var/www/html/test2.php"); 
?> 

You can also set your include path: http://php.net/manual/en/function.set-include-path.php

J. A. Streich
  • 1,683
  • 10
  • 13
  • Thanks, this actually worked. So should I just set my include path in every PHP file I intend to run with cron? Would I then be able to use relative paths? – vinsanity555 Feb 24 '15 at 17:27
  • You should avoid using absolut path for just one reason: If for any reason the directory changes (move to a different hoster, using a different distri or whatever) you have to change it again in all your scripts. – Stefan P Feb 24 '15 at 17:27
  • 1
    You can solve it using a path like this: `__DIR__ . '/../test2.php';` – asketsus Feb 24 '15 at 17:49
1

Why don't use absolute path in the include?

<?php
    include __DIR__ . '/../test2.php';
?>
asketsus
  • 96
  • 2
0

you write a wrong directory in cron

/usr/bin/php /var/www/**html**/test/test.php

in test.php

/var/www/test/test.php

in www directory have a html directory?

derylihs
  • 29
  • 1
  • 8