1

I have set a logrotate config file like this:

/var/log/tomcat/catalina.out{
copytruncate
daily
dateext
rotate 10
missingok
notifempty
}

And in my /sysconfig/cron settings file I have set DAILY_TIME="00:00" It rotates the logs but if a log has the date catalina.out-20150415 it contains log info from date 20150414, this is one day before.

Should I change the date in the /sysconfig/cron file or maybe I could achive this with the prerotate/endscript option?

As far as I know DAILY_TIME works in 15minute gaps and I would like to have as perfet daily logs that contain just the information of one day as possible. What is what I get now, but the date in the name of the files does not adhere to the contents within them.

Note: I am under SLES, where daily cron jobs work a bit different from other distros.

Watchmaker
  • 759
  • 1
  • 7
  • 16

1 Answers1

1

I wouldn't play with /sysconfig/cron, other than set the daily cronjobs at midnight, and prefer postrotate instead.

Let's suppose today is 17/04/2015, this is the content of your directory after the rotation but before postrotate script has run:

catalina.out-20150414
catalina.out-20150415
catalina.out-20150417
catalina.out

Note that catalina.out-20150416 is missing as 1) the last log file created by the rotation is named catalina.out-20150417 and 2) the log that was created yesterday has been renamed to catalina.out-20150415 to reflect its content.

This scenario allows us to do a "blind renaming" of today log with the date of yesterday, like:

...
postrotate
    mv "catalina.out-$(date +"%Y%m%d")" "catalina.out-$(date --date="yesterday" +"%Y%m%d")" > /dev/null 2>&1 || true
endscript
...

In case of missing log files mv will raise an error message that is redirected to /dev/null and a positive return value is ensured by || true

ColOfAbRiX
  • 1,080
  • 2
  • 12
  • 23
  • I touchted /sysconfig/cront to set the time for the logrotate tasks to run. Its default value is empty, so no problem to change it again. Otherwise I would have to delete cron.daily at a time near the time I want logrotate daily tasks to run. About your solution, mind that notifempty is in the logrotate, so gap names are expected. – Watchmaker Apr 20 '15 at 12:08
  • Sorry, what I mean is that I wouldn't tweak and play with it to achieve the behavior you desire. Moving the daily jobs at midnight is kind of "standard procedure". I too in my servers run the daily jobs at midnight. If there's a gap in file naming nothing happens, simply no file is renamed by mv and its stderr is redirected to /dev/null. I'll clarify "gap names" in the answer – ColOfAbRiX Apr 20 '15 at 12:50
  • I finally adopted a solution close to @ColOfAbRiX's, but a bit more applicable/scalable to any future/past date: `mv "catalina.out-$(date +"%Y%m%d")" "catalina.out-$(date -d "-1 days" +"%Y%m%d")" > /dev/null 2>&1 || true` – Watchmaker May 17 '15 at 10:19
  • Well, after trying I get `mv: cannot stat 'catalina.out-20150517': No such file or directory` I set the DAILY_TIME="00:00" in /etc/sysconfig/cron and the date of my catalina.out-20150517 file is 0:00:01, so I don't understand why it gets that error after rotating the file if the time of the file has already the same day as the day the postrotate is running. – Watchmaker May 19 '15 at 08:09
  • It was my bad, I was missing the absolute path of my catalina.out-YYYYMMDD file, so the correct way would be: `mv "/var/log/tomcat/catalina.out-$(date +"%Y%m%d")" "/var/log/tomcat/catalina.out-$(date -d "-1 days" +"%Y%m%d")" > /dev/null 2>&1 || true` – Watchmaker May 19 '15 at 10:36