how to rotate log files by filesize?

647 views Asked by At

ATTENTION: there are many articles about rotating log files by date/time. But i want to rotate log files by filesize. Is it possible?


i have apache access_log file which is growing largely and taking up the entire server free space after 2/3 weeks. As a result my server sees no more free space after 2/3 weeks and i have to manually clean up the log file every 2/3 weeks.

Here is what i want to do:

  • rotate the access_log file at every 5 MegaBytes.
  • don't want to have more than 20 such files.
  • if we reach 20x5MB files, then should remove the oldest log file first before creating a new rotation

Is it possible to do this?

1

There are 1 answers

1
dervishe On

Use the "size" parameter in the logrotate.conf to fix the max file size of the log file and the "rotate" option fix the number of rotation thus you can put:

/var/log/apache2/access_log {
    missingok
    notifempty
    size 5M
    rotate 20
    create 0600 apache2 apache2
} 

as usual, man logrotate is your best friend