日志切割工具

简介

系统日志需要按照时间分割和压缩

logrotate

安装

1
yum install logrotate

配置

添加日志分割配置文件

  • 创建或打开文件
    1
    vim /etc/logrotate.d/nginx
  • 填写内容
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    /data/logs/nginx/*.log {   
    daily
    rotate 7
    missingok
    notifempty
    dateext
    sharedscripts
    postrotate
    if [ -f /usr/local/nginx/logs/nginx.pid ]; then
    kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
    fi
    endscript
    }

    测试命令

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

    添加定时任务

  • 打开定时器配置文件
    1
    vim /etc/crontab
  • 添加定时任务
    1
    59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx
Author: suce
Link: https://haoubox.cn/2022/02/15/日志切割工具/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.