Jun 21

logrotate basics (where config file are, how to restart, etc.)

Category: Linux,webserver   — Published by goeszen on June 21, 2015 at 1:49 pm

Logrotate is a utility which rotates logs in regular intervals.

The main: logrotate config file is at /etc/logrotate.conf, but as with most things on Debian, there is a hierarchy of more config files in /etc/logrotate.d/
depending on what services and packages you've installed. For example if you run Apache, there will be an Apache config file with log rotation directives for logfiles generated by Apache,
also for exim (probably your MTA), or things like that.

logrotate is not a daemon or service! - it's merely a script run by cron

Now, after you've done changes to the config files, you may wonder how to restart logrotate? Or apply configuration. Well, you DON'T . logrotate is being run from cron, in regular intervals, but it's not a daemon or always-on service. So the next time cron will issue logrotate, your configuration will be executed.

Test your logrotate configuration

In case you want to test your configuration changes, you can tell logrotate to do a dry-run like so:

$ logrotate -d /etc/logrotate.conf

The -d swtich tells logrotate to run in debug mode, so that it'll tell you what it would do, but it doesn't touch the log files at all. /etc/logrotate.conf is the main config file, and it also references all the split config files usually found on Debian.

How to name rotated (archived) log files with a timestamp

Add

dateext
dateformat _%Y-%m-%d

to configuration sections in your config files and logrotate will name your archived log-files like access.log_1999-09-19.gz with a timestamp instead of the usual numbering with ascending numbers. But make sure you do this in a scheme where log files are not rotated twice a day (as timestamping/filenames have to be unique and be lexically sortable. Also note that older versions of logrotate might not have the dateformat option yet, so check the man page)

Leave a Reply

=