尾巴-f实时输出过程仍在运行

时间:2019-02-11 17:45:40

标签: php shell exec

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

我尝试了一些用

杀死它的解决方案
9,990,449,935

服务器位于Apache2上

我的代码:

while(true)
{
if($flag === false) die(); // Or exit if you prefer
}

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

1 个答案:

答案 0 :(得分:0)

否@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限制