一、摘要

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

配置文件

Logrotate的配置文件位于 /etc/logrotate.conf。
Logrotate的子配置文件位于文件夹 /etc/logrotate.d/

如何使用

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

  • -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



 

本文参考链接:

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

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