search
HomeBackend DevelopmentPHP TutorialPHP实现linux命令tail -f_php实例

tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新,使你看到最新的文件内容.

1.命令格式;

tail[必要参数][选择参数][文件]

2.命令功能:

用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件。

3.命令参数:

-f 循环读取

-q 不显示处理信息

-v 显示详细的处理信息

-c 显示的字节数

-n 显示行数

--pid=PID 与-f合用,表示在进程ID,PID死掉之后结束.

-q, --quiet, --silent 从不输出给出文件名的首部

-s, --sleep-interval=S 与-f合用,表示在每次反复的间隔休眠S秒

今天突然想到之前有人问过我的一个问题,如何通过PHP实现linux中的命令tail -f,这里就来分析实现下。

这个想一想也挺简单,通过一个循环检测文件,看文件的大小是否有变化,如果有变化,输出文件变化的部分,当然了这里面会有好多的细节,这里具体分析下。

如果初始文件太大或者改变内容太多

这个时候一下输出好多内容可能看不清,因此我这里设置了一个阈值8192,当内容长度超过这个阈值的时候,只输出最后面的8192个字节,这样就不会出现大面积的刷新导致看不清的问题。

如何检测文件大小的变化

这个问题是这个程序的核心,能不能成功,性能的好坏就靠这部分了。

我在这里的实现是下面这样:

•打开文件句柄$fp,这里要注意,这里的文件句柄全程需中只打开一次关闭一次,因此要将他放在循环的外面。
•初始化当前文件大小file_size和file_size_new都为0。 •循环里面更新file_size_new文件大小,这里要注意,php中获取文件大小之前一定要运行函数clearstatcache(),清除文件状态缓存,否则获取文件大小可能会有偏差。

•计算add_size = file_size_new - file_size,看文件大小是否有变化,如果有变化,将文件指针移动到指定位置,然后输出新加的内容,更新file_size值为new_file_size。
•usleep(50000),睡眠1/20秒。

代码实现

#!/usr/bin/env php 
<&#63;php
if(2 != count($argv)){
fwrite(
STDERR,
"调用格式错误!使用格式 ./tail filename".PHP_EOL
); 
return 1;
}
$file_name = $argv[1];
define("MAX_SHOW", 8192);
$file_size = 0;
$file_size_new = 0;
$add_size = 0;
$ignore_size = 0;
$fp = fopen($file_name, "r");
while(1){
clearstatcache();
$file_size_new = filesize($file_name);
$add_size = $file_size_new - $file_size;
if($add_size > 0){ 
if($add_size > MAX_SHOW){
$ignore_size = $add_size - MAX_SHOW;
$add_size = MAX_SHOW;
fseek($fp, $file_size + $ignore_size);
} 
fwrite(
STDOUT,
fread($fp, $add_size)
); 
$file_size = $file_size_new;
}
usleep(50000);
}
fclose($fp);

代码实现这里第一行的#!/usr/bin/env php 是告诉可执行文件,可执行文件php在系统PATH中查找,这样的好处就是移植性好。

下面是结果

下文给大家介绍如何实现Linux下高亮关键字的tail -f功能

公司内部一哥们发布到邮件列表中的一个小tip,挺有意思,属于程序员的“奇淫技巧”类吧,值得记录一下。
如果你在linux下工作,那用tail -f跟踪一个日志文件的输出内容应该是家常便饭了。
但是,有时你更关心的是一些敏感字词,希望能够在动态跟踪的同时,把这些字词高亮出来,比如日志中的ERROR关键字。
那么,一种思路就是把你tail输出的东西再做一次包装处理,这个很符合linux管道处理的思想。以高亮Log中的ERROR为例,你可以这样:

Shell代码

tail -f xxx.log | perl -pe 's/(ERROR)/\e[1;31m$1\e[0m/g' 

其中,xxx.log是你要跟踪的文件。这里假设了你的Linux的PATH中有perl。perl在这里干的事情,就是通过命令行的方式进行动态的替换ERROR字符串的操作,替换过程中,主要使用了Linux的console_codes的语法结构。(具体关于console_codes的细节,可以通过man console_codes进行了解)这里,\e主要进行转移说明。
如果你手头有server log之类的日志,试试上面的命令,是不是把ERROR全部标红了。
利用这个原理,你完全可以按照你所需要的颜色高亮你感兴趣的输出,具体的颜色说明,可以在man console_codes中查到。
另外,less本身也支持类似于tail -f的操作,就是在你用less打开一个文件之后,按住SHIFT+F键,这样就直接进入follow的模式了。看上去跟tail -f效果是一致的。利用这点,你想达到高亮的tail -f的效果,就拢共分为以下3步了:

less xxx.log
中/${key_work}的方式搜索你要高亮的关键字。(即使目前文件中没有也没关系)
SHIFT+F,进入follow模式
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in LaravelNotifications in LaravelMar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft