CentOS 8安装logrotate切割日志_centos8 logrotate

•log文件的路径(支持通配符*),再加上一对花括号{}
•花括号里面的内容就是logrotate的规则参数,需要单独成行。

参数解读:

missingok: 如果日志不存在,不产生错误信息

notifempty:如果日志文件为空,不做rotate,这个跟你ifempty互斥

sharedscripts:配合prerotate and postrotate 使用,sharedscripts 开启的话,如果使用了对需要做rotate的log文件使用了

通配符,那么*

prerotate** and postrotate 的脚本只会执行一次。没有sharedscripts 的话,每个需要log文件做rotate的时候都会执行一遍prerotate and postrotate 的脚本

delaycompress:延迟压缩旧的日志文件,先rotate,不进行压缩,等到下次rotate时,才会压缩上次rotate的文件。这个需要跟compress一起使用,单独使用不生效。

postrotate/endscript:rotate之后想要执行的脚本,需要放在postrotateendscript中间,这两个选项要单独成行。

除了上面的参数外,还有以下常见参数:

daily            指定rotate周期为每天,还有weekly, monthly。
rotate count    指定rotate日志文件保留的数量,如果没有配置这个参数,就不保留备份,设置1的话,就是保                    留1个rotate备份,10就是保留10个rotate备份。
size size        当日志文件到达指定的大小时才转储,默认的大小单位是bytes,可以以k,M,G。比如size 10k,                  10M, 10G。
compress        通过gzip压缩然后备份日志。
nocompress      默认值,不做gzip压缩处理。
delaycompress   和compress配合使用,rotate的日志文件到下一次执行logrotate时才进行压缩处理。
copytruncate    把当前日志备份并截断,先拷贝原日志文件再清空,由于拷贝和清空之间有一个时间差,可能会丢失部                 分日志数据。
create mode owner group        rotate之后,创建新文件的日志文件并指定新文件的属性,比如:
                            create 644 tomcat tomcat
mail address     把rotate的日志文件发送到指定的E-mail。
dateext          使用当期日期作为命名格式,如果指定的rotate大于1,默认rotate之后的文件名是:xx.log.1,                     xx.log.2, xx.log3,如果配置dateext规则,那么rotate之后的文件名就会以日期结                     尾:xx.log.2020-04-20,xx.log.2020-04-21, xx.log.2020-04-22

rotate的规则是配置好了,但是怎么来执行呢?我们来help一下。

# logrotate --help
Usage: logrotate [OPTION...] <configfile>
  -d, --debug               Don't do anything, just test (implies -v)
  -f, --force               Force file rotation
  -m, --mail=command        Command to send mail (instead of `/bin/mail')
  -s, --state=statefile     Path of state file
  -v, --verbose             Display messages during rotation
  -l, --log=STRING          Log file
  --version                 Display version information

Help options:
  -?, --help                Show this help message
  --usage                   Display brief usage message

这里着重说一下-d和-f两个选项:

-d:debug模式,就是先演练一遍,不是真干。

-f:强制模式,实打实的执行。

从上面我们man logrotate的信息可以了解到:一般情况下呢,logrotate是基于每天的cron job来执行的,所以对某个log文件的rotae每天执行一次,除非rotate的执行标准是基于log文件大小,并且你logrotate这个命令每天被执行了多次,也就是你自定义了定时任务,让logrotate每x分钟或者每x小时执行一次,再或者你手动执行这个命令添加了-f/–force这个选项。

为什么是每天执行一次呢?我们去/etc/cron.daily/,/etc/cron.weekly/,/etc/cron.monthly/,/etc/cron.hourly/可以看到,在/etc/cron.daily/这个文件夹下有一个logrotate可执行脚本,那每天就会跑一次啦!
在这里插入图片描述
那我们来看看logrotate脚本里写的什么内容:

# 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
exit $EXITVALUE

在这里插入图片描述
简单理解就是执行logrotate命令,并且指定执行logrotate时状态文件和执行的conf文件:/etc/logrotate.conf。大家还记不记得在这个文件里,有一行:include /etc/logrotate.d,也就是说在执行logrotate.conf这个文件时,一并执行/etc/logrotate.d文件夹下的配置文件。下面几行就是执行出错时,logger一下。

所以,我们执行logrotate时,可以直接运行:logrotate + confi文件名

如果一切顺利,那么logrotate命令会执行成功,httpd的日志文件会被按照你配置的规则进行rotate。但是如果没有达到你的期望结果,你就需要来debug了。其实推荐大家在放到生产环境之前,最好好是用-d选项来测试一下你写的配置文件是否可以达到你的预期。我们来看一下加上-d选项的运行结果:

# logrotate /etc/logrotate.d/httpd

#logrotate -d /etc/logrotate.d/httpd
reading config file /etc/logrotate.d/httpd
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /var/log/httpd/*log  1048576 bytes (no old logs will be kept)
empty log files are not rotated, old logs are removed
considering log /var/log/httpd/access_log
  log does not need rotating (log size is below the 'size' threshold)
considering log /var/log/httpd/error_log
  log does not need rotating (log size is below the 'size' threshold)
not running postrotate script, since no logs were rotated

在这里插入图片描述
httpd logrotate的配置文件更改如下:

version1:在原来基础上增加了一行:rotate 3

# cat /etc/logrotate.d/httpd
/var/log/httpd/*log {
    rotate 3
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
    endscript
}

在这里插入图片描述
然后我来创建一个cron job,每分钟执行一次:

*/1 * * * * /usr/sbin/logrotate /etc/logrotate.d/httpd

然后创建一个监控httpd文件夹下log文件的脚本:

# cat monitorlog.sh
#!/bin/bash
while :
do
    sleep 2
    ls -hl /var/log/httpd/
done

我们来执行一下monitorlog.sh,来观察一下httpd的日志文件。注意:我这里是访问了Apache默认的站点,然后手动刷新Apache站点,来产生点access log,然后停止刷新页面。

# ./monitorlog.sh
total 140K
-rw-r--r--. 1 root root 79K Apr 24 14:09 access_log
-rw-r--r--. 1 root root 12K Apr 24 14:09 error_log
total 140K
-rw-r--r--. 1 root root 79K Apr 24 14:09 access_log
-rw-r--r--. 1 root root 12K Apr 24 14:09 error_log
total 96K

在这里插入图片描述
我们可以看到:

•由于我设定的是每分钟执行一次logrotate,并且指定了httpd这个logrotate配置文件,所以我们配置的规则会每分钟被执行一次。
•/var/log/httpd/这个文件夹下:
•只有access_log和error_log这两个文件。
•Apr 24 14:09时,access_log文件大小是79K, error_log文件大小是12K。
•停止刷新页面,等待一分钟之后
•/var/log/httpd/这个文件夹下:
•增加了两个文件:access_log.1和error_log.1
•Apr 24 14:10时,access_log文件被清空,大小为0;而error_log不是空的,证明一直有error信息产生。
•为了验证notifempty这个参数是否生效,我们不刷新页面,再多等两分钟,然后从monitorlog.sh执行的结果来看:
•access_log没有被rotate,因为在第一次access log文件被rotate之后,没有访问日志产生,它的大小为0,我们在配置文件里加了notifempty这个参数,意思就是如果为空,不做rotate。

Apr 24 14:13 /var/log/httpd/文件夹下信息:
在这里插入图片描述

由于新安装的apache,没有新生成日志,所以

rotate 3这个参数:

•始终保留3份rotate的文件,那我们多等几分钟,再看看/var/log/httpd/这个文件夹下有哪些文件,即可验证。
•rotate后的文件名:是以.数字结尾。

Apr 24 14:14 /var/log/httpd/文件夹下信息:

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

哪些文件,即可验证。
•rotate后的文件名:是以.数字结尾。

Apr 24 14:14 /var/log/httpd/文件夹下信息:

[外链图片转存中…(img-OLcBQwdI-1726054707403)]
[外链图片转存中…(img-rxuUSzQC-1726054707403)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

  • 11
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值