logrotate测试_Linux下日志管理工具Logrotate

本文介绍了Linux系统自带的日志管理工具Logrotate,用于按周期切割、压缩日志文件,避免日志过大。文章详细解析了Logrotate的配置文件参数,并给出了Tomcat和Nginx日志转储的配置示例,同时讲解了如何调试和设置定时任务执行。
摘要由CSDN通过智能技术生成

背景:

项目上的Nginx和Tomcat已经跑了大半年了,Nginx的access.log和error.log将近1G大小;Tomcat下的catalina.out日志经常跑到打不出日志然后进行手动移动到别的目录,重新生成新的catalina.out文件来解决。

Logrotate:

logrotate是Linux系统自带的日志切割工具,可以按月、周、天来切割日志文件并压缩,解决了日志备份以及备份日志的可读性。其基于crontab运行,系统自动触发。

logrotate配置文件相关:

运行脚本/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/logrotate.d/下:

-rw-r--r--. 1 root root 178 Nov 24 2015 chrony

-rw-r--r--. 1 root root 71 Jul 21 2015 cups

-rw-r--r--. 1 root root 172 Nov 20 2015 iscsiuiolog

-rw-r--r--. 1 root root 165 Nov 21 2015 libvirtd

-rw-r--r--. 1 root root 163 Nov 21 2015 libvirtd.qemu

-rw-r--r--. 1 root root 893 Nov 21 2015 mariadb

-rw-r--r--. 1 root root 106 Mar 6 2015 numad

-rw-r--r--. 1 root root 136 Jun 10 2014 ppp

-rw-r--r--. 1 root root 408 Mar 6 2015 psacct

-rw-r--r--. 1 root root 115 Nov 21 2015 samba

-rw-r--r--. 1 root root 224 Sep 8 2015 syslog

-rw-r--r--. 1 root root 100 Jun 16 2015 wpa_supplicant

-rw-r--r--. 1 root root 100 Dec 3 2015 yum

配置文件参数详解:

compress:对转储后的日志文件进行压缩;

nocompress:不对转储的日志文件进行压缩;

delaycompress:与 compress 一起使用,转储的日志文件到下一次转储进行压缩;

nodelaycompress:与 compress 一起使用,转储的日志文件本次转储进行压缩;

copytruncate:备份正在打开的日志文件,将当前节点之前的文件备份出来,然后清空源文件;

nocopytruncate:直接备份整个日志文件,不做切割处理;

create mode owner group:使用指定属性创建新文件;

nocreate:不建立新的日志文件;

ifempty:如果文件为空也转储;

notifempty:如果是空文件的话,不转储;

missingok:如果日志文件丢失,不报错继续下一个日志文件转储;

errors address:把转储时的错误信息发送到指定的Email 地址;

mail address:把转储的日志文件发送到指定的E-mail 地址;

nomail:转储时不发邮件;

olddir directory:转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统;

noolddir:转储后的日志文件和当前日志文件放在同一个目录下

prerotate/endscript:在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行;

postrotate/endscript:在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行;

daily:指定转储周期为每天;

weekly:指定转储周期为每周;

monthly:指定转储周期为每月;

dateext:使用当前日期作为转储文件的命名格式,没有此配置转储文件以1、3、4....为格式;

dateformat:配合dateext使用,定义转储文件格式,只支持%Y%m%d%s四个参数;

rotate count:指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份;

size size:当日志文件到达指定的大小时才转储,Size 可以指定 bytes 以及KB 或者MB;

Tomcat日志catalina.out日志配置转储:

vim /etc/logrotate.d/tomcat

/data/opt/tomcat8/logs/catalina.out {

daily

rotate 30

compress

delaycompress

copytruncate

notifempty

missingok

dateext

}

Nginx日志access.log和error.log日志配置转储:

vim /etc/logrotate.d/nginx

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

daily

rotate 365

missingok

notifempty

compress

dateext

sharedscripts

postrotate

systemctl reload nginx.service

endscript

}

调试与测试:

logrotate -d /etc/logrotate.d/nginx #调试模式

#logrotate命令参数

logrotate --help

Usage: logrotate [OPTION...]

-d, --debug 调试模式,输出调试结果并不执行;

-f, --force 强制模式,对所有文件进行rotate操作;

-m, --mail=command 发送邮件(instead of `/bin/mail')

-s, --state=statefile 状态文件;

-v, --verbose 显示debug信息,配合-d参数使用;

--version 显示版本信息;

设置logrotate定时任务执行时间:

vim /etc/anacrontab

# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh

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

MAILTO=root

# the maximal random delay added to the base delay of the jobs

RANDOM_DELAY=0 #默认为45即45分钟,设置0即延迟时间为0;

# the jobs will be started during the following hours only

START_HOURS_RANGE=0-1 #默认3-22,即凌晨3点到22点,设置为0-1;

#period in days delay in minutes job-identifier command

1 5 cron.daily nice run-parts /etc/cron.daily

7 25 cron.weekly nice run-parts /etc/cron.weekly

@monthly 45 cron.monthly nice run-parts /etc/cron.monthly

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值