PHP implements Linux command tail

Source: Internet
Author: User
Tags usleep
PHP implements Linux command tail-f

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, it is important to note that the file handle is only opened once, so you want to put him outside the loop. Initializes the current file size of File_size and file_size_new to 0. Loop 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, 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
  1. #!/usr/bin/env PHP
  2. if (2! = count ($argv)) {
  3. Fwrite
  4. STDERR,
  5. "Bad call format!" Use the format./tail filename ". Php_eol
  6. );
  7. return 1;
  8. }
  9. $file _name = $argv [1];
  10. Define ("Max_show", 8192);
  11. $file _size = 0;
  12. $file _size_new = 0;
  13. $add _size = 0;
  14. $ignore _size = 0;
  15. $fp = fopen ($file _name, "R");
  16. while (1) {
  17. Clearstatcache ();
  18. $file _size_new = filesize ($file _name);
  19. $add _size = $file _size_new-$file _size;
  20. if ($add _size > 0) {
  21. if ($add _size > Max_show) {
  22. $ignore _size = $add _size-max_show;
  23. $add _size = max_show;
  24. Fseek ($fp, $file _size + $ignore _size);
  25. }
  26. Fwrite
  27. STDOUT,
  28. Fread ($fp, $add _size)
  29. );
  30. $file _size = $file _size_new;
  31. }
  32. Usleep (50000);
  33. }
  34. Fclose ($FP);
Copy Code

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.

2016-02-22 11:28:51 Improvement

Check out the official PHP manual, the Fseek function can be improved here, the function also accepts the third parameter, indicating the type of the offset pointer, the default is Seek_set, from the start offset, can also be set to Seek_cur, which is offset from the current position, so this is changed to Fseek ($FP, $ Ignore_size, $ignore _size);

Here's the result.

PHP, Linux, tail
  • 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.