网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[root@localhost ~]# ll /etc/logrotate.d
总用量 56
-rw-r–r–. 1 root root 91 5月 14 2019 bootlog
-rw-r–r–. 1 root root 130 2月 19 2018 btmp
-rw-r–r–. 1 root root 160 4月 4 2018 chrony
-rw-r–r–. 1 root root 526 1月 4 2019 dnf
-rw-r–r–. 1 root root 194 5月 20 12:31 httpd
可以看到,安装httpd之后,自动创建好了针对httpd服务的log rotate配置文件,打开看看:
cat /etc/logrotate.d/httpd
/var/log/httpd/*log {
missingok
notifempty
sharedscripts
delaycompress
postrotate
/bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
endscript
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/cad5f34dcd6d490b92a0fe57bda5bb4e.png)
**格式:**
•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之后想要执行的脚本,需要放在**postrotate** 与 **endscript**中间,这两个选项要单独成行。
除了上面的参数外,还有以下常见参数:
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…]
-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可执行脚本,那每天就会跑一次啦!
![在这里插入图片描述](https://img-blog.csdnimg.cn/2d07883371d44b92b236a01ffd91d42e.png)
那我们来看看logrotate脚本里写的什么内容:
cat /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [
E
X
I
T
V
A
L
U
E
!
=
0
]
;
t
h
e
n
/
u
s
r
/
b
i
n
/
l
o
g
g
e
r
−
t
l
o
g
r
o
t
a
t
e
"
A
L
E
R
T
e
x
i
t
e
d
a
b
n
o
r
m
a
l
l
y
w
i
t
h
[
EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate "ALERT exited abnormally with [
EXITVALUE!=0];then/usr/bin/logger−tlogrotate"ALERTexitedabnormallywith[EXITVALUE]"
fi
exit $EXITVALUE
![在这里插入图片描述](https://img-blog.csdnimg.cn/da133df973364b57bf5c06f5e0b5d1fb.png)
简单理解就是执行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
![在这里插入图片描述](https://img-blog.csdnimg.cn/7eb406344da243f2a71b38e65031e170.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwOTA3OTc3,size_16,color_FFFFFF,t_70)
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
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/ffe90bdd853246df93c4804fd480b789.png)
然后我来创建一个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
![在这里插入图片描述](https://img-blog.csdnimg.cn/26c5f5b4cf9a46fbbaf1ca64ccbdd1fe.png)
我们可以看到:
•由于我设定的是每分钟执行一次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/文件夹下信息:
![在这里插入图片描述](https://img-blog.csdnimg.cn/f5f332d7eb2e4b4cab0da9b1b9c6c5d3.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwOTA3OTc3,size_16,color_FFFFFF,t_70)
>
> 由于新安装的apache,没有新生成日志,所以
>
>
>
rotate 3这个参数:
•始终保留3份rotate的文件,那我们多等几分钟,再看看/var/log/httpd/这个文件夹下有哪些文件,即可验证。
•rotate后的文件名:是以.数字结尾。
**Apr 24 14:14 /var/log/httpd/文件夹下信息:**
total 96K
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
…(img-8HshcflG-1715731775419)]
[外链图片转存中…(img-6cqvfT17-1715731775419)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新