Showing posts with label logrotate not running automatically. Show all posts
Showing posts with label logrotate not running automatically. Show all posts

Thursday, November 10, 2022

Logrotate is not running automatically in Redhat or CentOS

When you run logrotate command manually, it creates archived gz files successfully in the directory which you mentioned in the config file /etc/logrotate.d/custom-config but it does not run this command using daily cron automatically.

This is the most common problem of logrotate generally which you do not get in Ubuntu. Even if you want to run logrotate for your default log directory i.e. /var/log in Redhat or CentOS, you might not get this issue. You are getting this issue because you want to run logrotate automatically for your non-default directory like /opt/httpd/logs, /opt/odoo/logs or /opt/tomcat/logs etc.

Anacron runs logrotate using cron.daily. The issue is SELinux does not provide access to run logrotate using anacron for non-default log directories. It needs to mention explicitly to run logrotate for non default log directories.

There are two solutions of this issue.

Solution : 1

Do not depend on anacron. Create your own cron using root user. Log into the terminal using root user and set daily cron.

00 05 * * * /usr/sbin/logrotate -f /etc/logrotate.conf

Solution : 2

Set SELinux to run logrotate using anacron for non-default directories.

Follow this solution.

https://access.redhat.com/solutions/39006

Run commands as root user.

semanage fcontext -a -t var_log_t '/opt/httpd/logs(/.*)?'
cat /etc/selinux/targeted/contexts/files/file_contexts.local
restorecon -Frvv /opt/httpd/logs

It will solve your problem to run logrotate using cron automatically.