php cron作业未运行 [英] php cron job not running

查看:122
本文介绍了php cron作业未运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我的 test.php 文件:

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

这是 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) 

cron作业将无法运行,问题出在php。如果我手动运行该文件,一切运行良好。

The cron job will not run, and the problem is with php. If i run the file manually, all works well.

php test.php

相关问题:为什么crontab不会执行我的PHP脚本?

推荐答案

在你的脚本。否则, cron 将不知道该文件在哪里。

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

而不是

$my_file = 'file.txt';

使用

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

可能你会收到 file.txt / 中的某处。

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

注意 crontab 有限的环境,所以它不能假设任何关于路径。这就是为什么你还必须提供 php 等的完整路径。

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.

排查cron作业常见问题

使用相对路径。如果你的cron作业正在执行一些
种类的脚本,你必须确保在该脚本中只使用绝对路径。
例如,如果你的脚本位于/path/to/script.phpand
你试图在同一目录中打开一个名为file.php的文件
,你不能使用相对路径如fopen(file.php)。文件必须从其绝对路径调用
,如下所示:fopen(/path/to/file.php)。
这是因为cron作业不一定从脚本所在的
目录中运行,因此必须专门调用所有路径。

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.

这篇关于php cron作业未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆