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
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:
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.
I tested this configuration on Centos 7 Server
Links and Official Documentation
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.