1

I've CentOS 7.4 with logrotate 3.8.6 installed. I've a custom logrotate file under /etc/logrotate.d/ to rotate some logs on a Tomcat (e.g., catalina.out) which is installed in the same machine.

/opt/test/apache-tomcat-8.5.15-client/logs/catalina.out {
    copytruncate
    daily
    rotate 30 
    olddir /opt/test/apache-tomcat-8.5.15-client/logs/backup
    compress
    missingok
    maxsize 50M
    dateext
    dateformat .%Y-%m-%d
}

I want the log to be rotated daily or if the size reaches 50MB. When this happens log files are compressed and copied into a backup folder and are kept for 30 days before being deleted.

I already ran logrotate manually in debug mode with the following command and no errors were displayed (and the expected zipped log files were created):

/usr/sbin/logrotate -d /etc/logrotate.d/openncp-tomcat-backoffice 2> /tmp/logrotate.debug

In /var/lib/logrotate/logrotate.status there are no issues, the files are shown as rotated but they're not in fact:

"/var/log/yum.log" 2017-11-27-19:0:0
"/opt/test/apache-tomcat-8.5.15-server/logs/catalina.out" 2017-12-15-3:41:1
"/var/log/boot.log" 2017-12-15-3:41:1
"/var/log/up2date" 2017-11-27-19:0:0

I've the default /etc/logrotate.conf:

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.

I also have the default /etc/cron.daily/logrotate:

#!/bin/sh

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

I ask for your guidance on configuring this appropriately.

Daniel Serodio
  • 4,229
  • 5
  • 37
  • 33
Baderous
  • 1,069
  • 1
  • 11
  • 32
  • 1
    The logrotate cron job is called just once a day, and since you already have the `daily` directive, `maxsize` shouldn't have any effect. Do you expect logs larger than 50M to be rotated immediately? – Benjamin W. Dec 15 '17 at 18:44
  • I need to set a size limit to force rotation because when catalina.out log reaches 2GB Tomcat stops working and this may happen before the end of the day. From the manpage I read: "Log files are rotated when they grow bigger than size bytes even before the additionally specified time interval ( daily, weekly, monthly, or yearly). The related size option is similar except that it is mutually exclusive with the time interval options, and it causes log files to be rotated without regard for the last rotation time. When maxsize is used, both the size and timestamp of a log file are considered." - – Baderous Dec 15 '17 at 18:47
  • Yes, but logrotate is still called just once a day. If you want `maxsize` to kick in more frequently, you have to move `logrotate` from `/etc/cron.daily` to `/etc/cron.hourly`. – Benjamin W. Dec 15 '17 at 18:49
  • But I've noticed that so far the logs have not reached 50MB, and several days have passed without any rotation. – Baderous Dec 15 '17 at 18:50
  • That's strange... they aren't in `/opt/test/apache-tomcat-8.5.15-client/logs/backup`? `daily` alone should put them there every day. – Benjamin W. Dec 15 '17 at 18:52
  • No. In that backup folder I only have the files that were created when I ran logrotate manually in debug mode. – Baderous Dec 15 '17 at 22:28
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Web Applications Stack Exchange](http://webapps.stackexchange.com/), [Webmaster Stack Exchange](http://webmasters.stackexchange.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Dec 16 '17 at 00:37

2 Answers2

0

The problem was related to the SELinux file type of the log files, which were located in a directory different from /var/log, meaning that the logrotate process didn't have access to perform its tasks. I found this other SO thread as well as this Redhat page that helped to solve the issue. I found the Redhat documentation very helpful, so I provide here 2 links:

Baderous
  • 1,069
  • 1
  • 11
  • 32
0

To answer your question (as in the title) about having daily and maxsize, note that by default logrotate runs once a day anyway so that means maxsize is hardly useful in that situation. The file will be rotated anyway (assuming you don't have SELinux in the way, of course).

maxsize is useful with weekly and monthly, of course, because logrotate still checks the files daily.


Note that the fact that logrotate runs daily is just because on many systems it is installed that way by default.

$ ls -l /etc/cron.daily
...
-rwxr-xr-x 1 root root  372 Aug 21  2017 logrotate
...

Moving that file to /etc/cron.hourly is safe and it now will be useful to have the daily option turned on:

$ sudo mv -i /etc/cron.daily/logrotate /etc/cron.hourly/logrotate

WARNING: a system like Ubuntu is likely to re-install the daily file on the next update of the logrotate package, which is rather infrequent, but can happen. I do not know of a clean way to avoid that issue. The ugly way is to create an empty file of the same name which will prevent the packager from adding the file from the package.

$ sudo touch /etc/cron.daily/logrotate

Or edit and put a comment such as:

# placeholder to prevent installer from changing this file
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
  • That is only because the cron script is located in /etc/cron.daily. You can just copy that to /etc/cron.hourly and it will run every hour. – fred Jun 24 '22 at 18:07
  • @fred, True! The default is daily, hence my answer, but I'll add a note about that. – Alexis Wilke Jun 24 '22 at 18:30