PHP版Tail命令,如何追踪长尾词动态?
- 内容介绍
- 文章标签
- 相关推荐
本文共计225个文字,预计阅读时间需要1分钟。

php// 监控文件变化,可以通过tail、inotify实现,也可以通过awk转换变化内容到外部命令中。// 下面是通过popen调用系统命令,性能和内存消耗相对较小。
// 定义监控的文件路径$filePath=/path/to/your/file;
// 使用inotify监控文件变化$inotify=inotify_init();inotify_add_watch($inotify, $filePath, IN_MODIFY);
// 循环检查文件变化while (true) { $events=inotify_read($inotify); foreach ($events as $event) { if ($event['mask']==IN_MODIFY) { // 文件内容变化,执行外部命令 $command=tail -n 10 . escapeshellarg($filePath); $output=popen($command, r); $content=fread($output, 1024); pclose($output);
// 处理文件内容变化 echo File content changed:\n; echo $content; } }}
监控文件变化,可以通过tail,inotify来实现,也可以通过awk转发变化的内容到外部命令中。下面是通过popen来调用系统命令,性能内存开销相对比较小。(转)
1.[代码][PHP]代码
$handle = popen("tail -f /var/log/your_file.log 2>&1", 'r'); while(!feof($handle)) { $buffer = fgets($handle); echo "$buffer\n"; flush(); } pclose($handle);
本文共计225个文字,预计阅读时间需要1分钟。

php// 监控文件变化,可以通过tail、inotify实现,也可以通过awk转换变化内容到外部命令中。// 下面是通过popen调用系统命令,性能和内存消耗相对较小。
// 定义监控的文件路径$filePath=/path/to/your/file;
// 使用inotify监控文件变化$inotify=inotify_init();inotify_add_watch($inotify, $filePath, IN_MODIFY);
// 循环检查文件变化while (true) { $events=inotify_read($inotify); foreach ($events as $event) { if ($event['mask']==IN_MODIFY) { // 文件内容变化,执行外部命令 $command=tail -n 10 . escapeshellarg($filePath); $output=popen($command, r); $content=fread($output, 1024); pclose($output);
// 处理文件内容变化 echo File content changed:\n; echo $content; } }}
监控文件变化,可以通过tail,inotify来实现,也可以通过awk转发变化的内容到外部命令中。下面是通过popen来调用系统命令,性能内存开销相对比较小。(转)
1.[代码][PHP]代码
$handle = popen("tail -f /var/log/your_file.log 2>&1", 'r'); while(!feof($handle)) { $buffer = fgets($handle); echo "$buffer\n"; flush(); } pclose($handle);

