Logrotate如何实现每小时切割日志文件

2023-12-01 0 907
目录
  • 一、Logrotate背景介绍
    • 1.1 安装
  • 二、logrotate配置介绍
    • 三、实现每小时切割日志文件
      • 3.1、添加 logrotate 配置文件
      • 3.2 执行命令
      • 3.3加入定时任务
    • 总结

      一、Logrotate背景介绍

      logrotate 程序是一个日志文件管理工具。

      用来把旧的日志文件删除,并创建新的日志文件,我们把它叫做“转储”。

      我们可以根据日志文件的大小,也可以根据其天数来转储,这个过程一般通过crontab 定时任务。

      1.1 安装

      一般在服务器初始化的时候这些工具都已经存在的,但是为了保险,还是手动安装一下:

      yum -y install logrotate

      服务简单的说明:

      服务的主配置文件:/etc/logrotate.conf

      Logrotate如何实现每小时切割日志文件

      在主配置中可以看到 include /etc/logrotate.d 说明我们可以将用户定义的配置直接放到这下面,系统会自动为我们执行。

      当然,系统的并不能很好的满足我们需求。

      二、logrotate配置介绍

      再看看配置模板:

      日志文件绝对路径 {
      各种参数…
      }

      参数包含:

      参数说明daily每天轮替一次weekly每周轮替一次monthly每月轮替一次yearly每年轮替一次rotate保留几个轮替日志文件ifempty日志没有内容的时候也进行轮替notifempty若日志为空,则不进行轮替create旧日志文件轮替后创建新的日志文件size日志达到多少后进行rotateminsize文件容量一定要超过多少后才进行rotatenocompress轮替但不进行压缩compress压缩轮替文件dateext轮替旧日志文件时,文件名添加-%Y %m %d形式日期,可用dateformat选项扩展配置。dateformat .%s对日期进行格式定制nodateext旧日志文件不使用dateext扩展名,后面序数自增如"*.log.1"sharedscripts作用域下文件存在至少有一个满足轮替条件的时候,执行一次prerotate脚本和postrotate脚本。prerotate/endscript在轮替之前执行之间的命令,prerotate与endscript成对出现。postrotate/endscript在轮替之后执行之间的命令,postrotate与endscript成对出现。olddir将轮替的文件移至指定目录下missingok如果日志文件不存在,继续进行下一个操作,不报错

      三、实现每小时切割日志文件

      3.1、添加 logrotate 配置文件

      vim /etc/logrotate.d/nginx

      内容如下:

      /data2/data/cp*log/cp.log
      {
      copytruncate
      rotate 87600
      missingok
      ifempty
      dateext
      dateformat -%Y%m%d-%H
      sharedscripts
      postrotate
      if [ -f /usr/local/openresty/nginx/logs/nginx.pid ]; then
      kill -USR1 `cat /usr/local/openresty/nginx/logs/nginx.pid`
      fi
      endscript
      }

      3.2 执行命令

       //手动执行一次轮替:
       /usr/sbin/logrotate -vf /etc/logrotate.d/nginx

      执行命令

      logrotate [-dv] [-f|–force] [-s|–state statefile] config_file ..

      执行命令选项

      # logrotate –help
      Usage: logrotate [OPTION…] <configfile>
      -d, –debug Don\’t do anything, just test (implies -v) 不做实际处理,仅调试
      -f, –force Force file rotation 强制执行,忽视参数要求
      -m, –mail=command Command to send mail (instead of `/bin/mail\’) 发送mail
      -s, –state=statefile Path of state file 查看状态文件
      -v, –verbose Display messages during rotation 轮替一次,并显示轮替过程信息
      –version Display version information 显示logrotate版本
      Help options:
      -?, –help Show this help message
      –usage Display brief usage message

      3.3加入定时任务

      crontab -e

      每小时的59分进行切割 内容如下:

      # Logrotate
      59 * * * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginx

      这里只简单地介绍该种定时任务配置。

      #格式
      *(分钟) *(小时) *(天) *(月) *(周几) 用户 命令
      # 若分钟位值为 *,表示0-59之间的任意有效值;
      # 若分钟位值为 1,表示每小时的第1分钟;
      # 若分钟位值为 */5,表示每5分钟
      # 若分钟位值为10,20 表示每小时的第10分钟和第20分钟
      # 若分钟位值为10-12 表示每小时的第10、11、12分钟

      效果如下:

      Logrotate如何实现每小时切割日志文件

      总结

      以上为个人经验,希望能给大家一个参考,也希望大家多多支持悠久资源。

      收藏 (0) 打赏

      感谢您的支持,我会继续努力的!

      打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
      点赞 (0)

      悠久资源 Nginx服务器 Logrotate如何实现每小时切割日志文件 https://www.u-9.cn/server/nginx/7966.html

      常见问题

      相关文章

      发表评论
      暂无评论
      官方客服团队

      为您解决烦忧 - 24小时在线 专业服务