【发布时间】:2016-01-21 11:00:18
【问题描述】:
我正在使用 crontab 调用一个 php 脚本。
在这个脚本中有
error_log('test');
当脚本从http或直接命令行执行时
php -f script.php
一切都很好,我的错误是日志。
但是当从 cron 调用时它不起作用。
这是我的 cron
* * * * * -u www-data /full_path_to/php -f /full_path_to/script.php
这是我尝试过的:
-
带参数的error_log:
error_log('test', 3, '/full_path_to/error.log'); -
更改错误报告:
error_reporting(E_ALL); ini_set('display_errors','On'); ini_set('error_log', '/full_path_to/error.log'); cron 调用以 > /full_path_to/error.log 2>&1 结尾(不知道是否有用)
对于 http,error_log 路径是从 htaccess 设置的。 我迷失了 php cli...
我可以看到每分钟执行一次 cron (syslog),所以应该是 PHP 配置问题?
如果您能提供帮助,非常感谢。
编辑:使用“-u www-data”执行 Cron
这是我在 syslog 中看到的调用:
CRON[13921]: (www-data) CMD (-u www-data /usr/bin/php -f /fullpath/script.php > /fullpath/error.log 2>&1)
【问题讨论】:
-
脚本 only 是否包含该日志行?还是有更多您没有向我们展示的背景信息?因为就其本身而言,这应该有效。所以这告诉我很可能缺少 context 。最常见的陷阱是使用包含相对路径。
-
是的,知道有 4 行:错误报告行和 error_log。创建文件也不起作用(我尝试了 fopen/fwrite 和 file_put_contents)
-
听起来像是权限问题。尝试在有权访问所需路径的用户下运行 cron(或作为 root,但这应该是最后的手段)。
-
cron 作为 www-data 运行。这个 AFAIK 与运行 apache 的用户相同。相同的用户,相同的组...我会尝试将 chmod 设置为 777
-
不要那样做,chmod 777 总是一个坏主意。 特别是当它涉及网络可访问文件时。另见How will a server become vulnerable with chmod 777?