沃梦达 / 编程问答 / php问题 / 正文

php cron作业没有运行

php cron job not running(php cron作业没有运行)

本文介绍了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作业没有运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:php cron作业没有运行

基础教程推荐