search
HomeBackend DevelopmentPHP TutorialPHP implements linux command tail -f, phplinuxtail-f_PHP tutorial

PHP实现linux命令tail -f,phplinuxtail-f

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模式

您可能感兴趣的文章:

  • Node.js中使用Log.io在浏览器中实时监控日志(等同tail -f命令)

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1102469.htmlTechArticlePHP实现linux命令tail -f,phplinuxtail-f tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,ta...
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment