nginx--基于crond定时服务 + logrotate实现nginx日志自动清理及备份

网友投稿 1065 2022-05-29

@TOC

背景介绍

logrotate是Linux默认自带日志文件分割工具,结合Linux默认自带的crond定时服务,可实现nginx、tomcat等应用日志的定时自动分割及清理,大大节省服务器磁盘空间,也方便运维人员按日期排查应用相关日志。

本文主要介绍基于crond + logrotate实现nginx应用日志自动清理及备份

需要使用root用户操作,通过Linux自带logrotate工具 + crond服务实现nginx日志每天定时自动分割及清理。

关于crond VS anacron 和crontab VS anacrontab区别及联系,参考我的这篇博文

Linux-- 自带定时服务crond VS anacron和 crontab VS anacrontab区别对比

进入正文~

一、logrotate说明

查看logrotate版本号logrotate --version

logrotate重要文件及目录说明

/usr/sbin/logrotate 选项

–选项

-d, --debug debug模式,不执行任何操作,仅测试,方便调试

-f, --force 强制转存文件

-m, --mail=command 发送日志到指定邮箱

-s, --state=statefile 状态记录文件

-v, --verbose 显示转存过程信息

-l, --log=STRING 日志文件

二、crond定时任务配置

2.1、查看crond状态

查看crond服务运行状态

systemctl status crond.service

running表示运行状态~

查看crond服务后台进程

ps -ef|grep crond

可以看到crond的守护进程是通过/usr/sbin/crond -n来实现的。

crond的后台进程,会每分钟去加载是否有要执行的定时任务。

nginx--基于crond定时服务 + logrotate实现nginx日志自动清理及备份

crond服务其他常用命令

systemctl status crond.service

systemctl start crond.service

systemctl restart crond.service

systemctl stop crond.service

systemctl reload crond.service

2.2、配置crond定时任务

(1)配置/etc/anacrontab

现在比较新版的Linux操作系统,比如Linux CentOS6.0+后,都默认自带anacron服务及/etc/anacrontab,如果操作系统比较老或者不存在/etc/anacrontab,则通过/etc/crontab配置crond的定时任务,系统一般默认已配置按日、周、月三种定时任务方案

**–说明:**1、其中RANDOM_DELAY=45表示随机延迟0~45分钟,日方案中delay对应的5则表示强制延迟5分钟,到达定时任务的指定日期后,还需要延迟一段时间再执行,总延迟时间(分钟)= 随机延迟RANDOM_DELAY + 强制延迟 delay

(2)配置/etc/crontab

配置/etc/anacrontab后,不需要再重复配置/etc/crontab

若操作系统不存在/etc/anacrontab则需要配置/etc/crontab

编辑配置vi /etc/crontab

–说明:

所标记的配置内容格式依次为: cron表达式 使用哪个用户执行 要执行的命令

三、配置logrotate指令脚本

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 + logrotate

4.1、验证说明

虽然crond服务每分钟会加载到/etc/anacrontab中配置的日方案、周方案、月方案对应的定时任务指令,但是/etc/anacrontab中配置的定时任务最小时间单位是日,不可能等到第二天看结果后才能验证配置是否正确吧,所以需要想办法马上验证crond + logrotate`配置是否正确。

这里以/etc/anacrontab中配置的日方案 中配置为例,其中 nice run-parts /etc/cron.daily 加载顺序为:

(1)加载/etc/cron.daily目录下的可执行文件并执行

(2) 加载到/etc/cron.daily/logrotate并执行

(3)执行/etc/cron.daily/logrotate时会加载到文件中指定的/etc/logrotate.conf主配置文件

(4)主配置文件/etc/logrotate.conf中又include包含了外部/etc/logrotate.d目录下的所有子配置文件

(5)而/etc/logroate.d目录下,存放的是Linux相关用户的日志管理指令文件,例如nginx用户的日志清理及备份指令文件等。

(6)总结来说,手动验证crond +logrotate配置是否正确的命令为:

/usr/sbin/logrotate -f /etc/logrotate.conf

进入手动验证测试~

4.2、验证测试

(1)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).

(2)强制验证

强制验证 -f表示强制转存日志文件

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

如上图,强制验证成功,说明配置的crond + logrotate实现nginx日志清理及备份功能正常~

Nginx

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:C++初学者必看的50个建议 绝对经典
下一篇:Python学习随笔
相关文章