2

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"?

1

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 "tee plus".

  • With Dan Bernstein's multilog from daemontools and Bruce Guenter's multilog from daemontools-encore:

    someprogram | multilog n2 s1048576 ./logdir/
  • With Laurent Bercot's s6-log from s6:

    someprogram | s6-log n2 s1048576 ./logdir/
  • Gerrit Pape's svlogd from runit with a configuration file that says n2 s1048576:

    someprogram | svlogd ./logdir/
  • Wayne Marshall's tinylog from perp:

    someprogram | tinylog -k 1 -s 1048576 ./logdir/
  • With my cyclog from nosh:

    someprogram | cyclog --max-file-size 1048576 --max-total-size 1048576 ./logdir/

Further reading

1

In package apache2-utils is present utility called rotatelogs, it fully meet to your requirements.

Synopsis:

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.