Linux的logrotate是什么

48次阅读
没有评论

共计 22910 个字符,预计需要花费 58 分钟才能阅读完成。

这篇文章主要讲解了“Linux 的 logrotate 是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着丸趣 TV 小编的思路慢慢深入,一起来研究和学习“Linux 的 logrotate 是什么”吧!

登录到 web 服务上,上来 df -h 一把,果然,挂的一个共享又被撑爆。

root@websrv001 ~ # df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rootvg-rootvol 12G 5.6G 5.6G 50% / tmpfs 14G 0 14G 0% /dev/shm /dev/sda1 477M 84M 368M 19% /boot /dev/mapper/rootvg-homevol 2.0G 4.0M 1.9G 1% /local/home /dev/mapper/rootvg-tmpvol 1.1G 252M 793M 25% /tmp /dev/mapper/rootvg-varvol 5.8G 1.1G 4.5G 20% /var /dev/mapper/fsvol 298G 298G 0 100% /net/fsvol tmpfs 14G 0 14G 0% /var/tmp

上次是因为 liferay 的 log 太大,这次再看看 liferay 的 log

root@websrv001 ~ # ll -h /data/tomcat/storage/BU001/liferay/logs/ total 199M drwxr-xr-x 2 tomcat tomcat 4.0K Apr 20 13:14 . drwxr-xr-x 5 tomcat tomcat 4.0K Mar 14 2016 .. -rw-r--r-- 1 tomcat tomcat 1.5M Apr 13 23:59 liferay.2020-04-13.log.gzipped_on_2020-04-17.gz -rw-r--r-- 1 tomcat tomcat 2.5M Apr 14 23:59 liferay.2020-04-14.log.gzipped_on_2020-04-18.gz -rw-r--r-- 1 tomcat tomcat 7.8M Apr 15 23:59 liferay.2020-04-15.log.gzipped_on_2020-04-19.gz -rw-r--r-- 1 tomcat tomcat 31M Apr 17 23:58 liferay.2020-04-17.log -rw-r--r-- 1 tomcat tomcat 7.0M Apr 18 23:59 liferay.2020-04-18.log -rw-r--r-- 1 tomcat tomcat 90G Apr 20 17:54 liferay.2020-04-20.log

我列个去,尼玛,一个 liferay log 文件,能增长到 90G,这也太扯了 吧。果断删掉,不知道为什么,每当我要敲 rm -rf 的时候,心里就莫名的紧张。。。

root@websrv001 ~ # rm -rf /data/tomcat/storage/BU001/liferay/logs/liferay.2020-04-20.log

重启一下服务

root@websrv001 ~ # service tomcat restart

随后通知了一下开发组那哥们儿,服务器磁盘已经清理,服务已经重启,那边也确认了 web 服务又活了,可以用了。随后,又检查了一下其它文件夹,结果发现 catalina.out 这个日志文件,又是超级无敌大:89G,我也是醉了。

root@websrv001 ~ # ll -h /data/tomcat/server/BU001/logs/ total 104G drwxrwxr-x 2 root tomcat 12K Apr 20 04:45 . drwxr-xr-x 13 root root 4.0K Apr 16 17:26 .. -rw-r--r-- 1 tomcat tomcat 5.1K Apr 13 10:28 catalina.2020-04-13.log.gz -rw-r--r-- 1 tomcat tomcat 13K Apr 14 20:46 catalina.2020-04-14.log.gz -rw-r--r-- 1 tomcat tomcat 7.1K Apr 16 17:51 catalina.2020-04-16.log.gz -rw-r--r-- 1 tomcat tomcat 89G Apr 20 10:39 catalina.out -rw-r--r-- 1 tomcat tomcat 0 Feb 10 23:57 catalina.out-20200210 -rw-r--r-- 1 tomcat tomcat 352M Apr 13 03:38 catalina.out-20200413

问题虽然解决了,但是这个问题本来就不应该发生啊!难道之前做运维的同事都没有做脚本来监控和限制日志大小嘛?这可是生产系统啊!这样可不行,既然没人搞,我就来搞搞。于是网上搜了一把,原来 Linux 系统就自带了一个日志切割神器 mdash; mdash; logrotate

man 了一下 logrotate,看到了以下描述:logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.

Normally, logrotate is run as a daily cron job. It will not modify a log more than once in one day unless the criterion for that log is based on the log s size and logrotate is being run more than once each day, or unless the -f or –force option is used.

原来这个神器是为了方便管理系统大量日志文件而设计的,对日志文件可实现自动 rotation(字面意思是旋转,觉得理解成日志循环更好一些),压缩,删除和通过 email 发送 log 文件。颗粒度控制很好,可以针对每一个 log 文件进行每天,每周,每个月或者当 log 文件过大时进行 rotate。

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

另外,我发现这个神器是系统自带的,可想而知这个工具是多么的重要,否则 Linux 也不会默认就安装了这个工具。

那接下来,我们来看看 logrotate 的相关配置以及如何执行 logrotate。

logrotate 的 conf 文件在 /etc/ 下:

[root@labhost ~]# ll /etc/logrotate. logrotate.conf logrotate.d/ [root@labhost ~]# cat /etc/logrotate.conf

那我们来看看 logrotate.conf 文件里面的内容:

[root@labhost ~]# cat /etc/logrotate.conf # see  man logrotate  for details # rotate log files weekly: 每周 rotate log 文件一次  weekly # keep 4 weeks worth of backlogs:保存最近 4 周的 log 日志,因为上面是每周 rotate 一次  rotate 4 # create new (empty) log files after rotating old ones:rotate 老日志文件之后,创建一个新的空日志文件  create # use date as a suffix of the rotated file:rotate 的文件以日期格式为后缀,比如:access_log-20200422,如果不加这个选项,rotate 的格式为:access_log.1,access_log.2 等等。 dateext # uncomment this if you want your log files compressed:如果想压缩 rotate 后的文件,把下面 compress 前面的 #号去掉就可以了。 #compress # RPM packages drop log rotation information into this directory:RPM 包的日志 rotation 配置信息,建议放到 /etc/logrotate.d 这个文件夹下,实现自定义控制 log 文件 rotate include /etc/logrotate.d # no packages own wtmp and btmp -- we ll rotate them here:wtmp 和 btmp 这两个不属于任何 package,我们就把 rotate 的规则写到这里  /var/log/wtmp { monthly #每个月执行一次 rotate create 0664 root utmp #创建空文件,权限是 664,  所属用户名   所属用户组  minsize 1M #日志文件大小超过 1M 才执行 rotate,否则跳过  rotate 1 #rotate 时,只保留一份 rotate 文件  } /var/log/btmp { missingok monthly create 0600 root utmp rotate 1 } # system-specific logs may be also be configured here.# 其它系统日志也可以在这里配置 rotate 规则 

关于 rotate 规则的配置,从上面的 logrotate.conf 文件里可以看到,有两个地方可以配置:

一种是直接在 logrotate.conf 里配置,不过一般适用于非 RPM 包的系统日志文件。

另外一种是如果你要做 rotate 的日志文件是由第三方 RPM 包软件产生的,需要在 /etc/logrotate.d 这个文件夹下新建一个配置文件,配置相关 rotate 规则。(UnleBen 发现,其实如果你安装了第三方的软件包之后,在 /etc/logrotate.d 这个文件夹下就会自动创建了对应软件的 rotate 配置文件。)

UncleBen 这里准备了一台干净的 CentOS7,在安装 httpd 这个服务之前,我们来看一下 /etc/logrotate.d 这个文件夹下面都有哪些配置文件

[root@labhost ~]# ll /etc/logrotate.d/ total 20 -rw-r--r--. 1 root root 91 Apr 11 2018 bootlog -rw-r--r--. 1 root root 224 Oct 30 2018 syslog -rw-r--r--. 1 root root 100 Oct 31 2018 wpa_supplicant -rw-r--r--. 1 root root 103 Nov 5 2018 yum [root@labhost ~]#

我们现在安装一下 httpd 服务:

[root@labhost ~]# yum install httpd -y Resolving Dependencies --  Running transaction check ---  Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed --  Finished Dependency Resolution ...... Install 1 Package Total download size: 2.7 M Installed size: 9.4 M Downloading packages: httpd-2.4.6-90.el7.centos.x86_64.rpm | 2.7 MB 00:00:04 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : httpd-2.4.6-90.el7.centos.x86_64 1/1 Verifying : httpd-2.4.6-90.el7.centos.x86_64 1/1 Installed: httpd.x86_64 0:2.4.6-90.el7.centos Complete! [root@labhost ~]#

再去 /etc/logrotate.d 这个文件夹下看一下:

[root@labhost ~]# ll /etc/logrotate.d/ total 24 -rw-r--r--. 1 root root 91 Apr 11 2018 bootlog -rw-r--r--. 1 root root 194 Aug 6 2019 httpd -rw-r--r--. 1 root root 224 Oct 30 2018 syslog -rw-r--r--. 1 root root 100 Oct 31 2018 wpa_supplicant -rw-r--r--. 1 root root 103 Nov 5 2018 yum [root@labhost ~]#

可以看到,安装 httpd 之后,自动创建好了针对 httpd 服务的 log rotate 配置文件,打开看看:

[root@labhost ~]# 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 } [root@labhost ~]#

这就是一个标准的,针对第三方软件的一个 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 之后想要执行的脚本,需要放在 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 dateformat -%Y%m%d%H.%s  定义文件 rotate 后的文件名的日期格式,必须配合 dateext 使用,查了一下资料,目前只支持: %Y %m %d %H %s  这几个个参数(年,月,日,时,秒)

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

[root@labhost ~]# 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 [root@labhost ~]#

这里着重说一下 - 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 可执行脚本,那每天就会跑一次啦!

[root@labhost ~]# ll /etc/cron.daily/ total 8 -rwx------. 1 root root 219 Oct 31 2018 logrotate -rwxr-xr-x. 1 root root 618 Oct 30 2018 man-db.cron [root@labhost ~]# ll /etc/cron.weekly/ total 0 [root@labhost ~]# ll /etc/cron.monthly/ total 0 [root@labhost ~]# ll /etc/cron.hourly/ total 4 -rwxr-xr-x. 1 root root 392 Apr 11 2018 0anacron [root@labhost ~]#

那我们来看看 logrotate 脚本里写的什么内容:

[root@labhost ~]# cat /etc/cron.daily/logrotate #!/bin/sh /usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf EXITVALUE=$? if [ $EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate  ALERT exited abnormally with [$EXITVALUE]  fi exit 0 [root@labhost ~]#

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

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

[root@labhost ~]# logrotate /etc/logrotate.d/httpd

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

[root@labhost ~]# 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 [root@labhost ~]#

现在我们把 httpd 这个 logrotate 的流程梳理一下:

httpd 这个 logrotate 的配置文件会每天被执行一次,因为 logrotate 脚本默认放置在 /etc/cron.daily/ 下,脚本会根据 /etc/logrotate.conf 配置文件来执行 logrotate 命令,而针对 httpd 服务的 logrotate 配置文件在 /etc/logrotate.d/ 下,此文件夹被包含在了 /etc/logrotate.conf 配置文件里。

如果 /var/log/httpd/ 下没有以 log 结尾的文件也不会记录错误信息。

如果 /var/log/httpd/ 下的 log 文件是空文件,那么不会执行 rotate 操作。

在执行 logrotate 之后,httpd 服务会被重新加载

对于大多数情况下,每天把日志备份一下已经足够了。但是像 UncleBen 周一遇到的这种 case:一天之内,log 日志把磁盘给撑爆了。那这种 case,用 logrotate 该如何解决呢?

首先我们来捋一下需求:

要解决 log 文件一天之内增长过快,撑爆磁盘的问题。所以关键词就是:一天之内 - 时间,撑爆磁盘 - 文件大小

执行周期

logrotate 默认是每天,我们的需求是一天之内,所以可以让 logrotate 每半天(每 12 小时)、每 8 个小时、每 4 个小时、每 2 个小时,甚至是每小时执行一次,都可以。这个看各位心情。

文件大小

超过多大才执行 logrotate,这个也是根据实际环境来确定。UncleBen 遇到这个 case,日志竟然暴涨到 89G,我这里就设置成超过 1G 大小就进行 rotate。

为了能让各位客官老爷们看到真是的效果,我这里做了一点小小的改动,话不多说了,上 demo:

httpd logrotate 的配置文件更改如下:

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

[root@labhost ~]# 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 } [root@labhost ~]#

然后我来创建一个 cron job,每分钟执行一次:

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

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

[root@labhost ~]# cat monitorlog.sh #!/bin/bash while : do sleep 2 ls -hl /var/log/httpd/ done

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

[root@labhost ~]# ./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 -rw-r--r--. 1 root root 0 Apr 24 14:10 access_log -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.1 -rw-r--r--. 1 root root 370 Apr 24 14:10 error_log -rw-r--r--. 1 root root 12K Apr 24 14:10 error_log.1 total 96K -rw-r--r--. 1 root root 0 Apr 24 14:10 access_log -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.1 -rw-r--r--. 1 root root 370 Apr 24 14:10 error_log -rw-r--r--. 1 root root 12K Apr 24 14:10 error_log.1 ...

我们可以看到:

由于我设定的是每分钟执行一次 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/ 文件夹下信息:

total 96K -rw-r--r--. 1 root root 0 Apr 24 14:10 access_log -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.1 -rw-r--r--. 1 root root 370 Apr 24 14:13 error_log -rw-r--r--. 1 root root 663 Apr 24 14:13 error_log.1 -rw-r--r--. 1 root root 663 Apr 24 14:12 error_log.2 -rw-r--r--. 1 root root 663 Apr 24 14:11 error_log.3

rotate 3 这个参数:

始终保留 3 份 rotate 的文件,那我们多等几分钟,再看看 /var/log/httpd/ 这个文件夹下有哪些文件,即可验证。

rotate 后的文件名:是以. 数字结尾。

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

total 96K -rw-r--r--. 1 root root 0 Apr 24 14:10 access_log -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.1 -rw-r--r--. 1 root root 370 Apr 24 14:14 error_log -rw-r--r--. 1 root root 663 Apr 24 14:14 error_log.1 -rw-r--r--. 1 root root 663 Apr 24 14:13 error_log.2 -rw-r--r--. 1 root root 663 Apr 24 14:12 error_log.3

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

total 96K -rw-r--r--. 1 root root 0 Apr 24 14:10 access_log -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.1 -rw-r--r--. 1 root root 370 Apr 24 14:15 error_log -rw-r--r--. 1 root root 663 Apr 24 14:15 error_log.1 -rw-r--r--. 1 root root 663 Apr 24 14:14 error_log.2 -rw-r--r--. 1 root root 663 Apr 24 14:13 error_log.3

delaycompress 这个参数:

是必须要和 compress 一起使用的,单独使用无效,可以看到没有任何 log 文件被压缩处理,rotate 前和 rotate 后的文件大小保持一致。

postrotate/endscript 这个参数里的脚本:/bin/systemctl reload httpd.service /dev/null 2 /dev/null || true endscript

就是在 log 日志被 rotate 之后,需要重新 reload httpd service,如果没有这个脚本会怎么样呢?我们来到 /etc/logrotate.d/httpd 里,把这 3 行注释掉。再到 monitorlog.sh 执行的结果来看看:

total 92K -rw-r--r--. 1 root root 0 Apr 24 14:10 access_log -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.1 -rw-r--r--. 1 root root 370 Apr 24 14:47 error_log.1 -rw-r--r--. 1 root root 663 Apr 24 14:47 error_log.2 -rw-r--r--. 1 root root 663 Apr 24 14:46 error_log.3

注释掉 postrotate/endscript 脚本后,我们发现:

error_log 文件在执行 rotate 之后,新的 error_log 并没有被创建出来。

我们来刷新一下页面,尝试产生新的 access log,发现可以正常写入,大小有变化。 

total 136K -rw-r--r--. 1 root root 38K Apr 24 14:52 access_log -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.1 -rw-r--r--. 1 root root 5.4K Apr 24 14:52 error_log.1 -rw-r--r--. 1 root root 663 Apr 24 14:47 error_log.2 -rw-r--r--. 1 root root 663 Apr 24 14:46 error_log.3

但是我们等待文件被 rotate 之后,再来观察一下,我们发现,access_log 在被 rotate 之后,新的 access_log 文件同样没有被创建出来。 

total 136K -rw-r--r--. 1 root root 38K Apr 24 14:52 access_log.1 -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.2 -rw-r--r--. 1 root root 5.4K Apr 24 14:52 error_log.1 -rw-r--r--. 1 root root 663 Apr 24 14:47 error_log.2 -rw-r--r--. 1 root root 663 Apr 24 14:46 error_log.3

如果我们继续尝试刷新页面,发现 access_log.1 和 error_log.1 日志大小都会增长。这个就可以说明 postrotate/endscript 中间脚本的作用了,如果没有这个脚本,也就是在执行 logrotate 之后,没有 reload httpd service,那么 httpd 服务会继续往旧的 log 日志中写信息,那么针对 /var/log/httpd/*log 后续的 logrotate 命令都不会被执行。为什么?

配置文件里的文件路径写了:/var/log/httpd/*log,你是针对以 log 结尾的文件名。

现在被 rotate 之后的文件名全都是以 . 数字结尾的。 

total 148K -rw-r--r--. 1 root root 49K Apr 24 15:30 access_log.1 -rw-r--r--. 1 root root 79K Apr 24 14:09 access_log.2 -rw-r--r--. 1 root root 6.9K Apr 24 15:30 error_log.1 -rw-r--r--. 1 root root 663 Apr 24 14:47 error_log.2 -rw-r--r--. 1 root root 663 Apr 24 14:46 error_log.3

记不记得我们还有一个参数叫 copytruncate,我们来试试,在没有 postrotate/endscript 脚本时,加上这个参数是什么效果。

version2:删除 postrotate/endscript 脚本,增加 copytruncate

[root@labhost ~]# cat /etc/logrotate.d/httpd /var/log/httpd/*log { rotate 3 missingok notifempty sharedscripts delaycompress copytruncate }

我们先 stop httpd service,然后删除所有的 access_log 和 error_log,最后 start httpd service,可以看到,httpd 的日志文件正常产生了。

[root@labhost ~]# systemctl stop httpd [root@labhost ~]# rm -rf /var/log/httpd/* [root@labhost ~]# ll /var/log/httpd/ total 0 [root@labhost ~]# [root@labhost ~]# systemctl start httpd [root@labhost ~]# ll /var/log/httpd/ total 4 -rw-r--r--. 1 root root 0 Apr 24 15:47 access_log -rw-r--r--. 1 root root 812 Apr 24 15:47 error_log [root@labhost ~]#

接下来我们再刷新一下页面,产生一些 access log 和 error log,等待几分钟,执行 mornitorlog.sh 来看一下:

access_log 和 error_log 都可以正常 rotate。

新的 access_log 和 error_log 文件都可以被创建出来,并且可以正常写入 log 日志。 

total 60K -rw-r--r--. 1 root root 17K Apr 24 15:51 access_log -rw-r--r--. 1 root root 27K Apr 24 15:51 access_log.1 -rw-r--r--. 1 root root 2.3K Apr 24 15:51 error_log -rw-r--r--. 1 root root 3.6K Apr 24 15:51 error_log.1 -rw-r--r--. 1 root root 812 Apr 24 15:48 error_log.2

所以,比起放在 postrotate/endscript 中间的 reload httpd service 脚本,我更喜欢用 copytruncate 这个参数。

version3:在 version2 的基础上,增加 dateext 参数

[root@labhost ~]# cat /etc/logrotate.d/httpd /var/log/httpd/*log { rotate 3 missingok notifempty sharedscripts delaycompress copytruncate dateext }

我们尝试访问页面,刷新,产生一些 access log 和 error log。等待几分钟,我们执行 monitorlog.sh 来看一下,可以发现:

最近一次被 rotate log 文件时以日期格式结尾的。

我们的 cron job 规定每分钟执行一次 logrotate,但是在添加 dateext 参数之后失效了。所以:

如果你想要 logrotate 每天执行多次(大于一次),就不要添加 dateext 参数。

如果你既想要 logrotate 每天执行多次(大于一次),还想 rotate 之后的文件以日期格式结尾,有一种方法就是添加 dateformat .%s 参数。 

total 144K -rw-r--r--. 1 root root 30K Apr 24 16:55 access_log -rw-r--r--. 1 root root 9.3K Apr 24 15:56 access_log.1 -rw-r--r--. 1 root root 17K Apr 24 15:52 access_log.2 -rw-r--r--. 1 root root 26K Apr 24 16:07 access_log-20200424 -rw-r--r--. 1 root root 27K Apr 24 15:51 access_log.3 -rw-r--r--. 1 root root 4.1K Apr 24 16:55 error_log -rw-r--r--. 1 root root 1.3K Apr 24 15:56 error_log.1 -rw-r--r--. 1 root root 2.3K Apr 24 15:52 error_log.2 -rw-r--r--. 1 root root 3.6K Apr 24 16:07 error_log-20200424 -rw-r--r--. 1 root root 3.6K Apr 24 15:51 error_log.3

version4:在 version3 的基础上,增加 dateformat -%Y%m%d%H.%s 参数

[root@labhost ~]# cat /etc/logrotate.d/httpd /var/log/httpd/*log { rotate 3 missingok notifempty sharedscripts delaycompress copytruncate dateext dateformat -%Y%m%d%H.%s }

我们再次尝试访问页面,刷新,产生一些 access log 和 error log。等待几分钟,我们执行 monitorlog.sh 来看一下,可以发现:

我们的 cron job 现在执行正常了,当 log 文件非空并且有更新时,可以被执行多次。

被 rotate 之后的文件是以日期格式结尾的:

- 横杠开头,接着时年月日时,然后句点. 最后是秒,但是最后的.s% 是从 1970 年 1 月 1 日 00:00:00 到目前经历的秒数。 

total 172K -rw-r--r--. 1 root root 7.5K Apr 24 17:41 access_log -rw-r--r--. 1 root root 26K Apr 24 16:07 access_log-20200424 -rw-r--r--. 1 root root 25K Apr 24 17:32 access_log-2020042417.1587720721 -rw-r--r--. 1 root root 14K Apr 24 17:39 access_log-2020042417.1587721141 -rw-r--r--. 1 root root 3.8K Apr 24 17:40 access_log-2020042417.1587721201 -rw-r--r--. 1 root root 1.1K Apr 24 17:41 error_log -rw-r--r--. 1 root root 3.6K Apr 24 16:07 error_log-20200424 -rw-r--r--. 1 root root 3.3K Apr 24 17:32 error_log-2020042417.1587720721 -rw-r--r--. 1 root root 1.8K Apr 24 17:39 error_log-2020042417.1587721141 -rw-r--r--. 1 root root 514 Apr 24 17:40 error_log-2020042417.1587721201

给大家留个问题:如果 dateformat 定义的格式是:-%Y%m%d%H,那么在日志文件非空并且持续更新的情况下,access_log 和 error_log 会多久被 rotate 一次?

version5:在 version4 的基础上,增加 size 50K 参数

[root@labhost ~]# cat /etc/logrotate.d/httpd /var/log/httpd/*log { size 50K rotate 3 missingok notifempty sharedscripts delaycompress copytruncate dateext dateformat -%Y%m%d%H.%s }

这一次,我们先执行 monitorlog.sh 来看一下 access_log 和 error_log 的大小,再尝试访问页面,刷新,产生一些 access log 和 error log,注意一下,因为我配置的 size 大小是 50K,所以让 access_log 文件大小 50K,然后等待几分钟,观察 monitorlog.sh 输出,可以发现:

access_log 和 error_log 文件都没有被 rotate,因为他们的大小都没有超过 50K,从下面的结果可以看出:

access_log 大小是 49K

error_log 大小是 6.6K 

total 132K -rw-r--r--. 1 root root 49K Apr 24 18:04 access_log -rw-r--r--. 1 root root 26K Apr 24 16:07 access_log-20200424 -rw-r--r--. 1 root root 1.9K Apr 24 17:56 access_log-2020042417.1587722161 -rw-r--r--. 1 root root 3.8K Apr 24 17:58 access_log-2020042417.1587722281 -rw-r--r--. 1 root root 7.5K Apr 24 17:59 access_log-2020042417.1587722341 -rw-r--r--. 1 root root 6.6K Apr 24 18:04 error_log -rw-r--r--. 1 root root 3.6K Apr 24 16:07 error_log-20200424 -rw-r--r--. 1 root root 257 Apr 24 17:56 error_log-2020042417.1587722161 -rw-r--r--. 1 root root 514 Apr 24 17:58 error_log-2020042417.1587722281 -rw-r--r--. 1 root root 1.1K Apr 24 17:59 error_log-2020042417.1587722341

现在我们继续刷新页面,让 access_log 大小 50K,观察 monitorlog.sh 输出,可以发现:

Apr 24 18:12 时

logrotate 被执行了一次,由于 access_log 文件大小 50K,所以就被 rotate 了。并保存为:access_log-2020042418.1587723121

由于 error_log 文件大小 50K, 所以此次 logrotate 忽略了这个文件。 

total 132K -rw-r--r--. 1 root root 52K Apr 24 18:11 access_log -rw-r--r--. 1 root root 26K Apr 24 16:07 access_log-20200424 -rw-r--r--. 1 root root 1.9K Apr 24 17:56 access_log-2020042417.1587722161 -rw-r--r--. 1 root root 3.8K Apr 24 17:58 access_log-2020042417.1587722281 -rw-r--r--. 1 root root 7.5K Apr 24 17:59 access_log-2020042417.1587722341 -rw-r--r--. 1 root root 7.1K Apr 24 18:11 error_log -rw-r--r--. 1 root root 3.6K Apr 24 16:07 error_log-20200424 -rw-r--r--. 1 root root 257 Apr 24 17:56 error_log-2020042417.1587722161 -rw-r--r--. 1 root root 514 Apr 24 17:58 error_log-2020042417.1587722281 -rw-r--r--. 1 root root 1.1K Apr 24 17:59 error_log-2020042417.1587722341 total 128K -rw-r--r--. 1 root root 0 Apr 24 18:12 access_log -rw-r--r--. 1 root root 26K Apr 24 16:07 access_log-20200424 -rw-r--r--. 1 root root 3.8K Apr 24 17:58 access_log-2020042417.1587722281 -rw-r--r--. 1 root root 7.5K Apr 24 17:59 access_log-2020042417.1587722341 -rw-r--r--. 1 root root 52K Apr 24 18:12 access_log-2020042418.1587723121 -rw-r--r--. 1 root root 7.1K Apr 24 18:11 error_log -rw-r--r--. 1 root root 3.6K Apr 24 16:07 error_log-20200424 -rw-r--r--. 1 root root 257 Apr 24 17:56 error_log-2020042417.1587722161 -rw-r--r--. 1 root root 514 Apr 24 17:58 error_log-2020042417.1587722281 -rw-r--r--. 1 root root 1.1K Apr 24 17:59 error_log-2020042417.1587722341

version6:在 version5 的基础上,增加 compress 参数

[root@labhost ~]# cat /etc/logrotate.d/httpd /var/log/httpd/*log { compress size 50K rotate 3 missingok notifempty sharedscripts delaycompress copytruncate dateext dateformat -%Y%m%d%H.%s }

我们继续刷新页面,让 access_log 大小 50K,观察 monitorlog.sh 输出,可以发现:

Apr 24 17:59 时

logrotate 被执行了一次,由于 access_log 文件大小 50K,所以就被 rotate 了,并且被做了压缩处理,保存为:access_log-2020042417.1587722341.gz

被压缩之前大小是 7.5K,被压缩后是 398,不到 1k,所以强烈推荐添加这个参数,节省空间啊!!!

由于 error_log 文件大小 50K, 所以此次 logrotate 忽略了这个文件。 

total 168K -rw-r--r--. 1 root root 0 Apr 24 18:20 access_log -rw-r--r--. 1 root root 26K Apr 24 16:07 access_log-20200424 -rw-r--r--. 1 root root 398 Apr 24 17:59 access_log-2020042417.1587722341.gz -rw-r--r--. 1 root root 1.3K Apr 24 18:12 access_log-2020042418.1587723121.gz -rw-r--r--. 1 root root 84K Apr 24 18:20 access_log-2020042418.1587723601 -rw-r--r--. 1 root root 19K Apr 24 18:19 error_log -rw-r--r--. 1 root root 3.6K Apr 24 16:07 error_log-20200424 -rw-r--r--. 1 root root 257 Apr 24 17:56 error_log-2020042417.1587722161 -rw-r--r--. 1 root root 514 Apr 24 17:58 error_log-2020042417.1587722281 -rw-r--r--. 1 root root 1.1K Apr 24 17:59 error_log-2020042417.1587722341

感谢各位的阅读,以上就是“Linux 的 logrotate 是什么”的内容了,经过本文的学习后,相信大家对 Linux 的 logrotate 是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是丸趣 TV,丸趣 TV 小编将为大家推送更多相关知识点的文章,欢迎关注!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-25发表,共计22910字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)