logrotate,日志文件收割机

2021-10-25  本文已影响0人  halfempty

1 问题

日志很重要, 通过它可以知晓过去一段时间程序经历了什么

我们期望日志记录地更多更全, 于是出现了新的问题

因此需要做出取舍, 通常采取折中的方案, 记录日志的同时, 限制日志的大小以及保留期限

2 案例

/var/log目录下, 能发现大量带日期的日志文件, 如

每类日志包含5个文件, 且日期后缀相差7

如何实现这种滚动效果, 以及如何将效果应用到自定义日志文件中?

比如

  1. 通过nohup启动的程序会生成nohup.out文件
  2. tomcat的catalina.out文件

3 原理

centos7.5最小化安装自带logrotate, 望文生义

3.1 what

man logrotate

手册解释如下

logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files.

3.2 when

logrotate通过cron调度, 执行周期为每天

cat /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

3.3 how

如何知道哪些日志文件需要滚动, 以及如何滚动?

这时就需要知道logrotate的配置文件

全局配置项

详细参数可以参见man logrotate

此时看下系统日志的相关配置, 便能理解日志文件的生成规律了

cat /etc/logrotate.d/syslog 
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
    missingok
    sharedscripts
    postrotate
    /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

那么, 如果想解决nohup.out文件过大, 你知道该如何配置了吗?

上一篇 下一篇

猜你喜欢

热点阅读