Failed to start php cron job

root@xx:/var/www/test# which php
/usr/bin/php

root@xx:/var/www/test# ls -la
total 16
drwxrwxrwx 2 root root 4096 Nov 14 09:37 .
drwxrwxrwx 6 root root 4096 Nov 13 15:51 ..
-rwxrwxrwx 1 root root  153 Nov 14 09:35 test.php

This is my file test.php:

<?php
$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file); //implicitly creates file

And this is the result crontab -l:

#this is ok
* * * * * touch /tmp/hello
#this only creates an empty php_result.log
* * * * * /usr/bin/php /var/www/test/test.php > /tmp/php_result.log

root@xx:/var/www/test# php -v
PHP 5.4.34-0+deb7u1 (cli) (built: Oct 20 2014 08:50:30) 

The cron task does not start and the problem is with php. If I run the file manually, everything works fine.

php test.php

Related question: Why does crontab not execute my PHP script? .

+4
source share
1 answer

You need to use full paths in your scripts. Otherwise, cronit will not know where this file is.

therefore instead

$my_file = 'file.txt';

using

$my_file = '/path/to/file.txt';

You probably got file.txtsomewhere in /.

crontab , . php ..

cron:

. cron script , script. , script /path/to/ script.phpand file.php , , fopen (file.php). , : fopen (/path/to/file.php). , cron script, .

+8

All Articles