adrift on a cosmic ocean

Writings on various topics (mostly technical) from Oliver Hookins and Angela Collins. We have lived in Berlin since 2009, have two kids, and have far too little time to really justify having a blog.

Excluding files from logrotate globbing matches

Posted by Oliver on the 21st of May, 2014 in category Tech
Tagged with: excludinglogrotatepostrotateprerotateregex

A few days ago I wrote about logging with Upstart and the various implications that has if you want more frequent log rotation than the defaults provide, or do something other than rotation with the files that logrotate cannot do.

There doesn't seem to be any good canonical source of information about how to exclude certain files from the logrotation so I thought I'd make a small note about it here. This is how I've done it in this case:

/var/log/upstart/*.log {
        daily
        missingok
        rotate 7
        compress
        notifempty
    nocreate
   nosharedscripts
    prerotate
      bash -c "[[ ! $1 =~ testprogram ]]"
    endscript
}

The bulk of the logrotate fragment is identical to what Upstart places on the disk itself from its own package. The exceptions are the nosharedscripts directive (I believe it is a default, but I put it there to be sure) which ensures that prerotate and postrotate scripts are run for each individual file matched by the glob (/var/log/upstart/*.log in this case). Then, I run a short bash command which uses its regular expression support to match the log file being considered against the name of the program I wish to exclude. When this matches, I invert the status code and the prerotate script exits non-zero - effectively avoiding any rotation for this file. All other files don't match, the prerotate script exits with a zero status code and is rotated as expected.

© 2010-2018 Oliver Hookins and Angela Collins