Linux – Free-space driven log rotation on linux

linuxlogginglogrotate

Someone just asked me 'how long should we keep logs for our application', and my answer was 'until the disk is full' as there's no reason to throw them away other than running out of space.

However, standard logrotate wants us to specify a specific period + number of rotations. Is there something similar that would let us say "rotate daily, and keep as much history as you like until there is only 5% space free"?

The platform is Redhat Linux.

Best Answer

You can perhaps use firstaction or lastaction directives to call a shell script that tests for disk free space, and then run a delete on the oldest files.

   firstaction/endscript
          The lines between firstaction and endscript (both of which must appear on lines by themselves) are
          executed (using /bin/sh) once before all log files that match the wildcarded pattern are  rotated,
          before  prerotate  script  is  run  and  only if at least one log will actually be rotated.  These
          directives may only appear inside a log file definition. Whole pattern is passed to the script  as
          first  argument.  If  the script exits with error, no further processing is done. See also lastac-
          tion.

Update:

Here's a Stackoverflow post on the type of script you can run:

https://stackoverflow.com/questions/7523059/remove-oldest-file-in-repository