Log rotation of stdout?

As an alternative, you could pipe the output through tools designed with the primary purpose of maintaining size-capped, automatically rotated, log file sets, such as:

  • Dan Bernstein's multilog from daemontools
  • Bruce Guenter's multilog from daemontools-encore
  • Laurent Bercot's s6-log from s6
  • Gerrit Pape's svlogd from runit
  • Wayne Marshall's tinylog from perp
  • My cyclog from nosh

Tools to then process multilog-format log file sets include, amongst others:

  • Russ Allbery's multilog-watch
  • logrange
  • Paul Kremer's multilog-stamptail
  • My follow-log-directories from nosh
  • My export-to-rsyslog from nosh

Further reading

  • Jonathan de Boyne Pollard (2015). "Logging". The daemontools family. Frequently Given Answers.
  • Jonathan de Boyne Pollard (2016). Don't use logrotate or newsyslog in this century.. Frequently Given Answers.
  • https://unix.stackexchange.com/a/326166/5132

the rotatelogs tool shipped with apache (in the bin dir) (see docs) takes input from stdin and rotates the log after some specific amount of time


I had similar problem and had initially discard logrotate but it turned out logrotate can actually do this well, the key directive is "copytruncate". For some reason that term didn't come up on any of the googling I did, so I am adding this answer to clarify exactly how to use it for this case.

The trick is this only works if the redirect is done with ">>" (append) instead of ">" (create).

Config File (truncate.cfg):

/tmp/temp.log {
    size 10M
    copytruncate
    rotate 4
    maxage 100
}

Test Program (never gives up file). You can watch it filling disk and though deleting logfile will appear to work it will not actually free up any space on the disk:

cat /dev/urandom >> /tmp/temp.log

Running log rotate:

logrotate truncate.cfg