我已经设置了一个每小时运行一次的crontab作业。
m | h | d | M | w | command
--|---|---|---|---|----------
0 | * | * | * | * | php path/job.php
在工作中.php我有以下代码:
<?php
$today = date('Y-m-d');
echo "echo: Today is $today <br>";
printf("printf: Today is $today n");
file_put_contents("/path/$today.log","log file created");
exit();
当我在浏览器上访问job.php
时,我看到预期的输出:
printf: 今天是 20-08-2018
并创建一个新文件20-08-2018.log
。
但是,当 crontab 运行此job.php
时,我会收到作业生成的输出的电子邮件通知,它只包含:
printf: 今天是 20-08-2018
此外,我检查文件是否已生成/附加,但找不到生成文件的任何证据(即使我在等待 crontab 运行作业之前删除了所有日志文件(。
这怎么解释呢? 当 crontab 作业自动触发时,我该怎么做才能使file_put_contents
工作?
编辑:我忘了提到我已经检查了php_errorlog
怀疑在crontab触发作业时出了问题,但未能找到任何错误。
> @Ahmad:尝试此解决方案
尝试在 file_put_contents(( 函数中添加完整路径并授予适当的文件夹权限。
前任:file_put_contents(__DIR__ . DIRECTORY_SEPARATOR . "{$today}.log", "log file created")
试试这个
$today = date('Y-m-d');
$myfile = fopen("/path/$today.log", "a") or die("Unable to open file!");
$txt = "Today is $today <br> n";
echo "echo: Today is $today <br> n";
fwrite($myfile, $txt);
fclose($myfile);
exit();