Linux logrotate,日志文件收割机

1 问题

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

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

  • 日志量庞大, 存储吃紧
  • 单日志文件过大, 不利于检索

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

2 案例

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

  • cron
  • maillog
  • messages
  • secure
  • vsftpd

每类日志包含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的配置文件

  • /etc/logrotate.conf
  • /etc/logrotate.d/*

全局配置项

  • weekly, 滚动周期为一周
  • rotate 4, 保留4份滚动日志
  • create, 触发滚动条件后, 重新创建新文件
  • dateext, 滚动日志文件添加日期(YYYYMMDD)后缀
  • compress, 压缩滚动日志

详细参数可以参见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文件过大, 你知道该如何配置了吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值