logrotate切割日志

2019-06-28 23:34:00
CJL
原创
3168

大部分linux系统自带logrotate

可以通过logrotate来自动进行日志切割

添加配置文件在/etc/logrotate.d

/var/log/nginx/*log {  #文件名
    create 0644 nginx nginx
    daily 
    rotate 10 
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}


配置参数

说明

monthly

日志文件将按月轮循。其它可用值为'daily''weekly'或者'yearly'

rotate 5

一次将存储5个归档日志。对于第六个归档,时间最久的归档将被删除。

compress

在轮循任务完成后,已轮循的归档将使用gzip进行压缩。

delaycompress

总是与compress选项一起用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩将在下一次轮循周期进行。这在你或任何软件仍然需要读取最新归档时很有用。

missingok

在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误。

notifempty

如果日志文件为空,轮循不会进行。

create 644 root root

以指定的权限创建全新的日志文件,同时logrotate也会重命名原始日志文件。

postrotate/endscript

在所有其它指令完成后,postrotateendscript里面指定的命令将被执行。在这种情况下,rsyslogd 进程将立即再次读取其配置并继续运行。

具体配置项参考:man logrotate



发表评论
评论通过审核后显示。
流量统计