logrotate:
??logrotate 程序是一个日志文件管理工具。用来把旧的日志文件删除,并创建新的日志文件,称为日志转储或滚动。
作用:
??可以根据日志文件的大小,也可以根据其天数来转储,这个过程一般通过 cron 程序来执行
logrotate 相关文件:
-
计划任务:/etc/cron.daily/logrotate
-
程序文件:/usr/sbin/logrotate
-
配置文件: /etc/logrotate.conf
-
日志文件:/var/lib/logrotate/logrotate.status
说明:
-
logrotate程序是系统自带的。
-
logrotate的实现是基于计划任务的。程序本身被计划任务调用
#每天执行一次logrotate,会调用/etc/logrotate.conf定义的日志转储规则来对日志进行操作
[root@LogServer ~]# vim /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit $EXITVALUE
#这个文件定义了日志转储的规则
#这是一个全局性配置,没有特殊约定的都按照这个规则执行
[root@LogServer ~]# vim /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly #每周做一次日志的转储
# keep 4 weeks worth of backlogs
rotate 4 #表示只保存最近的四个日志文件
# create new (empty) log files after rotating old ones
create #表示老日志文件滚动以后,创建一个同名的新日志文件
# use date as a suffix of the rotated file
dateext #旧的日志文件会添加一个时间后缀
# uncomment this if you want your log files compressed
#compress
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d #存放指定服务的配置
# system-specific logs may be also be configured here.
例如:
#btmp日志转储的配置
[root@LogServer logrotate.d]# cat btmp
# no packages own btmp -- we'll rotate it here
/var/log/btmp { #/var/log/btmp表示对btm这个文件定义转储规则
missingok #如果日志不存在,不提示错误,继续处理下一个
monthly #表示一个月转储一次
create 0600 root utmp #生成新文件的权限 所有者 所属组
rotate 1 #只保存最近的一个文件
}
范例: 设置nginx的日志转储
cat /etc/logrotate.d/nginx
/var/log/nginx/*.log { #/var/log/nginx路径下只要是log结尾的文件都进行转储 -- 表示针对谁进行规则配置
daily #指定转储周期为每天
rotate 100 #表示保留最近的100哥文件
missingok #如果日志不存在,不提示错误,继续处理下一个
compress #对转储的日志文件加逆行压缩
delaycompress #和compress一起使用时,转储的日志文件到下一次转储时才压缩。延迟压缩
notifempty #not if empty:如果日志文件为空旧不进行转储
create 644 ngnix nginx #创建的新文件 文件权限-所有者-所属组
postrotate #
if [ -f /app/nginx/logs/nginx.pid ]; then #如果进程生成的这个pid文件存在
kill -USR1 `cat /app/nginx/logs/nginx.pid` # 在转储以后需要执行的命令 发送一个信号重新加载nginx进程
fi
endscript #postrotate和endscript中的内容为对日志文件转储后的操作
}
范例:对指定日志手动执行日志转储
#生成测试日志
[root@centos8 ~]#dd if=/dev/zero of=/var/log/test1.log bs=2M count=1
#创建日志转储配置文件
[root@centos8 ~]#cat /etc/logrotate.d/test1
/var/log/test1.log { #表示要对那个文件进行转储
daily #每天转储一次
rotate 5 #只保存最近的五个文件
compress #对转储的日志文件进行压缩
delaycompress #不立即压缩,而是再下一次转储的时候再压缩
missingok #如果这个文件不存在,不提示错误
size 1M #文件大小超过1M就进行转储
notifempty #如果/var/log/test1.log为空就不进行转储
create 640 bin nobody #新创建的/var/log/test1.log 文件权限为640,所有者为bin,所属组为nobody
postrotate
echo `date +%F_%T` >> /var/log/test1.log #转储完成后,对新生成的/var/log/test1.log执行操作
endscript
}
#手动执行日志转储
[root@LogServer log]# logrotate /etc/logrotate.d/test1
#查看内容
[root@LogServer log]# cat test1.log
2022-10-20_15:54:26
触发总配置文件:子配置文件里面没定义的就按照总配置文件的对应配置来进行转储。
配置参数:
配置参数 | 说明 |
---|---|
compress | 通过gzip压缩转储以后的日志 |
nocompress | 不压缩 |
copytruncate | 用于还在打开中的日志文件,把当前日志备份并截断 |
nocopytruncate | 备份日志文件但是不截断 |
create mode owner group | 转储文件,使用指定的权限,所有者,所属组创建新的日志文件 |
nocreate | 不建立新的日志文件 |
delaycompre |
首页 上一页 1 2 下一页 尾页 1/2/2 | |
【大 中 小】【打印】 【繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部】 | |
上一篇:nginx做负载均衡服务器,配置动静.. | 下一篇:实现基于文件验证的vsftpd虚拟用户 |