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?

4 Answers 4

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

Comments

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

3 Comments

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?
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.
You can solve it using a path like this: __DIR__ . '/../test2.php';
1

Why don't use absolute path in the include?

<?php
    include __DIR__ . '/../test2.php';
?>

Comments

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?

3 Comments

Some servers use /var/www/html that is the default in RedHat's Apache configuration.
so why he dont put the file in /var/www/html/test/test.php
I made a mistake writing my question. It is corrected now, thanks for catching it.

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.