English 中文(简体)
监听日志文件时检测 PHP 中丢失的文件处理器
原标题:Detect lost filehandler in PHP when listening to logfiles

我试图在 PHP 中建立一个小恶魔, 分析 Linux 系统中的日志文件 。 (例如, 跟随系统) 。

我设法通过 fopen 打开了文件, 并用 stream_get_line 直接读了它。 当被监视的文件被删除并重新创建( 例如, 当旋转日志时) 时, 我的问题就开始了。 程序不再读了, 即使文件比以前大 。

是否有优雅的解决方案? stream_get_meta_data 无助于解决问题,在命令行使用 tail -f 也存在同样的问题。

EDIT, added sample code I tried to boil down the code to a minimum to illustrate what I am looking for

<?php
$break=FALSE;
$handle = fopen( ./testlog.txt ,  r );
do {
    $line = stream_get_line($handle, 100, "
");
    if(!empty($line)) {
        // do something
        echo $line;
    }
    while (feof($handle)) {
      sleep (5);
      $line = stream_get_line($handle, 100, "
");
      if(!empty($line)) {
        // do something
        echo $line;
      }
      // a commented on php.net indicated it is possible
      // with tcp streams to distinguish empty and lost
      // does NOT work here  --> need somefunction($handle)
      if($line !== FALSE && $line =  ) $break=TRUE;
    }
} while (!$break);
fclose($handle);
?>
最佳回答

当日志文件旋转时, 原始文件被复制, 然后删除, 并创建一个同名的新文件。 它可能和原始文件有相同的名字, 但是它有不同的线条。 信条( 下沉描述如下) 与文件的隐藏递增索引数字相似。 您可以更改文件的名称, 或者移动它, 但用它来取用。 一旦删除原始日志文件, 您可以使用相同的文件处理器重新打开一个同名的文件, 因为信条已经更改。 您最好的赌注是检测失败, 并尝试打开新文件 。

问题回答

暂无回答




相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签