Change Logrotate rotation interval to Hourly

If you are here you already know what Logratate is, in case you don’t I will write another tutorial later but the problem I want to see today is how to run logrotate hourly.

Hypothesize my logrotate configuration file looks like this

[root@myserver ] vim /etc/logrotate.d/myfantastic.log
/var/log/myfantastic.log {
           daily
           rotate 7
           compress
       }

In this situation the file “myfantastic.log” will be rotated and compressed daily (saved as tar.gz) and after 7 days the older rotated file will be deleted.

What if I want to rotate it hourly instaed of daily?

Logrotate accepts the following rotate period:

  • hourly
  • daily
  • weekly
  • monthly
  • yearly

But if you type hourly it will not work! Why?

The first thing you need to kwow when we talk about Logrotate is that Logrotate is not a daemon but it is a command run by cron job! Logrotate cron job runs on daily basis so if you set hourly in your configuration file it will be considered once a day (so besically it will have the behavior of daily configuration)

So If you want to execute logrotate on hourly basis you only need to set logrotate cron job on cron hourly configuration just moving logrotate cron job config file from daily directory to hourly directory. Very easy:

[root@myserver ] mv /etc/cron.daily/logrotate /etc/cron.hourly/logrotate
[root@myserver ] systemctl restart crond

Remember to restart crond to see changes applied.

From now on “hourly” option in logrotate config file will be managed properly and other statements (daily, weekly, monthly…) keep working as expected.

 

Environment

I tested this configuration on Centos 7 Server

Links and Official Documentation

Logrotate official 

Disclaimer

It’s not my responsibility if, following this tutorial, you destroy your server, loose your data, damage your company, exterminate whales in the ocean or you will have any issue for yourself or other. Take care.  

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.