Logrotate日志切割

日志切割Logrotate

关于日志切割

logrotate程序是一个日志文件管理工具。用于分割日志文件,删除旧的日志文件,并创建新的日志文件,起到“转储”作用。
可以节省磁盘空间
配置完后,logrotate的运作完全自动化,不必进行任何进一步的人为干预。

安装Logrotate

系统版本说明

[root@CentOS ~]# uname -r
2.6.32-696.el6.x86_64
[root@CentOS ~]# cat /etc/redhat-release 
CentOS release 6.9 (Final)

安装方法

默认centos系统安装自带logrotate,安装方法如下

yum -y install logrotate crontabs 

配置文件介绍

Linux系统默认安装logrotate工具,它默认的配置文件在:

/etc/logrotate.conf
/etc/logrotate.d/

logrotate.conf 是主要的配置文件
logrotate.d 是一个目录,该目录里的所有文件都会被主动的读入/etc/logrotate.conf中执行。
另外,如果 /etc/logrotate.d/ 里面的文件中没有设定一些细节,则会以/etc/logrotate.conf这个文件的设定来作为默认值。

[root@CentOS ~]# rpm -ql logrotate
/etc/cron.daily/logrotate
/etc/logrotate.conf   ## 主配置文件
/etc/logrotate.d      # 配置目录
/usr/sbin/logrotate
/usr/share/doc/logrotate-3.7.8
/usr/share/doc/logrotate-3.7.8/CHANGES
/usr/share/doc/logrotate-3.7.8/COPYING
/usr/share/man/man5/logrotate.conf.5.gz
/usr/share/man/man8/logrotate.8.gz
/var/lib/logrotate.status

配置Logrotate

测试Logrotate管理日志

创建日志,然后填入一个10MB的随机比特流数据文件

[root@CentOS ~]# touch /var/log/ceshi-log
[root@CentOS ~]# ll /var/log/ceshi-log
-rw-r--r-- 1 root root 0 Nov 13 08:45 /var/log/ceshi-log
[root@CentOS ~]# head -c 10M < /dev/urandom > /var/log/ceshi-log 
[root@CentOS ~]# ll /var/log/ceshi-log
-rw-r--r-- 1 root root 10485760 Nov 13 08:46 /var/log/ceshi-log

创建一个配置文件

配置logrotate来轮循该日志文件
此处模板是通用的,配置参数则根据实际需求进行调整,不是所有的参数都是必要的。
当然也可以通过man手册中的例子进行配置。

[root@CentOS ~]# vim /etc/logrotate.d/ceshi-log
/var/log/ceshi-log {
    monthly
    rotate 5
    compress
    delaycompress
    missingok
    notifempty
    create 644 root root
    postrotate
        /usr/bin/killall -HUP rsyslogd
    endscript
}

配置参数详解

配置参数 说明
monthly 日志文件将按月轮循。其它可用值为'daily','weekly'或者'yearly'。
rotate 5 一次将存储5个归档日志。对于第六个归档,时间最久的归档将被删除。
compress 在轮循任务完成后,已轮循的归档将使用gzip进行压缩。
delaycompress 总是与compress选项一起用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩将在下一次轮循周期进行。这在你或任何软件仍然需要读取最新归档时很有用。
missingok 在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误。
notifempty 如果日志文件为空,轮循不会进行。
create 644 root root 以指定的权限创建全新的日志文件,同时logrotate也会重命名原始日志文件。
postrotate/endscript 在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种情况下,rsyslogd 进程将立即再次读取其配置并继续运行。

以上信息来源 "man logrotate"

手动运行Logrotate

logrotate可以在任何时候从命令行手动调用。要调用为/etc/lograte.d/下配置的所有日志调用logrotate:
logrotate可以在任何时候从命令行手动调用。要调用为/etc/lograte.d/下配置的所有日志调用logrotate:

[root@CentOS ~]# logrotate /etc/logrotate.conf

为特定的配置调用logrotate,执行一次切割任务测试
-f选项来强制logrotate轮循日志文件
-v参数提供了详细的输出

[root@CentOS ~]# ll /var/log/ceshi-log
-rw-r--r-- 1 root root 0 Nov 13 09:01 /var/log/ceshi-log
[root@CentOS ~]# logrotate -vf /etc/logrotate.d/ceshi-log
[root@CentOS ~]# ll /var/log/ceshi-log
ceshi-log    ceshi-log.1

Logrotate的记录日志

logrotate的日志一般存放于/var/lib/logrotate/status目录下。如果我们想要logrotate记录到指定的文件,可以从命令行指定。

[root@CentOS ~]# logrotate -vf -s /var/lib/logrotate.status /etc/logrotate.d/ceshi-log 
reading config file /etc/logrotate.d/ceshi-log
reading config info for /var/log/ceshi-log 

Handling 1 logs

rotating pattern: /var/log/ceshi-log  forced from command line (5 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/ceshi-log
  log does not need rotating
not running postrotate script, since no logs were rotated

Logrotate定时任务

logrotate的定时任务在安装时就自动创建了

[root@CentOS ~]# cat /etc/cron.daily/logrotate 
#!/bin/sh

/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi

Logrotate生产应用

为nginx设置日志切割
防止访问日志文件过大

[root@CentOS ~]# cat /etc/logrotate.d/nginx 
/var/log/nginx/*log {
    daily
    rotate 10
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null || :
    endscript
}

猜你喜欢

转载自www.cnblogs.com/wenrulaogou/p/9950436.html
今日推荐