Pipe Output – Command Like ‘tee’ That Limits File Size and Treats Output File Like a Fixed-Size Queue

bufferpipestdouttee

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

Best Answer

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

Related Question