I have a Linux program which can write information to stdout and stderr.

I have a shell script which redirects that output to a file in /var/log. (Via >> and 2>&1.)

Is there a way to make that log file rotate? (max size, then switch to a different file, keep only a limited number of files)

I've seen a few answers which talk about the logrotate program, which sounds good, but they also seem to be focused on programs which are generating log files internally and handle HUP signals. Is there a way to make this work with a basic output redirection script?


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