php cron job not running

user2990084
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 test.php file:

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

And this is the output of 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 job will not run, and the problem is with php. If i run the file manually, all works well.

php test.php

Related question: Why is crontab not executing my PHP script?.

fedorqui 'SO stop harming'

You need to use full paths in your scripts. Otherwise, cron won't know where is that file.

so instead of

$my_file = 'file.txt';

use

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

Probably you were getting file.txt stored somewhere in /.

Note crontab runs in a limited environment, so it cannot assume anything regarding to paths. That's why you also have to provide the full path of php, etc.

From Troubleshooting common issues with cron jobs:

Using relative paths. If your cron job is executing a script of some kind, you must be sure to use only absolute paths inside that script. For example, if your script is located at /path/to/script.phpand you're trying to open a file called file.php in the same directory, you cannot use a relative path such as fopen(file.php). The file must be called from its absolute path, like this: fopen(/path/to/file.php). This is because cron jobs do not necessarily run from the directory in which the script is located, so all paths must be called specifically.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

XAMPP Windows上的Php Cron Job

来自分类Dev

需要PHP Cron Job Flush / OB Flush?

来自分类Dev

使用PHP的Cpanel中的Cron Job

来自分类Dev

PHP包括在Cron Job中不起作用

来自分类Dev

在Windows的localhost上的PHP Script上运行Cron Job

来自分类Dev

来自public_html外部的PHP MySql请求(Cron Job)

来自分类Dev

PHP包括在Cron Job中不起作用

来自分类Dev

有超时的Cron Job

来自分类Dev

Cron Job语法Debian

来自分类Dev

Tell PHP to enable or use GD when running script via cron

来自分类Dev

PHP Curl脚本可通过浏览器工作,不能用作Cron Job

来自分类Dev

运行Cron Job,文件放到根目录中/而不是从中运行php脚本

来自分类Dev

Setting cron job using codeigniter

来自分类Dev

Cron Job仅特定月份

来自分类Dev

Cron Job在Laravel 4中

来自分类Dev

Cron Job无法执行

来自分类Dev

Cron Job在错误的日期执行

来自分类Dev

Cron Job Killing and Restarting Python Script

来自分类Dev

全天以不同的频率运行Cron Job

来自分类Dev

cron-job Linux apache ssl

来自分类Dev

Laravel CPanel Cron Job命令未运行

来自分类Dev

使用Cron Job从Instagram获取Rails记录

来自分类Dev

Cron Job今天开始吗?

来自分类Dev

GAE Cron Job会在运行之前等待先前的Cron Job完成吗?

来自分类Dev

PHP在Cron工作

来自分类Dev

从php设置cron作业

来自分类Dev

PHP背景cron作业

来自分类Dev

Cron和PHP问题

来自分类Dev

在PHP中编辑cron