3

Consider a server which generates extensive logging, which are archived using logrotate and bz2:

"/var/log/uwsgi/*/*.log" {
  copytruncate
  daily
  rotate 14
  compress
  delaycompress
  missingok
  notifempty
  compresscmd /bin/bzip2
  uncompresscmd /bin/bunzip2
  compressext .bz2
}

Using hourly rotation is not convenient for log inspection in real time (problems tend to happen just when the hour changes), and daily rotation gets the disk full far to often.

Is there a way to set logrotate to run every given amount of hours? An invocation every 6 hours would be perfect for my case.

Adam Matan
  • 12,519

1 Answers1

6

Running the logrotate commad with -f in a cronjob could be enough. From man logrotate:

-f, --force
      Tells logrotate to force the rotation, even if it doesn't  think
      this  is  necessary.   Sometimes this is useful after adding new
      entries to a logrotate config file, or if  old  log  files  have
      been  removed  by  hand,  as  the new files will be created, and
      logging will continue correctly.

So edit /etc/crontab and add:

30 */6 * * * root logrotate -f /etc/logrotate.conf

Change the minute to suit your needs. The */6 means it should run every six hours.


This will rotate all log files, so isolate the settings for the application to a (self-contained) config file, then use that as the parameter. For example, create /etc/logrotate.d/uwsgi, containing:

"/var/log/uwsgi/*/*.log" {
  copytruncate
  daily
  rotate 14
  compress
  delaycompress
  missingok
  notifempty
  compresscmd /bin/bzip2
  uncompresscmd /bin/bunzip2
  compressext .bz2
}

along with any other lines in /etc/logrotate.conf that you may be implicitly relying on. Then the crontab entry will look like:

30 */6 * * * root logrotate -f /etc/logrotate.d/uwsgi
muru
  • 197,895
  • 55
  • 485
  • 740
  • 1
    +1, But that would cause every log in the system (not just /var/log/uwsgi/*/*.log) to be rotated every 6 hours. – Adam Matan Nov 12 '14 at 05:52
  • 1
    @AdamMatan If you can isolate the settings for that particular set of logs into a config file, you can pass that config file as an argument to logrotate. – muru Nov 12 '14 at 05:53