PHP Implementation Linux Command tail-f_php instance

Source: Internet
Author: User
Tags sleep usleep

The tail command writes the file to the standard output from the specified point. Using the-f option of the tail command makes it easy to refer to the changing log file, Tail-f filename will display the tail contents of the filename on the screen, and not only refresh, so that you see the latest file content.

1. command format;

tail[necessary parameters [select parameters] [file]

2. Command function:

Used to display the content at the end of a specified file and to process it as input when no file is specified. Common view log files.

3. Command parameters:

-F Loop Read

-Q does not display processing information

-V Display detailed processing information

Number of-c< > bytes displayed

-n< number of rows > show rows

--pid=pid is shared with-F, which ends after the process id,pid dead.

-Q,--quiet,--silent never output the header of the filename

-S,--sleep-interval=s is shared with-F, which means sleeping s seconds at each repeated interval

Today suddenly thought of someone asked me a question, how to implement Linux in the PHP command Tail-f, here to analyze the implementation.

This think also quite simple, through a circular detection file to see whether the size of the file changes, if there are changes in the output file changes, of course, there will be a lot of details, here specific analysis.

If the initial file is too large or changes too many things

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

How to detect changes in file size

This problem is the core of the program, can not be successful, the performance of the good and bad depends on this part.

My implementation here is the following:

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

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

Code implementation

#!/usr/bin/env php 
<?php
if (2!= count ($argv)) {
fwrite (
STDERR,
"calling format Error!) Use format./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);

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

Here's the result.

Below to show you how to achieve the Linux highlighted keyword tail-f function

Inside the company a buddy posted to the mailing list of a tip, very interesting, belong to the programmer's "strange Tricks" category, 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 about some sensitive words, want to be able to dynamically track the same time, to highlight these words, such as the error keyword in the log.
One idea, then, is to do a wrapper over what you tail output, which is very much in line with the idea of a Linux pipeline. Take the error in the highlight log for example, you can do this:

Shell Code

 
 

Among them, Xxx.log is the file you want to track. This assumes that you have Perl in your Linux path. What Perl does here is to dynamically replace the error string with the command line, and in the process of replacing it, it mainly uses the CONSOLE_CODES syntax structure of Linux. (Specific details about console_codes, can be understood through man console_codes) here, \e main transfer instructions.
If you have a log like server log on hand, try the above command to make all the error marks red.
Using this principle, you can highlight the output you are interested in according to the color you need, and the specific color description can be found in man console_codes.
In addition, the less itself supports similar tail-f operations, that is, after you open a file with less, hold down the Shift+f key, and then go directly to the follow mode. Looks consistent with the tail-f effect. Using this, you want to achieve the effect of the highlighted tail-f, Shang is divided into the following 3 steps:

Less xxx.log
/${key_work} to search for the keyword you want to highlight. (Even if there's nothing in the current file it doesn't matter)
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.