Does there exist a command where one can do something like:
someprogram | tee --limit=1MB afile
Whereby "afile" would contain the most recent 1 MB of output of "someprogram"?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
Exactly that, probably not. Once it had warmed up and filled the file, it would be rewriting 1MiB-N bytes of old data, to shuffle them along the file, for every N bytes of new data that it added to the end of the buffer. This is not exactly an ideal mechanism, and moreover reading the file as it is being written would be prone to "tearing" problems.
Close to that, many commands exist. Raise the number of files to 2, a current file and its immediate predecessor, so that at any given time one has between 1MiB and 2MiB of the most recent output, with output not being copied around once it has been written; and you have something that a whole bunch of daemontools-family logging programs do by design. They are, in essence, exactly this sort of "
multilogfrom daemontools and Bruce Guenter's
someprogram | multilog n2 s1048576 ./logdir/
someprogram | s6-log n2 s1048576 ./logdir/
svlogdfrom runit with a configuration file that says
someprogram | svlogd ./logdir/
someprogram | tinylog -k 1 -s 1048576 ./logdir/
someprogram | cyclog --max-file-size 1048576 --max-total-size 1048576 ./logdir/
apache2-utils is present utility called
rotatelogs, it fully meet to your requirements.
rotatelogs [ -l ] [ -L linkname ] [ -p program ] [ -f ] [ -t ] [ -v ] [ -e ] [ -c ] [ -n number-of-files ] logfile rotationtime|filesize(B|K|M|G) [ offset ]
Full manual you may read on this link.