logrotate
是Linux默认自带日志文件分割工具,结合Linux默认自带的crond
定时服务,可实现nginx
、tomcat
等应用日志的定时自动分割及清理,大大节省服务器磁盘空间,也方便运维人员按日期排查应用相关日志。crond
+ logrotate
实现nginx
应用日志自动清理及备份root
用户操作,通过Linux自带logrotate工具
+ crond服务
实现nginx
日志每天定时自动分割及清理。进入正文~
logrotate
版本号logrotate --version
logrotate
重要文件及目录说明文件或目录 | 说明 |
---|---|
/etc/logrotate.conf | logrotate 主配置文件,默认文件分割配置方案,文件中还配置了include 加载/etc/logrotate.d/ 下所有子配置文件内容 |
/etc/logrotate.d/ | logrotate 子配置文件,自定义文件分割配置方案,不同应用单独文件配置,增强可读性,同时会覆盖/etc/logrotate.conf 中相同的参数配置 |
/usr/sbin/logrotate | 二进制可执行文件,执行命令 /usr/sbin/logrotate 选项 /etc/logrotate.conf |
/usr/sbin/logrotate 选项
-d, --debug
debug模式,不执行任何操作,仅测试,方便调试-f, --force
强制转存文件-m, --mail=command
发送日志到指定邮箱-s, --state=statefile
状态记录文件-v, --verbose
显示转存过程信息-l, --log=STRING
日志文件查看crond
服务运行状态
systemctl status crond.service
running
表示运行状态~
查看crond
服务后台进程
ps -ef|grep crond
可以看到crond
的守护进程是通过/usr/sbin/crond -n
来实现的。
crond
的后台进程,会每分钟去加载是否有要执行的定时任务。
crond
服务其他常用命令
systemctl status crond.service
systemctl start crond.service
systemctl restart crond.service
systemctl stop crond.service
systemctl reload crond.service
配置/etc/anacrontab
Linux CentOS6.0+
后,都默认自带anacron
服务及/etc/anacrontab
/etc/anacrontab
,则通过/etc/crontab
配置crond的定时任务。/etc/anacrontab
日
、周
、月
三种定时任务方案RANDOM_DELAY=45
表示随机延迟0~45分钟,日方案
中delay
对应的5
则表示强制延迟5
分钟,到达定时任务的指定日期后,还需要延迟一段时间再执行,总延迟时间(分钟)= 随机延迟RANDOM_DELAY
+ 强制延迟 delay
/etc/anacrontab
后,不需要再重复配置/etc/crontab
配置/etc/crontab
/etc/anacrontab
则需要配置/etc/crontab
vi /etc/crontab
cron表达式
使用哪个用户执行
要执行的命令
logrotate
日志管理要执行的相关指令文件一般放在/etc/logrotate.d
目录下
编辑vi /etc/logrotate.d/nginx
#logrotate nginx分割方案
/usr/nginx/logs/nginx/*.log {
#赋予root权限,否则报权限问题insecure permissions
su root root
#每天滚动
daily
#如果日志丢失,不报错继续滚动下一个日志
missingok
#使用当前日期作为命令格式,如:host.access.log-20210101.log
dateext
#保留最近52次滚动的日志
rotate 52
#使用gzip压缩日志文件为gz,节省空间
compress
#delaycompress和compress一起使用时,转存的日志文件到下一次转存时才压缩
delaycompress
#日志文件为空不进行滚动
notifempty
#转存日志文件,使用指定的文件模式640创建新的日志文件,归属nginx nginx用户
create 640 nginx nginx
#运行postrotate的脚本
sharedscripts
postrotate
if [ -f /usr/nginx/nginx/nginx.pid ]; then
kill -USER1 `cat /usr/nginx/nginx/nginx.pid`
fi
endscript
}
–说明:
注意/usr/nginx/nginx/nginx.pid
,要改成自己nginx的PID进程文件
查看PID配置路径:cat /etc/nginx/nginx.conf
虽然crond
服务每分钟会加载到/etc/anacrontab
中配置的日方案
、周方案
、月方案
对应的定时任务指令
但是/etc/anacrontab
中配置的定时任务最小时间单位是日
,不可能等到第二天看结果后才能验证配置是否正确吧,所以需要想办法马上验证crond
+ logrotate`配置是否正确。
以/etc/anacrontab
中配置的日方案
中配置为例,其中 nice run-parts /etc/cron.daily
表示会加载/etc/cron.daily
目录下的可执行文件并执行
然后会加载到/etc/cron.daily/logrotate
并执行
执行/etc/cron.daily/logrotate
时会加载到文件中指定的/etc/logrotate.conf
主配置文件
主配置文件/etc/logrotate.conf
中又include
包含了外部/etc/logrotate.d
目录下的所有子配置文件
而/etc/logroate.d
目录下,存放的是Linux相关用户的日志管理指令文件,例如nginx
用户的日志清理及备份指令文件等。
总结来说,手动验证crond
+logrotate
配置是否正确的命令为:
/usr/sbin/logrotate -f /etc/logrotate.conf
进入手动验证测试~
debug模式验证
debug模式,不执行任何操作,也不会清理和备份日志文件,仅测试,方便调试
/usr/sbin/logrotate -d -l debug.log /etc/logrotate.conf
查看输出日志问加你debug.log
定位关键字nginx
– 说明
/etc/logrotate/nginx
归属要设置为root
用户chown root:root /etc/logrotate/nginx
,否则报错:
Ignoring nginx because the file owner is wrong (should be root).
强制验证
强制验证 -f
表示强制转存日志文件
/usr/sbin/logrotate -f /etc/logrotate.d/nginx
如上图,强制验证成功,说明配置的crond
+ logrotate
实现nginx
日志清理及备份功能正常~
|