0
Is there a way to create special files under Linux that would keep only, say the 100 last written lines? I have a process filling a log file, and I'd like to regularly parse its 100 last lines.
I know I could use some kind of logrotate, but is there a way to create a special file that would fill up till it reaches 100 lines, then, adding a line removes the oldest one, so that the file only keeps 100 lines? (a kind of line-based FIFO)
Thanks a lot
2
tail
does that, so you could write a cron job to fill the file on whatever refresh interval you want. just overwrite withtail <file> > <targetfile>
. if your refresh rate is high enough, it will act like a FIFO buffer. – Frank Thomas – 2016-02-15T22:51:22.7071
While it would be cool to have a custom virtual file system that you can
– David Betz – 2016-02-16T00:31:34.840cat
to get derived content (like a ring buffer), these aren't very popular. They are called log-structured file systems. It would be perfect for that scenario. https://en.wikipedia.org/wiki/Log-structured_file_system . For now, I suggest you just write a script that does a tail -n 100 to itself.Thanks, I was not aware of such filesystems! The ideal would have been special files that act like such ring buffers (that would avoid needing to install and mount a whole FS, and I thought mainstream Linux distros already supported this). I'll definitely end up using
tail
, but I'm happy I have learnt something :) – Daladim – 2016-02-16T21:49:47.473In general there is a way to create files that are just what you want them to be: custom FUSE (Filesystem in Userspace). It is not as easy as you probably hoped for, but it's possible. – Kamil Maciorowski – 2016-02-22T09:48:42.303