您的位置:  首页 > 技术 > java语言 > 正文

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

2021-08-07 10:40 管理员 次阅读 条评论

背景介绍

  • logrotate是Linux默认自带日志文件分割工具,结合Linux默认自带的crond定时服务,可实现nginxtomcat等应用日志的定时自动分割及清理,大大节省服务器磁盘空间,也方便运维人员按日期排查应用相关日志。
  • 本文主要介绍基于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重要文件及目录说明
文件或目录说明
/etc/logrotate.conflogrotate主配置文件,默认文件分割配置方案,文件中还配置了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定时任务配置

2.1、查看crond状态

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

2.2、配置crond定时任务

  • 配置/etc/anacrontab

    • 现在比较新版的Linux操作系统,比如Linux CentOS6.0+后,都默认自带anacron服务及/etc/anacrontab
    • 如果操作系统比较老或者不存在/etc/anacrontab,则通过/etc/crontab配置crond的定时任务。
    • 配置/etc/anacrontab
      系统一般默认已配置按三种定时任务方案
      在这里插入图片描述**–说明:**1、其中RANDOM_DELAY=45表示随机延迟0~45分钟,日方案delay对应的5则表示强制延迟5分钟,到达定时任务的指定日期后,还需要延迟一段时间再执行,总延迟时间(分钟)= 随机延迟RANDOM_DELAY + 强制延迟 delay
      2、配置/etc/anacrontab后,不需要再重复配置/etc/crontab
  • 配置/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 表示会加载/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

进入手动验证测试~

4.2、验证测试

  • 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日志清理及备份功能正常~

  • 0
    感动
  • 0
    路过
  • 0
    高兴
  • 0
    难过
  • 0
    搞笑
  • 0
    无聊
  • 0
    愤怒
  • 0
    同情
热度排行
友情链接