一、摘要

Linux服务器上我们用Logrotate来分割归档日志文件,结合crond我们可以指定每天在某个时间自动整理日志等文档。本文主要说明了Centos下Logrotate的使用和配置的方法。

配置文件

Logrotate的配置文件位于 /etc/logrotate.conf。Logrotate的子配置文件位于文件夹 /etc/logrotate.d/ 下,某些软件,入nginx,会在rpm命令安装后会把对应的nginx日志分割文件释放在此,用于定时整理日志文件。

如何使用

我们先试用帮助命令看一下,需要强调的是

-d,其翻译为什么都不做,仅仅是测试,这个参数很大程度方便了我们测试配置文件而不用担心当前的配置出差错。

-f,强制执行日志滚动操作。

 

如果想测试配置文件

# 测试所有logrotate配置

/usr/sbin/logrotate -d -v /etc/logrotate.conf

# 强制执行日志滚动操作,比如nginx

/usr/sbin/logrotate -d -v /etc/logrotate.d/nginx

 

二、实际操作

单文件

确保openresty已经安装好了,默认的日志文件是 /usr/local/openresty/nginx/logs/host.access.log

我们需要对这个文件,每天做一次切割。

 

编辑文件

vim /etc/logrotate.d/openresty

 

内容如下:

/usr/local/openresty/nginx/logs/host.access.log {

daily

rotate 5

compress

copytruncate

dateext

sharedscripts

postrotate

/bin/kill -HUP `cat /usr/local/openresty/nginx/logs/nginx.pid 2> /dev/null` 2> /dev/null || true

endscript

}

 

参数解释:

weekly #默认每天一个日志归档

rotate 5 #最多保存 5 个归档

create #日志滚动后创建一个新的日志文件

dateext #归档文件名加上日期后缀

compress #归档文件是否启用压缩

copytruncate  用于还在打开中的日志文件,把当前日志备份并截断

sharedscripts 作用是在所有的日志文件都轮转完毕后统一执行一次脚本。

dateext表示备份的日志文件后缀格式为YYYYMMDD

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

 

强制执行回滚

/usr/sbin/logrotate -vf /etc/logrotate.d/openresty

 

查看日志目录,就会多出一个gz文件

root@ubuntu:/usr/local/openresty/nginx/logs# ll

total 28

drwxr-xr-x 2 root root 4096 Jul 16 06:25 ./

drwxr-xr-x 12 root root 4096 Jul 15 17:17 ../

-rw-r--r-- 1 root root 417 Jul 15 17:18 access.log

-rw-r--r-- 1 root root 587 Jul 15 18:40 error.log

-rw-r--r-- 1 root root 0 Jul 16 06:25 host.access.log

-rw-r--r-- 1 root root 206 Jul 15 18:41 host.access.log-20190715.gz

-rw-r--r-- 1 root root 5 Jul 16 02:39 nginx.pid

 

理论是每天会执行一次,查看任务计划

root@ubuntu:~# cat /etc/crontab

# /etc/crontab: system-wide crontab

# Unlike any other crontab you don't have to run the `crontab'

# command to install the new version when you edit this file

# and files in /etc/cron.d. These files also have username fields,

# that none of the other crontabs do.

SHELL=/bin/sh

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

# m h dom mon dow user command

17 * * * * root cd / && run-parts --report /etc/cron.hourly

25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )

52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

#

 

可以发现,每天的任务计划,是6:25执行一次。

如果不想等的话,直接修改系统时间,提前一天

date -s "2019-07-16 06:24:58"

 

注意:等到6:25的时候,日志文件,不会立即产生。我猜测,是还有其他的脚本要执行。

等待2分钟,就会出现新的文件。

root@ubuntu:/usr/local/openresty/nginx/logs# ll

total 28

drwxr-xr-x 2 root root 4096 Jul 16 06:25 ./

drwxr-xr-x 12 root root 4096 Jul 15 17:17 ../

-rw-r--r-- 1 root root 417 Jul 15 17:18 access.log

-rw-r--r-- 1 root root 587 Jul 15 18:40 error.log

-rw-r--r-- 1 root root 0 Jul 16 06:25 host.access.log

-rw-r--r-- 1 root root 206 Jul 15 18:41 host.access.log-20190715.gz

-rw-r--r-- 1 root root 201 Jul 15 18:42 host.access.log-20190716.gz

-rw-r--r-- 1 root root 5 Jul 16 02:39 nginx.pid

 

多日志文件与通配符

一个配置条目 日志文件 { 配置项 } 不仅仅支持一个日志文件,可以配置多个文件或使用通配符,如

/var/log/httpd/access.log /var/log/httpd/error.log {

....

}

# 或

/var/log/httpd/access.log

/var/log/httpd/error.log {

...

}

 

通配符的形式

/var/log/news/* {

...

}

/var/log/news/*.log {

...

}

/var/log/*/stdout.log {

...

}

/var/log/*/*.log {

...

}

 

不仅文件名处可以用通配符,目录处也能用通配符。例如最后那个配置 /var/log/*/*.log {...} 将会对 /var/log/ 下所有目录中的 *.log 文件产生效果。比如下面那样的文件

/var/log/aa/x.log

/var/log/bb/y.log

/var/log/cc/z.log

 

针对多个日志文件时,归档文件也会生成在相应日志所在目录中。有了多日志文件与通配符的支持,能够通过一个配置对系统中众多日志文件采取一致的行动。

 

举例

查看 /usr/local/openresty/nginx/logs 目录,有3个日志文件,分别是 access.log,error.log,host.access.log。

host.access.log 已经做了滚动,现在需要对另外个日志文件,也做一下滚动。

有2种写法,先看第一种:通配符的形式

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

daily

rotate 5

compress

copytruncate

dateext

sharedscripts

postrotate

/bin/kill -HUP `cat /usr/local/openresty/nginx/logs/nginx.pid 2> /dev/null` 2> /dev/null || true

endscript

}

 

第二种:多个文件形式

/usr/local/openresty/nginx/logs/access.log

/usr/local/openresty/nginx/logs/error.log

/usr/local/openresty/nginx/logs/host.access.log {

daily

rotate 5

compress

copytruncate

dateext

sharedscripts

postrotate

/bin/kill -HUP `cat /usr/local/openresty/nginx/logs/nginx.pid 2> /dev/null` 2> /dev/null || true

endscript

}

 

这2种写法,效果是一样的。

先强制回滚一下

/usr/sbin/logrotate -vf /etc/logrotate.d/openresty

 

修改日期

date -s "2019-07-17 06:24:58"

 

等待2分钟,再次查看日志目录,效果如下:

root@ubuntu:/etc/logrotate.d# ll /usr/local/openresty/nginx/logs/

total 36

drwxr-xr-x 2 root root 4096 Jul 18 06:25 ./

drwxr-xr-x 12 root root 4096 Jul 15 17:17 ../

-rw-r--r-- 1 root root 0 Jul 18 06:25 access.log

-rw-r--r-- 1 root root 229 Jul 15 17:18 access.log-20190718.gz

-rw-r--r-- 1 root root 0 Jul 18 06:25 error.log

-rw-r--r-- 1 root root 382 Jul 16 18:19 error.log-20190718.gz

-rw-r--r-- 1 root root 0 Jul 18 06:25 host.access.log

-rw-r--r-- 1 root root 206 Jul 15 18:41 host.access.log-20190715.gz

-rw-r--r-- 1 root root 201 Jul 15 18:42 host.access.log-20190716.gz-rw-r--r-- 1 root root 20 Jul 17 06:25 host.access.log-20190718.gz

-rw-r--r-- 1 root root 5 Jul 16 17:48 nginx.pid

 

本文参考链接:

https://blog.phpgao.com/logrotate_conf.html

https://yanbin.blog/linux-config-log-ratation-logrotate/#more-8826

 

参考文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。