The tail command writes the file to the standard output from the specified point. you can use the-f option of the tail command to conveniently check the changing log file. tail-ffilename will display the tail content in filename on the screen and not only refresh, so that you can see the latest file content. next, I will introduce you to the linux Command tail-f through PHP. For more information, see the tail command to write the file to the standard output from the specified point. you can use the-f option of the tail command to conveniently check the changing log file. The tail content of the filename is displayed on the screen and refreshed, so that you can see the latest file content.
1. Command format;
Tail [required parameters] [select parameters] [files]
2. command functions:
Displays the content at the end of a specified file. If no file is specified, it is processed as an input. Common view log files.
3. command parameters:
-F: Read cyclically
-Q: no processing information is displayed.
-V: displays detailed processing information.
-C <数目> Number of bytes displayed
-N <行数> Number of lines displayed
-- Pid = PID and-f are used together to indicate that the process ID ends after the PID dies.
-Q, -- quiet, -- silent never outputs the header of the given file name
-S, -- sleep-interval = S and-f are used together, indicating that the sleep seconds at each repeated interval
Today, I suddenly thought of a question that someone asked me before.PHP implements the linux Command tail-fHere, we will analyze the implementation.
It's quite easy to think about this. Check whether the file size has changed through a loop. If there is a change in the size of the output file, of course there will be a lot of details here, here is a detailed analysis.
If the initial file is too large or the content is changed too much
At this time, the output may not be clear, so I set a threshold value of 8192 here. When the content length exceeds this threshold, only the last 8192 bytes are output, in this way, you will not see the problem caused by a large area of refresh.
How to detect file size changes
This problem is the core of the program. Whether the program can be successful or not depends on the performance.
The implementation here is as follows:
• Open the file handle $ fp. Note that the entire process of the file handle must be closed once, so you need to put it outside the loop.
• Initialize the current file. The file_size and file_size_new values are both 0. • Update the file_size_new file size in the loop. Note that before getting the file size in php, run the clearstatcache () function to clear the File status cache. Otherwise, the file size may be deviated.
• Calculate add_size = file_size_new-file_size to check whether the file size has changed. If there is any change, move the file pointer to the specified position, and then output the newly added content to update the value of file_size to new_file_size.
• Usleep (50000), sleep for 1/20 seconds.
Code Implementation
#! /Usr/bin/env php <? Phpif (2! = Count ($ argv) {fwrite (STDERR, "the call format is incorrect! 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 );
The code is implemented in the first line #! /Usr/bin/env php tells the executable file, and the executable file php is searched in the system PATH. This advantage is good portability.
The result is as follows:
The following describes how to implement the tail-f function with highlighted keywords in Linux.
A buddy in the company posted a small tip to the email list. It is interesting. It belongs to the programmer's "odd sex skills" class. It is worth record.
If you work in linux, it is common to use tail-f to track the output content of a log file.
However, sometimes you are more concerned with sensitive words, hoping to highlight these words, such as the ERROR keyword in the log, while tracking them dynamically.
Then, one idea is to pack the output of your tail, which is in line with the linux pipeline processing idea. Take the ERROR in the highlighted Log as an example. You can do this:
Shell code
tail -f xxx.log | perl -pe 's/(ERROR)/\e[1;31m$1\e[0m/g'
Xxx. log is the file to be tracked. Here we assume that your Linux PATH contains perl. What perl does here is to dynamically Replace the ERROR string using the command line. During the replacement process, the Linux console_codes syntax structure is mainly used. (For more information about console_codes, see man console_codes.) Here, \ e mainly describes the transfer.
If you have logs such as server logs on hand, try the above command to see if all the errors are marked red.
By using this principle, you can highlight the output you are interested in according to the color you need. For specific color descriptions, you can find it in man console_codes.
In addition, less itself supports operations similar to tail-f, that is, after you open a file with less, press SHIFT + F to directly enter the follow mode. It looks like the effect of tail-f is the same. To achieve the highlighted tail-f effect, you can perform the following three steps:
Search for the keyword you want to highlight in the/$ {key_work} method in less xxx. log. (Even if the current file does not exist, it does not matter) SHIFT + F, enter the follow Mode