3

I'm trying to reduce the number of log files kept for Apache by reducing 'rotate 52' in /etc/logrotate.d/apache2 to 'rotate 8'. With weekly rotation this should keep 2 months of logs instead of a full year.

I deployed the new configuration via Puppet to our dozen servers and noticed it works differently than I expected. I expected it to remove the Apache error and access logs logs older than 8 weeks, i.e. 9-52. Instead it kept all old logs and simply doesn't create any new ones that would exceed that 8 week limit! Rotation 9 is now missing, but 10-52 still exist.

Searching the archive showed me this is known behaviour, e.g. Logrotate does not remove old logs

How do I get rid of the old logs easily? I want to avoid manually deleting them on each server. I don't have any orchestration software deployed yet, so Puppet will have to do.

My /etc/logrotate.d/apache2 file reads:

/var/log/apache2/*.log {
    weekly
    missingok
    rotate 8
    compress
    delaycompress
    notifempty
    create 644 root adm
    sharedscripts
    postrotate
        /etc/init.d/apache2 reload > /dev/null
    endscript
    prerotate
        if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
            run-parts /etc/logrotate.d/httpd-prerotate; \
        fi; \
    endscript
}
4
  • What's your logrotate configuration ? Oct 14, 2014 at 9:55
  • @XavierLucas I've added it to the question. Oct 14, 2014 at 9:59
  • @XavierLucas That's certainly a good solution. Why don't you enter that as an answer? Oct 14, 2014 at 11:23
  • You are right, just did it. Oct 14, 2014 at 11:27

1 Answer 1

6

You could add a postrotate script to your current block in order to delete files older than n days with the following :

find /var/log/apache2 -maxdepth 1 -mtime +<n> -name "*.log.*.gz" -delete &>/dev/null

Wait for the next rotation then remove it and let logrotate do its normal job again.

1
  • 1
    Be aware that find -mtime operates on the filesystem's modification time, not the date in the filename.
    – David Lord
    Jan 26, 2016 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.