Debugging logrotate scripts

by Sebastien Mirolo on Tue, 21 Dec 2021

Many times debugging logrotate scripts rely on making changes and waiting for the next day to check the results. Today we will see how to be more efficient and debug logrotate scripts until they work.

Running logrotate in debug mode

First the -d option will show the steps logrotate would take (equivalent to -v) without effectively executing the rotation.

Terminal
/usr/sbin/logrotate -d

Debugging a single logrotate script

When debugging, it can be tempting to run the command against a single logrotate config file.

Terminal
/usr/sbin/logrotate -d /etc/logrotate.d/nginx

The problem with this approach is that logrotate will not take into account the global variables defined in /etc/logrotate.conf then. Thus be aware to define all global configurations in your local conf file (ex: dateext, dateformat) or run the command against the same configuration file as defined in the cron job (/etc/cron.daily/logrotate).

Terminal
/usr/sbin/logrotate -d /etc/logrotate.conf

Forcing a rotation

When testing dateformat, postrotate or lastaction for example, it might be tempting to run with -vf command line options. The problem here is that the files are actually rotated and it might be difficult to get back into a situation where you can test again, without loosing data in the process.

Best is to change the status file logrotate keeps about rotation times. You can find the name of that statefull file either through man logrotate or in the commands executed by /etc/cron.daily/logrotate. Typically it is a good idea to debug logrotate scripts through the command run by the cron script.

For example, if you are looking to understand the effect of logrotate on a /var/log/nginx/access.log file, you can just update the appropriate line in the status file (/var/lib/logrotate/logrotate.status) with a date far enough in the past.

Terminal
sed -i.bak -e 's,\("/var/log/nginx/access.log"\) [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]:[0-9][0-9]:[0-9][0-9],\1 2021-12-19-00:00:00,' /var/lib/logrotate/logrotate.status

More to read

You might also like to read Logrotate, S3 storage, and AWStats or Fast-tracking server errors to a log aggregator on S3.

More technical posts are also available on the DjaoDjin blog, as well as business lessons we learned running a SaaS application hosting platform.

by Sebastien Mirolo on Tue, 21 Dec 2021


Receive news about DjaoDjin in your inbox.

Bring fully-featured SaaS products to production faster.

Follow us on