Linux系统中Logrotate工具用法汇总

来源:互联网 发布:python数组shape(0) 编辑:程序博客网 时间:2024/06/05 20:53

Linux系统中Logrotate工具用法汇总

 

兄弟连IT教育官方与大家分享Linux系统中Logrotate工具用法汇总,简单的过程中其实暗藏玄机,还是要细心学习,喜欢还请记得收藏哦!

Linux系统操作中,Logrotate是一款日志管理工具,可对Linux日志进行处理,在使用前,需要对Logrotate工具进行配置,下面就给大家介绍下Linux中Logrotate工具的用法,一起来了解下吧。

1运行原理

 

Logrotate是基于CRON来运行的,其脚本是/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

 

exit 0

 

/etc/cron.daily目录所有脚本运行频率是由CRON通过/etc/crontab控制,

 

有两种运行方式: 1 直接执行单个命令; 2 目录规划,下面仅列举目录规划配置

 

SHELL=/bin/bash

 

PATH=/sbin:/bin:/usr/sbin:/usr/bin

 

MAILTO=root

 

HOME=/

 

# run-parts

 

*/1 * * * * root run-parts /etc/cron.min

 

01 * * * * root run-parts /etc/cron.hourly

 

59 23 * * * root run-parts /etc/cron.daily

 

22 4 * * 0 root run-parts /etc/cron.weekly

 

42 4 1 * * root run-parts /etc/cron.monthly

 

2配置文件

 

/etc/logrotate.conf 全局默认文件

 

/etc/logrotate.d/ 目录,下属文件通过include纳入前者

 

常用选项

 

weekly 《==默认一周执行一次 rotate 工作

 

rotate 4 《==保留多少个日志文件。默认保留四个。

 

create 《==创建新的文件。因为日志被改名,因此要创建一个新的来继续存储之前的日志

 

dateext 《==文件后缀是日期格式,也就是切割后文件是:xxx.log-20131216,如果注释掉,切割出来是按数字递增,即前面说的 xxx.log-1

 

compress 《==是否压缩日志。

 

include /etc/logrotate.d # 将 /etc/logrotate.d/ 目录中的所有文件都加载进来

 

/var/log/wtmp { 《==仅针对 /var/log/wtmp 所设定的参数

 

monthly 《==每月一次切割,取代默认的一周

 

minsize 1M 《==文件大小超过 1M 后才会切割

 

create 0664 root utmp 《==指定新建的日志文件权限以及所属用户和组

 

rotate 1 《==只保留一个日志。

 

}

 

通过include加载的文件示例如下

 

root@www ~]# vi /etc/logrotate.d/syslog

 

/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron

 

{

 

sharedscripts

 

prerotate

 

/usr/bin/chattr -a /var/log/messages

 

endscript

 

sharedscripts

 

postrotate

 

/bin/kill -HUP `cat /var/run/syslogd.pid 2》 /dev/null` 2》 /dev/null || true

 

/bin/kill -HUP `cat /var/run/rsyslogd.pid 2》 /dev/null` 2》 /dev/null || true

 

/usr/bin/chattr +a /var/log/messages

 

endscript

 

}

 

日志文件: 被处理的日志绝对路径。使用空格符分隔多个文件名;

 

执行脚本:

 

可调用外部指令来进行额外的命令,这个设定需与 sharedscripts 。。。。 endscript 设定合用才行。命令介绍:

 

prerotate:在启动 logrotate 之前进行的指令,例如修改文件的属性等动作;

 

postrotate:在做完 logrotate 之后启动的指令,例如重新启动 (kill -HUP) 某个服务;

 

那么 /etc/logrotate.d/syslog 内设定的六个文件的切割功能就变成了:

 

1.该设定只对 /var/log/ 内的 messages, secure, maillog, spooler, boot.log, cron 有效;

 

2.日志切割每周一次、保留四个、且切割下来的日志文件不进行压缩(未更改预设值);

 

3.切割完毕后 (postrotate) 取得 syslog 的 PID 后,以 kill -HUP 重新启动 syslogd

 

切割案例:Nginx

 

============================

 

/etc/logrotate.d 新建 nginx

 

/usr/local/nginx/logs/*.log {

 

daily

 

rotate 5

 

dateext

 

compress

 

sharedscripts

 

postrotate

 

if [ -f /usr/local/nginx/logs/nginx.pid ]; then

 

kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

 

fi

 

endscript

 

}

 

上面就是Linux中Logrotate工具的用法介绍了,本文介绍了Logrotate工具的运行原理和Logrotate文件的配置,如果你要管理Linux系统日志,可选择使用Logrotate日志管理工具。

0 0
原创粉丝点击