Nginx--logrotate日志切割打包,-f,--force


1.系统是默认安装的,查看系统是否安装logrotate

centos

rpm -ql logrotate

/etc/cron.daily/logrotate
/etc/logrotate.conf
/etc/logrotate.d
/etc/rwtab.d/logrotate
/usr/sbin/logrotate
/usr/share/doc/logrotate-3.8.6
/usr/share/doc/logrotate-3.8.6/CHANGES
/usr/share/doc/logrotate-3.8.6/COPYING
/usr/share/man/man5/logrotate.conf.5.gz
/usr/share/man/man8/logrotate.8.gz
/var/lib/logrotate
/var/lib/logrotate/logrotate.status

ubuntu

logrotate --version

logrotate 3.14.0
Default mail command:       /usr/bin/mail
Default compress command:   /bin/gzip
Default uncompress command: /bin/gunzip
Default compress extension: .gz
Default state file path:    /var/lib/logrotate/status
ACL support:                yes
SELinux support:            yes

2.docker配置日志截取

创建文件
/etc/docker/daemon.json
内容
{
"log-driver": "json-file",
"log-opts": {
"max-size": "500m",
"max-file": "3"
}
}

max-size 指定日志文件大小上限
max-file 指定日志文件个数

systemctl daemon-reload
systemctl restart docker.service

3.创建logrotate 配置文件,系统会自动创建 crontab 任务自动执行

/etc/logrotate.d/nginx

/work/nginx/logs/*log {
    su root root
    create 0644 root root   
    daily            #每天执行一次
    rotate 365       #保留365个日志文件
    missingok	     #如果日志丢失,不报错继续滚动下一个日志
    notifempty       #当日志文件为空时不生成新的文件
    compress         #压缩
    sharedscripts    #统一执行一次脚本postrotate
    postrotate       #运行脚本
         docker exec nginx  nginx -s reload;
    endscript
}

手动测试logrotate

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

 -d, --debug :debug模式,测试配置文件是否有错误。
 -f, --force :强制转储文件。

4.推荐博客:

https://www.cnblogs.com/kevingrace/p/6307298.html

相关内容