fwrite或error_log无法使用CLI&;处理后台任务;


fwrite or error_log not working with background task with CLI &

我有一个基本的php脚本,它将写入/日志到文本文档

<?php
$filename = 'php_log.log';
$somecontent = 'writing with fwrite!'.PHP_EOL;
if (is_writable($filename)) {
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }
    echo "Success, wrote ($somecontent) to file ($filename)";
    fclose($handle);
} else {
    echo "The file $filename is not writable";
}
error_log('logging with error_log!'.PHP_EOL,3,'php_log.log');
?>

在CLI中,如果我执行php test.php并检查php_log.log,我会看到这两个写入。但是当我尝试php test.php &时,php_log.log 中没有任何内容

有没有我不知道的php设置?谢谢

可能是没有锁定
为什么要手动写入文件,然后使用error_log

  • 考虑file_put_contents()FILE_APPEND|FILE_LOCK。它只附加到文件的末尾,并负责锁定。

  • 启用error_reporting并将stderr重定向到一个单独的文件以了解更多信息。

    php test.php 2> php_errors.txt &