Logrotate: run every N hours

19,526

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
Share:
19,526
Adam Matan
Author by

Adam Matan

Team leader, developer, and public speaker. I build end-to-end apps using modern cloud infrastructure, especially serverless tools. My current position is R&D Manager at Corvid by Wix.com, a serverless platform for rapid web app generation. My CV and contact details are available on my Github README.

Updated on September 18, 2022

Comments

  • Adam Matan
    Adam Matan over 1 year

    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
    Adam Matan over 9 years
    +1, But that would cause every log in the system (not just /var/log/uwsgi/*/*.log) to be rotated every 6 hours.
  • muru
    muru over 9 years
    @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.