du7133 2013-02-13 01:58
浏览 43

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

I have a basic php script that will write/log to a text document

<?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');
?>

From CLI, if i do php test.php and check php_log.log, I see both writes. But when I try php test.php &, there is nothing written to php_log.log

Is there some php setting I am unaware of? Thanks!

  • 写回答

1条回答 默认 最新

  • dthjnc306679 2013-02-13 02:26
    关注

    Possibly the absence of locking.
    Why are writing to the file manually and then with error_log?

    • Consider file_put_contents() with FILE_APPEND|FILE_LOCK instead. That appends just to the end of the file and takes care of locking.

    • Enable error_reporting and redirect stderr to a separate file to find out more.

      php test.php 2> php_errors.txt &

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用