【问题标题】:Tail -f live output process still runningtail -f 实时输出进程仍在运行
【发布时间】:2019-02-11 17:45:40
【问题描述】:

我正在尝试对名为 fail2ban.log 的文件进行实时输出,该日志位于我的 linux 服务器上,我尝试使用它来处理它。尾部进程保持打开状态,因此在某些人打开页面后,它会使用大量 cpu 性能,因为进程保持打开状态

我尝试了一些用

杀死它的解决方案
while(true)
{
if($flag === false) die(); // Or exit if you prefer
}

服务器在 Apache2 上

我的代码:

<?php
echo "Number of banned ip (live) : ";
$hand = popen("grep 'Ban' /var/log/fail2ban.log | wc -l 2>&1", 'r');
while(!feof($hand)) {
    $buff = fgets($hand);
    echo "$buff<br/>\n";
    ob_flush();
    flush();
}
pclose($hand);
echo " ";
echo "Current Log (go at the bottom of the page for the live log)";
echo " ";
$output = shell_exec('cat /var/log/fail2ban.log 2>&1');
echo "<pre>$output</pre>";
echo "Live Logs";
echo "<h1> </h1> ";
echo " ";
$handle = popen("tail -f /var/log/fail2ban.log 2>&1", 'r');
while(!feof($handle)) {
    $buffer = fgets($handle);
    echo "$buffer<br/>\n";
    ob_flush();
    flush();
}
pclose($handle);
?>

我希望它在用户退出页面时终止进程。

【问题讨论】:

标签: php shell exec


【解决方案1】:

没有@jhnc 在这种情况下popen是有罪的,它不会在程序关闭时结束进程。

一般来说,PHP 是实现tail -f 的最糟糕的选择之一。最好用node+websocket。

在这种情况下,您需要检查是否已通过其他方法将某些内容添加到文件中。来自http://php.net/manual/en/function.inotify-init.php#101093

<?php
/**
* Tail a file (UNIX only!)
* Watch a file for changes using inotify and return the changed data
*
* @param string $file - filename of the file to be watched
* @param integer $pos - actual position in the file
* @return string
*/
function tail($file,&$pos) {
    // get the size of the file
    if(!$pos) $pos = filesize($file);
    // Open an inotify instance
    $fd = inotify_init();
    // Watch $file for changes.
    $watch_descriptor = inotify_add_watch($fd, $file, IN_ALL_EVENTS);
    // Loop forever (breaks are below)
    while (true) {
        // Read events (inotify_read is blocking!)
        $events = inotify_read($fd);
        // Loop though the events which occured
        foreach ($events as $event=>$evdetails) {
            // React on the event type
            switch (true) {
                // File was modified
                case ($evdetails['mask'] & IN_MODIFY):
                    // Stop watching $file for changes
                    inotify_rm_watch($fd, $watch_descriptor);
                    // Close the inotify instance
                    fclose($fd);
                    // open the file
                    $fp = fopen($file,'r');
                    if (!$fp) return false;
                    // seek to the last EOF position
                    fseek($fp,$pos);
                    // read until EOF
                    while (!feof($fp)) {
                        $buf .= fread($fp,8192);
                    }
                    // save the new EOF to $pos
                    $pos = ftell($fp); // (remember: $pos is called by reference)
                    // close the file pointer
                    fclose($fp);
                    // return the new data and leave the function
                    return $buf;
                    // be a nice guy and program good code ;-)
                    break;

                    // File was moved or deleted
                case ($evdetails['mask'] & IN_MOVE):
                case ($evdetails['mask'] & IN_MOVE_SELF):
                case ($evdetails['mask'] & IN_DELETE):
                case ($evdetails['mask'] & IN_DELETE_SELF):
                    // Stop watching $file for changes
                    inotify_rm_watch($fd, $watch_descriptor);
                    // Close the inotify instance
                    fclose($fd);
                    // Return a failure
                    return false;
                    break;
            }
        }
    }
}

// Use it like that:
$lastpos = 0;
$file = '/var/log/fail2ban.log'l
while (true) {
    echo tail($file,$lastpos);
    ob_flush();
    flush();
}
?>

您不能忘记 max_execution_time 和 Apache 限制

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多