Changed logrotate generation count to delete old log files

Tadashi Shigeoka ·  Fri, December 29, 2017

I changed the generation count of logrotate to delete old log files, so I’ll introduce the configuration method.

Linux | リナックス

Changing the generation count of log files managed by logrotate

Let’s look at the default logrotate configuration for Nginx as an example.

/etc/logrotate.d/nginx

/var/log/nginx/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
        endscript
}

rotate is the number of generations to retain log files.

By default it’s 52, so if logrotate runs once a day, it retains log files for 52 days. In my case, 7 days is sufficient, so I changed it to rotate 7.

Even after changing the configuration, old log files won’t be automatically deleted, so you need to manually delete them with commands like rm -f access.log-*.gz.

That’s all from the Gemba where I wanted to change logrotate settings to free up server disk space.