赞
踩
logrotate程序是一个日志文件管理工具,用于分割日志文件,删除旧的日志文件,并创建新的日志文件,起到“转储”作用,可以节省磁盘空间;是linux里内置的日志分割工具,若没有可默认安装logrotate包
”Debian或Ubuntu安装“
# apt-get install logrotate cron
"Fedora,CentOS或RHEL安装"
# yum install logrotate crontabs
/etc/logrotate.conf (主要的配置文件)
/etc/logrotate.d/ (可以将自己的需要滚动的日志配置放在这个目录下。该目录里的所有文件都会被主动的读入/etc/logrotate.conf中执行)
[root@localhost log]# cat /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly //默认每周执行一次rotate轮转工作
# keep 4 weeks worth of backlogs
rotate 4 //保留多少个日志文件(轮转几次).默认保留四个.就是指定日志文件删除之前轮转的次数;0 指没有备份
# create new (empty) log files after rotating old ones
create //自动创建新的日志文件,新的日志文件具有和原来的文件相同的权限
# use date as a suffix of the rotated file
dateext //切割后的日志文件以当前日期为格式结尾如xxx.log-20131216;如果注释掉,切割出来是按数字递增,即前面说的 xxx.log-1这种格式
# uncomment this if you want your log files compressed
#compress //是否通过gzip压缩转储以后的日志文件,如xxx.log-20131216.gz ;如果不需要压缩,注释掉就行
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d //将 /etc/logrotate.d/ 目录中的所有文件都加载进来
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp { //log指定位置;仅针对此目录下所设定的参数
monthly //每月一次切割
create 0664 root utmp //指定新建的日志文件权限以及所属用户和组
minsize 1M //文件大小超过 1M 后才会切割
rotate 1 //只保留一个日志
}
//这个 wtmp 可记录用户登录系统及系统重启的时间;登录成功的日志
# 因为有 minsize 的参数,因此不见得每个月一定会执行一次喔.要看文件大小
/var/log/btmp {
missingok //在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误
monthly
create 0600 root utmp
rotate 1
}
//btmp就是记录所有尝试登录,但是登录失败的日志
# system-specific logs may be also be configured here.
/etc/logrotate.d其实就是由/etc/logrotate.conf 所规划出来的目录,虽然可以将所有的配置都写入/etc/logrotate.conf ,但是这样一来这个文件就实在是太复杂了,尤其是当使用很多的服务在系统上面时, 每个服务都要去修改/etc/logrotate.conf的设定也似乎不太合理了。
所以,如果独立出来一个目录,那么每个要切割日志的服务, 就可以独自成为一个文件,并且放置到 /etc/logrotate.d/ 当中
compress | 通过gzip 压缩转储以后的日志 |
nocompress | 不做gzip压缩处理 |
copytruncate | 用于还在打开中的日志文件,把当前日志备份并截断;是先拷贝再清空的方式,拷贝和清空之间有一个时间差,可能会丢失部分日志数据。 |
nocopytruncate | 备份日志文件不过不截断 |
create mode owner group | 轮转时指定创建新文件的属性,如create 0777 nobody nobody |
nocreate | 不建立新的日志文件 |
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 100k size = 100M 或 size 100M |
logrotate基于cron来运行,其脚本是/etc/cron.daily/logrotate,日志轮转是系统自动完成的。
实际运行时,Logrotate会调用配置文件/etc/logrotate.conf;可以在/etc/logrotate.d目录里放置自定义好的配置文件,用来覆盖Logrotate的缺省值
[root@localhost pstuser]# cat /etc/cron.daily/logrotate
#!/bin/sh/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
若不等自动切割,想手动强制切割日志,着急验证当前配置的结果,可用以下命令执行
/usr/sbin/logrotate -dvf /etc/logrotate.d/nginx
参数详解:
-v --verbose:详细的输出数据
-f --force:强制转储logrotate轮循日志文件
-d --debug:debug模式,测试配置文件是否有错误
-m --mail=command:压缩日志后,发送日志到指定邮箱
-s --state=statefile:使用指定的状态文件
比如以系统日志/var/log/message做切割来简单说明下:
第一次执行完rotate(轮转)之后,原本的messages会变成messages.1,而且会制造一个空的messages给系统来储存日志;
第二次执行之后,messages.1会变成messages.2,而messages会变成messages.1,又造成一个空的messages来储存日志!
如果仅设定保留三个日志(即轮转3次)的话,那么执行第三次时,则 messages.3这个档案就会被删除,并由后面的较新的保存日志所取代!也就是会保存最新的几个日志。
日志究竟轮换几次,这个是根据配置文件中的rotate参数来判定的
问题:
A、啥都没发生,日志还是原来那份日志
B、转储后的日志不见了
办法:
先查看自动执行的脚本有没有问题:cat /etc/cron.daily/logrotate (正常)
再查看日志切割的配置文件是否正常:cat /etc/logrotate.d/nginx (正常)
最后查看预定的cron(若无预定则省略此步)有没有执行,查看cron的日志:cat /var/log/cron(正常)
再往下看!
对于第一种情况: 因为默认情况下,logrotate只会转储大于1M的日志,因此日志并没有发生变化(日志太小不会动,以kb,b单位); 若想解决可强制执行:logrotate -f /etc/logrotate.d/test
对于第二种情况: 转储后的日志不见了,这是因为手动执行某个logrotate配置文件时,不会加载默认的配置文件/etc/logrotate.conf, 因此此时转储的次数rotate为0,也就是不会保留转储后的日志,表现出来就是转储后的my.log.1被删除,也就出现日志丢失的情况 若想解决可执行:配置rotate参数
要想知道logrotate什么时候执行日志分割操作,需要关注/etc/anacrontab及/var/lib/logrotate/logrotate.status这两个文件
而/etc/anacrontab是使logrotate按时执行脚本的主要配置文件
[root@localhost pstuser]# cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours onlySTART_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
从上面内容可以看出:
若机器没有关机,默认的logrotate(配置文件里设置的是cron.daily) 一般会在每天的3点05分到3点50分之间执行,真实的延迟时间是RANDOM_DELAY+delay in minutes
如果在3-22这个时间段内服务器处于关机状态,则logrotate会在机器开机5分钟后执行分割日志的操作
注意!!!
不建议直接修改此文件,如果想修改logrotate默认的执行时间,可通过crontab执行自定义的logrotate配置文件,但这时间不能将自定义的配置文件放置在/etc/logrotate.d下,应该放置在/etc/logrotate.d下层目录或另外的路径,否则就会执行两遍日志分割的操作,执行时间分别是你在crontab中定义的时间以及anacrontab默认的执行时间即3:05-3:50
正常的日志分割都是按天进行的,即每个日志只记录0-24点之间的内容,如果是按默认的crontab设定的执行时间那肯定是不行的,具体实现其实有两种方法
方法1(推荐):
1:在/etc/logrotate.d创建下层目录,mkdir -p/etc/logrotate.d/nginx 当然也可在非/etc/logrotate.d/下创建此目录,例如我是在 /etc/logrotate.daily.0下创建了nginxLogrotate文件
2: 移除之前自定义的配置文件,sudo mv /etc/logrotate.d/nginxLogrotate ~/bak/,这样做的作用就是防止执行两次日志分割脚本的操作.
3: 添加crontab计划任务, sudo echo "59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.daily.0/nginxLogrotate >/dev/null 2>&1" > /etc/crontab 当然也可直接在root用户下执行crontab -e然后添加上面的内容. 如果用非root用户则会报错error: error creating output file /var/lib/logrotate/logrotate.status.tmp: 权限不够
4: 重启crontab,service crond restart,其实可以不用重启.
注意!!!
如果crontab中定义的执行时间是0点,则需要在/etc/logrotate.d/nginx/nginxLogrotate里添加上
dateyesterday这个选项,否则日志内容记录的是昨天的,而 日志名称格式却是今天的,但crontab中定义的执行时间是23:59这样的则不用添加dateyesterday选项.
方法2(没验证):
需谨慎,会替换系统默认执行logrotate的时间。
设置00:00执行:
1: /etc/anacrontab 配置文件,将 RANDOM_DELAY(随机延迟)=0 , START_HOURS_RANGE(启动时间)=0-22 , delay in minutes(以分钟为单位的延迟) 改为0。
2: etc/cron.d/0hourly 配置文件,将01改为00(/etc/cron.daily那一行)。
3: /etc/logrotate.d/nginx/nginxLogrotate里添加上dateyesterday 这样logrotate就可以在每天00点00分转储日志(配置文件里设置的是daily)
设置23:59执行:
1: 调整/etc/anacrontab配置文件,将RANDOM_DELAY=0, START_HOURS_RANGE=23-24 , delay in minutes 改为0。
2: 调整 /etc/cron.d/0hourly 配置文件,将01改为59(/etc/cron.daily那一行),即每小时的59分执行,但因为logrotate每天只执行一次所以具体执行时间是23:59
crond服务加载 /etc/cron.d/0hourly ---> 在每小时的01分执行/etc/cront.hourly/0anacron --->执行anacron --->根据/etc/anacrontab的配置执行/etc/cron.daily,/etc/cron.weekly,/etc/cron.monthly --->执行/etc/cron.daily/下的logrotate脚本 --->执行logrotate --->根据/etc/logrotate.conf配置执行脚本/etc/logrotate.d/nginx --->分割nginx日志成功
以下三个目录的作用:
/var/spool/cron/USER_NAME
#这个文件才是跟crontab -e/-l 关联的,这个文件保存了crontab -e编辑的任务内容
#比如执行 crontab -u root -e,编辑保存后,就会有/var/spool/cron/root 这个文件
/var/spool/anacron/{cron.daily,cron.monthly,cron.weekly}
#这三个文件记录了anacron上一次执行的时间(上一天,上一周或上一月)
#anacron任务执行时,对照这里的时间,决定是否执行anacron任务
/var/lib/logrotate.status
#这个文件记录logrotate执行情况,logrotate参考这个文件来决定是否需要rotate日志
[root@test ~]# cat /etc/cron.d/0hourly #这个文件指定每小时的01分执行/etc/cron.hourly内的所有脚本
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
01 * * * * root run-parts /etc/cron.hourly #这里的root指定执行任务的用户,run-parts其实是一个可执行脚本,在/usr/bin/run-parts,用来 执行cron.hourly目录内的所有脚本
说明:用crontab -e命令每次编辑完某个用户的cron设置后,cron自动在/var/spool/cron下生成一个与此用户同名的文件,此用户的cron信息都记录在这个文件中。cron启动后每过一份钟读一次这个文件,检查是否要执行里面的命令。因此此文件修改后不需要重新启动cron服务。cron服务每分钟不仅要读一次/var/spool/cron内的所有文件,还需要读一次/etc/crontab,因此我们配置这个文件也能运用cron服务做一些事情。用crontab命令配置是针对某个用户的,而编辑/etc/crontab是针对系统的任务。此文件的文件格式是:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin #可执行文件查找路径
MAILTO=root #如果出现错误,或者有数据输出,数据作为邮件发给这个帐号
HOME=/ #使用者运行的路径,这里是根目录
[root@test ~]# cat /etc/cron.hourly/0anacron #cron.hourly目录下的脚本,根据条件执行anacron命令
#!/bin/bash
# Skip excecution unless the date has changed from the previous run
if test -r /var/spool/anacron/cron.daily; then
day=`cat /var/spool/anacron/cron.daily`
fi
if [ `date +%Y%m%d` = "$day" ]; then
exit 0;
fi
# Skip excecution unless AC powered
if test -x /usr/bin/on_ac_power; then
/usr/bin/on_ac_power &> /dev/null
if test $? -eq 1; then
exit 0
fi
fi
/usr/sbin/anacron -s
[root@test ~]# cat /etc/anacrontab #如果执行anacron命令,那么接着查看anacron的配置文件
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45 #最大延迟时间
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22 #只有在3-22点之间执行任务
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
以上anacrontab配置文件最重要的是最后一部分,以这行为例:
1 5 cron.daily nice run-parts /etc/cron.daily
表示每天都执行/etc/cront.daily/目录下的脚本文件,真实的延迟是RANDOM_DELAY+delay。这里的延迟是5分钟,加上上面的RANDOM_DELAY,所以实际的延迟时间是5-50之间,开始时间为03-22点,如果机器没关,那么一般就是在03:05-03:50之间执行。 nice命令将该进程设置为nice=10,默认为0,即低优先级进程。如果RANDOM_DELAY=0,那么表示准确延迟5min,即03:05执行cron.daily内的脚本
[root@test ~]# cat /etc/cron.daily/logrotate #最后在cron.daily内有logrotate的调用脚本 #!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf #logrotate将会读取配置文件,最终会读取到/etc/logrotate.d/nginx
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
当logrotate命令加载了/etc/logrotate.d/nginx配置文件时,还要比较nginx日志的归档日期
[root@test ~]# cat /var/lib/logrotate.status | grep /root
"/root/install-2017-5-14.log" 2017-5-21 #如果今天是2017-5-21,这个文件里也是2017-5-21,说明今天已经归档过了,否则就会归档(分割)nginx日志
本文章借鉴并总结所得:
crontab和anacron和logrotate的关系详解_小杰666的博客-CSDN博客
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。