PHP implementation of Linux command tail-f_php instance

Source: Internet
Author: User
Tags usleep
The tail command writes the file to standard output starting at the specified point. Using the-f option of the tail command makes it easy to see the log files that are being changed, TAIL-F filename will display the most up-to-date contents of the filename on the screen, and not only refresh, so you can view the latest file content.

1. command format;

tail[necessary parameters [selection parameters] [file]

2. Command function:

Used to display the content at the end of the specified file, and is processed as input when the file is not specified. Common view log files.

3. Command parameters:

-F Loop Read

-Q does not display processing information

-V displays detailed processing information

<数目> -C Displays the number of bytes

-N <行数> display number of rows

--pid=pid is shared with-F, which means that the process ends after the id,pid dies.

-Q,--quiet,--silent never output the header of the file name

-S,--sleep-interval=s is combined with-F, which means sleep s seconds at each repetition interval

Today, I suddenly think of someone asked me a question, how to implement the Linux command tail-f through PHP, here to analyze the implementation.

This thought is also quite simple, through a loop detection file, see whether the size of the file changes, if there is a change, the output of the changes in the file part, of course, there will be a lot of details here, the specific analysis below.

If the initial file is too large or changes too much content

This time the output a lot of content may not be able to see clearly, so I set a threshold of 8192, when the content length exceeds this threshold, only the last 8,192 bytes, so that there will be no large area of the refresh cause to see the problem.

How to detect changes in file size

This problem is the core of this program, can not succeed, the performance of the good or bad depends on this part.

My implementation here is as follows:

• Open the file handle $fp, here to note that the file handle here is only open once, so put him on the outside of the loop.
• Initialize the current file size file_size and file_size_new are all 0. • Cycle inside update file_size_new file size, it is important to note that PHP to get the file size before you must run the function Clearstatcache (), clear the file state cache, or get the file size may be biased.

• Calculate add_size = File_size_new-file_size to see if the file size changes, if there is a change, move the file pointer to the specified location, and then output the newly added content, update the file_size value to new_file_size.
Usleep (50000), sleep 1/20 seconds.

Code implementation

#!/usr/bin/env php <?phpif (2! = count ($argv)) {fwrite (STDERR, "Call format Error! Use the format./tail filename ". PHP_EOL); return 1;} $file _name = $argv [1];d efine ("Max_show", 8192); $file _size = 0; $file _size_new = 0; $add _size = 0; $ignore _size = 0; $fp = fope N ($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 ($f P, $file _size + $ignore _size);} Fwrite (Stdout,fread ($fp, $add _size)); $file _size = $file _size_new;} Usleep (50000);} Fclose ($FP);

Code implementation here the first line of #!/usr/bin/env PHP is to tell the executable file, the executable PHP in the system path search, the advantage is good portability.

Here's the result.

Below is to introduce how to realize the TAIL-F function of the highlighted keyword in Linux

Inside the company a buddy posted to the mailing list of a small tip, very interesting, belongs to the programmer's "kinky Tricks" class bar, it is worth recording.
If you work under Linux, it should be commonplace to track the output of a log file with Tail-f.
However, sometimes you are more concerned with sensitive words, and want to be able to highlight these words while dynamically tracking, such as the error keyword in the log.
So, one way to do this is to put your tail output in the packaging process, which is in line with the idea of Linux pipeline processing. Take the error in the highlighted log as an example, you can do this:

Shell Code

Where Xxx.log is the file you want to track. This assumes that there is Perl in your Linux path. What Perl does here is to dynamically replace the error string in a command-line way, using the syntax structure of the Linux console_codes in the replacement process. (Details about Console_codes, which can be learned by man Console_codes) here, \e the main transfer instructions.
If you have a log such as server log, try the above command, is not all the error marked red.
Using this principle, you can highlight the output you are interested in according to the color you need, the specific color description, can be found in the man console_codes.
In addition, less itself supports operations similar to tail-f, that is, after you open a file with less, press and hold the SHIFT+F key so that it goes directly into the follow mode. It looks consistent with the tail-f effect. Using this, you want to achieve the tail-f effect of highlighting, fly is divided into the following 3 steps:

Less xxx.log in/${key_work} to search for keywords you want to highlight. (even if not in the current file) Shift+f, enter follow mode
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.