Is it possible to trim lines from the top of a .log
file while another process is doing a tail -f
on it using ruby?
1 回答
You can't "trim" the start of the file any more than you could trim the trunk of a tree and expect it to remain the same height.
The "tail" process relies on the current file size to know where to periodically read from. It reads from the last length-of-file to the end-of-file, then remembers that file-length and sleeps. It wakes up and reads forward to the end-of-file again.
If you "remove" lines from the front of the file, which you can't do in a sequential file anyway, you'd shorten the file to be less than the remembered length-of-file value and try to read off the end of the file.
If the purpose is to reduce file size, there are various techniques for file or log rolling. Typically they copy a given number of lines from the end of the file to a new file, then do some file renames to temporarily archive the old file in a ".bak" or time stamped file, allowing the tailing process to continue reading its original file.