大网数据广告活动
服务器运维问答

logrotate advanced date in daily rotations

· 52CE 我爱测

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…

问题原文

原帖链接 · Watchmaker · CC BY-SA 3.0

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.

已采纳解答

原帖链接 · ColOfAbRiX · CC BY-SA 3.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-04-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。