[YBA] No cyclical file in Linux?
Oleg Goldshmidt
pub at goldshmidt.org
Mon Mar 1 19:57:26 IST 2010
On Mon, Mar 1, 2010 at 4:51 PM, Jonathan Ben Avraham <yba at tkos.co.il> wrote:
> Hi Linux-IL subscribers,
> I don't see any cyclical file in Linux. I see plenty of solutions based on
> logrotate, etc. but no simple cyclical file. Is there such a thing that I
> can implement in a JFFS?
What do you mean by "implement" and what are the exact requirements?
You can implement your own file type in the kernel. IIRC journalling
(in ext3 and other filesystems) is implemented using a circular buffer
that gets periodically committed to disk. I believe the code is
somewhere in fs/ext3 - look for JBD (journal block device).
I suspect you do not mean kernel hacking. I have some vague
recollection that squid had a circular file somewhere for the cache -
maybe look at that?
If all you want is rotate at most N lines as you, e.g., log something,
maybe a poor man's solution will do? Something almost as simple as
#!/bin/awk -f
BEGIN {
MAXLINES=1000
}
{ printf "until [ $(/usr/bin/wc -l %s | /bin/cut -d' ' -f1) -lt %d ];
do /bin/sed -i '1d' %s; done; echo \"%s\" >> %s\n",\
FILE,MAXLINES,FILE,$0,FILE | "/bin/bash" }
- assuming the script is called "circular" and is in the path, try
$ cat | circular FILE=<filename> [MAXLINES=<n>]
and start typing and checking the file as you go. (NB: I used this
roundabout way to write and execute a shell script in awk since awk
allows me to keep processing input while there is some. There is more
than one way to...).
Caveat: I suspect "tail -f" will not work as naively expected (with
any circular file, not just the stupid script above).
--
Oleg Goldshmidt | oleg at goldshmidt.org
More information about the Linux-il
mailing list