日志分隔工具logrotate的学习使用

Linux日志文件总管logrotate的学习使用

logrotate是个十分有用的工具,它可以自动对日志进行截断(或轮循)、压缩以及删除旧的日志文件。例如,你可以设置logrotate,让/var/log/nginx日志文件每30天轮循,并删除超过6个月的日志。配置完后,logrotate的运作完全自动化,不必进行任何进一步的人为干预。另外,旧日志也可以通过电子邮件发送

1.0 安装logrotate

yum -y install logrotate crontabs

安装完成后,自动在/etc/cron.daily/下生成个logrotate文件与 生成/etc/logrotate.d/ /etc/logrotate.conf

其中系统自带的每1天执行的cron计划配置文件放在/etc/cron.daily/目录下

在主配置中可以看到 /etc/logrotate.d 说明我们可以将用户定义的配置直接放到这下面,系统会自动为我们执行。当然,系统的并不能很好的满足我们需求。

1.1 安装完成启动需要启动rsyslog

service rsyslog start

logrotate的配置文件是/etc/logrotate.conf,通常不需要对它进行修改。日志文件的轮循设置在独立的配置文件中,它(们)放在/etc/logrotate.d/目录下。

2.0 进到脚本目录下创建脚本文件

/etc/logrotate.d/

[root@my ~]# cd /etc/logrotate.d/
[root@my cron.daily]# vim
# 输入以下内容
/var/log/nginx/*.log {
    # 指定转储周期为每天  日志文件将按规则轮循。其它可用值为'monthly','weekly'或者'yearly'。
    daily
    # 在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误。
	missingok
	# rotate 30 一次将存储30个归档日志。对于第31个归档,时间最久的归档将被删除。
    rotate 30
    # 通过gzip 压缩转储以后的日志
    #compress
	#和 compress 一起使用时,转储的日志文件到下一次转储时才压缩 
    #delaycompress
    # 如果日志文件为空,轮循不会进行
    notifempty
	# 日志以日期命名
	dateext
	dateformat -%Y-%m-%d
	extension  .log
    # 以指定的权限创建全新的日志文件,同时logrotate也会重命名原始日志文件
    create 0640 root root
    # 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
    olddir /usr/local/zhihao_nginx  
    # postrotate/endscript, 在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种情况下,rsyslogd 进程将立即再次读取其配置并继续运行。
    sharedscripts
    postrotate
		/bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}
# 然后  在编写窗口 wq nginx_log_sp  保存文件, 然后查看
[root@my logrotate.d]# ll
total 52
-rw-r--r--  1 root root  162 Sep 15 23:50 kvm_stat
-rw-r--r--  1 root root  242 Oct  8  2019 nginx
-rwxrwxrwx  1 root root 1329 Sep 17 14:05 nginx_log_sp
..................................

USR1亦通常被用来告知应用程序重载配置文件;例如,向Apache HTTP服务器发送一个USR1信号将导致以下步骤的发生:停止接受新的连接,等待当前连接停止,重新载入配置文件,重新打开日志文件,重启服务器,从而实现相对平滑的不关机的更改

手动调用测试看看是否配置可以

[root@my cron.daily]# /usr/sbin/logrotate -vf /etc/logrotate.d/nginx_log_sp

修改logrotate默认的执行时间

Logrotate是基于CRON运行的,所以这个时间是由CRON控制的,

如果使用的是新版CentOS,那么配置文件为:/etc/anacrontab

# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1	5	cron.daily		nice run-parts /etc/cron.daily
7	25	cron.weekly		nice run-parts /etc/cron.weekly
@monthly 45	cron.monthly		nice run-parts /etc/cron.monthly

使用 anacrontab 轮转的配置文件,日志切割的生效时间是在凌晨 3 点到 22 点之间,而且随机延迟时间是 45 分钟,但是这样配置无法满足我们在现实中的应用

现在的需求是将切割时间调整到每天的晚上 12 点,即每天切割的日志是前一天的 0-24 点之间的内容,操作如下

mv /etc/anacrontab /etc/anacrontab.bak          // 取消日志自动轮转的设置 

使用 「/etc/crontab」来作为日志轮转的触发容器来修改 logrotate 默认执行时间

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
59 23 * * * root run-parts /etc/cron.daily
# 备用, 上面那个无效时候, 自定义定时执行命令
59 23 * * * root /usr/sbin/logrotate -vf /etc/logrotate.d/nginx_log_sp

修改完配置文件, 重载定时cron : sudo service crond reload 重新加载定时配置完成

1.如何测试logrotate程序执行的情况

/usr/sbin/logrotate -d /etc/logrotate.d/nginx_log_sp

2.怎么查看log文件的具体执行情况

cat /var/lib/logrotate/status

3.使用-v或-d参数时,显示log does not need rotating

logrotate在对status未记录的文件进行转储时,会在status添加一条该文件的记录,并将操作时间设为当天。之后程序再次对此文件进行转储时发现这个文件今天已经操作过,就不再进行相关操作。

解决方法:

1. vi /var/lib/logrotate/status 更改相对应的文件操作日期

2. 使用-s指定状态文件

4.分割日志时报错:error: skipping "/var/log/nginx/test.access.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.

添加“su root list”到/etc/logrotate.d/nginx文件中即可

/var/log/nginx/*.log {
        su root list
        daily
 # .......其他省略

常用配置说明

4K3op4.png

参考文档

https://blog.huoding.com/2013/04/21/246

1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懵懵懂懂程序员

如果节省了你的时间, 请鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值