https://urlify.cn/F3Uzmi
https://www.cnblogs.com/clsn/p/8428257.html
[root@nginx01 ~]# logrotatelogrotate 3.8.6 - Copyright (C) 1995-2001 Red Hat, Inc.This may be freely redistributed under the terms of the GNU Public LicenseUsage: logrotate [-dfv?] [-d|--debug] [-f|--force] [-m|--mail command] [-s|--state statefile] [-v|--verbose] [-l|--log STRING][--version] [-?|--help] [--usage] [OPTION...] <configfile>[root@nginx01 ~]# ll /etc/logrotate.d/bootlog nginx syslog yummariadb subscription-manager wpa_supplicant[root@nginx01 ~]# ll /etc/logrotate.d/nginx-rw-r--r-- 1 root root 242 Oct 3 2019 /etc/logrotate.d/nginx[root@nginx01 ~]# cat /etc/logrotate.d/nginx/var/log/nginx/*log {create 0664 nginx rootdailyrotate 10missingoknotifemptycompresssharedscriptspostrotate/bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || trueendscript}[root@nginx01 ~]# cat /etc/cron.daily/logrotate#!/bin/sh/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.confEXITVALUE=$?if [ $EXITVALUE != 0 ]; then/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"fiexit 0[root@nginx01 ~]#
[root@uat-nightingale-server ~]# 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[root@uat-nightingale-server ~]#
[root@uat-nightingale-server ~]# ll /etc/logrotate.conf-rw-r--r--. 1 root root 662 Jul 31 2013 /etc/logrotate.conf[root@uat-nightingale-server ~]# ll /etc/logrotate.dtotal 36-rw-r--r--. 1 root root 76 May 12 2017 bootlog-rw-r--r-- 1 root root 160 Jan 31 2017 chrony-rw-r--r-- 1 root root 893 Apr 3 2020 mariadb-rw-r--r-- 1 root root 242 Oct 3 2019 nginx-rw-r--r--. 1 root root 71 Jun 22 2017 subscription-manager-rw-r--r--. 1 root root 224 May 10 2017 syslog-rw-r--r--. 1 root root 32 Dec 9 2013 up2date-rw-r--r--. 1 root root 100 May 17 2017 wpa_supplicant-rw-r--r--. 1 root root 100 Mar 28 2017 yum[root@uat-nightingale-server ~]#
定时日志轮询
[root@uat-nightingale-server ~]# cat /etc/cron.daily/logrotate#!/bin/sh/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.confEXITVALUE=$?if [ $EXITVALUE != 0 ]; then/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"fiexit 0[root@uat-nightingale-server ~]#
手动执行日志轮询 -f
# /usr/sbin/logrotate -f /etc/logrotate.d/nginx# /usr/sbin/logrotate -d -f /etc/logrotate.d/nginx ##强制执行
logrotate [OPTION...] <configfile>-d, --debug :debug模式,测试配置文件是否有错误。-f, --force :强制转储文件。-m, --mail=command :压缩日志后,发送日志到指定邮箱。-s, --state=statefile :使用指定的状态文件。-v, --verbose :显示转储过程。
日志轮询规则
[root@uat-nightingale-server ~]# cat /etc/logrotate.conf# see "man logrotate" for details# rotate log files weeklyweekly# keep 4 weeks worth of backlogsrotate 4# create new (empty) log files after rotating old onescreate# use date as a suffix of the rotated filedateext# uncomment this if you want your log files compressed#compress# RPM packages drop log rotation information into this directoryinclude /etc/logrotate.d# no packages own wtmp and btmp -- we'll rotate them here/var/log/wtmp {monthlycreate 0664 root utmpminsize 1Mrotate 1}/var/log/btmp {missingokmonthlycreate 0600 root utmprotate 1}# system-specific logs may be also be configured here.[root@uat-nightingale-server ~]#
#
# cat /etc/logrotate.conf# 底下的设定是 "logrotate 的默认值" ,如果別的文件设定了其他的值,# 就会以其它文件的设定为主weekly //默认每一周执行一次rotate轮转工作rotate 4 //保留多少个日志文件(轮转几次).默认保留四个.就是指定日志文件删除之前轮转的次数,0 指没有备份create //自动创建新的日志文件,新的日志文件具有和原来的文件相同的权限;因为日志被改名,因此要创建一个新的来继续存储之前的日志dateext //这个参数很重要!就是切割后的日志文件以当前日期为格式结尾,如xxx.log-20131216这样,如果注释掉,切割出来是按数字递增,即前面说的 xxx.log-1这种格式compress //是否通过gzip压缩转储以后的日志文件,如xxx.log-20131216.gz ;如果不需要压缩,注释掉就行include /etc/logrotate.d# 将 /etc/logrotate.d/ 目录中的所有文件都加载进来/var/log/wtmp { //仅针对 /var/log/wtmp 所设定的参数monthly //每月一次切割,取代默认的一周minsize 1M //文件大小超过 1M 后才会切割create 0664 root utmp //指定新建的日志文件权限以及所属用户和组rotate 1 //只保留一个日志.}# 这个 wtmp 可记录用户登录系统及系统重启的时间# 因为有 minsize 的参数,因此不见得每个月一定会执行一次喔.要看文件大小。
参数
compress #通过gzip 压缩转储以后的日志nocompress #不做gzip压缩处理copytruncate #用于还在打开中的日志文件,把当前日志备份并截断;是先拷贝再清空的方式,拷贝和清空之间有一个时间差,可能会丢失部分日志数据。nocopytruncate #备份日志文件不过不截断create mode owner group #轮转时指定创建新文件的属性,如create 0777 nobody nobodynocreate #不建立新的日志文件delaycompress #和compress 一起使用时,转储的日志文件到下一次转储时才压缩nodelaycompress #覆盖 delaycompress 选项,转储同时压缩。missingok #如果日志丢失,不报错继续滚动下一个日志errors address #专储时的错误信息发送到指定的Email 地址ifempty #即使日志文件为空文件也做轮转,这个是logrotate的缺省选项。notifempty #当日志文件为空时,不进行轮转mail address #把转储的日志文件发送到指定的E-mail 地址nomail #转储时不发送日志文件olddir directory #转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统noolddir #转储后的日志文件和当前日志文件放在同一个目录下sharedscripts #运行postrotate脚本,作用是在所有日志都轮转后统一执行一次脚本。如果没有配置这个,那么每个日志轮转后都会执行一次脚本prerotate #在logrotate转储之前需要执行的指令,例如修改文件的属性等动作;必须独立成行postrotate #在logrotate转储之后需要执行的指令,例如重新启动 (kill -HUP) 某个服务!必须独立成行daily #指定转储周期为每天weekly #指定转储周期为每周monthly #指定转储周期为每月rotate count #指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份dateext #使用当期日期作为命名格式dateformat .%s #配合dateext使用,紧跟在下一行出现,定义文件切割后的文件名,必须配合dateext使用,只支持 %Y %m %d %s 这四个参数size(或minsize) log-size #当日志文件到达指定的大小时才转储,log-size能指定bytes(缺省)及KB (sizek)或MB(sizem).当日志文件 >= log-size 的时候就转储。 以下为合法格式:(其他格式的单位大小写没有试过)size = 5 或 size 5 (>= 5 个字节就转储)size = 100k 或 size 100ksize = 100M 或 size 100M
nginx日志切割配置
####
[root@uat-nightingale-server ~]# cat /etc/logrotate.d/nginx.conf/usr/local/nginx/logs/*.log {dailyrotate 7missingoknotifemptydateextsharedscriptspostrotateif [ -f /usr/local/nginx/logs/nginx.pid ]; thenkill -USR1 `cat /usr/local/nginx/logs/nginx.pid`fiendscript}[root@uat-nightingale-server ~]#
#
[root@uat-nightingale-server logrotate.d]# cat nginx/var/log/nginx/*log {create 0664 nginx rootdailyrotate 10missingoknotifemptycompresssharedscriptspostrotate/bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || trueendscript}
#!/bin/bashlog_path=/usr/local/nginx/logs/access.logsave_path=/usr/local/nginx/logs/bak/access_$(date +%Y%m%d -d 'yesterday').logcp $log_path $save_path && echo > $log_path$ crontab -e#输入0 0 * * * /usr/bin/sh cut_logs.sh #每天的00:00执行日志切分$ crontab -l #查看定时任务是否添加成功
