发布时间:
Oct 16, 2015
更新时间:
Oct 16, 2015
总字数:1915
阅读时间:4m
作者: 谢先斌
在Centos6.5中,基本上所有服务都会把日志打印到日志文件,而随着时间的推移,日志文件会越来越大,直到占用所有的硬盘空间。而logrotate就是一个提供定时归档日志的功能。
logrotate简介
logrotate是一个日志管理工具,它可以自动对日志进行轮循、压缩,删除,邮件发送等。
使用
logrotate的详细帮助命令可以通过man查看,如下:
[root@xiexianbin_cn ~]# man logrotate
例子
logrotate nginx日志如下
[root@xiexianbin_cn ~]# cat /etc/logrotate.d/nginx
/var/log/nginx/*log {
daily
rotate 10
missingok
notifempty
compress
dateext %Y-%m-%s
sharedscripts
postrotate
/bin/kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null || :
endscript
}
[root@xiexianbin_cn ~]#
参数说明:
/var/log/nginx/*log:需要轮询的日志路径
daily:每天轮询
rotate 10:一次将存储10个归档日志。对于第十个归档,时间最久的归档将被删除
dateext:使用日期作为命名格式
sharedscripts: 表示postrotate脚本在压缩了日志之后只执行一次
postrotate/endscript: 最通常的作用是让应用重启,以便切换到新的日志文件, 在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种情况下,rsyslogd 进程将立即再次读取其配置并继续运行。
其他参数说明:
notifempty:当日志为空时不进行滚动
compress 通过gzip 压缩转储以后的日志
nocompress 不需要压缩时,用这个参数
copytruncate 用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate 备份日志文件但是不截断*
create mode owner group 以指定的权限创建全新的日志文件,同时logrotate也会重命名原始日志文件。
nocreate 不建立新的日志文件
delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。
errors address 专储时的错误信息发送到指定的Email 地址
ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty 如果是空文件的话,不转储
mail address 把转储的日志文件发送到指定的E-mail 地址
nomail 转储时不发送日志文件
olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir 转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
daily 指定转储周期为每天
weekly 指定转储周期为每周
monthly 指定转储周期为每月
rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
tabootext [+] list 让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~
size size 当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及K (sizek)或者M (sizem).
命令生效
默认情况下,logrotate的安装创建一个名为/etc/cron.daily里面的logrotate crontab文件。 因为它与这个目录里面的其他crontab文件的情况下,将每天定时执行。
有时候一些日志文件比较大时,使用如下命令手动归档:
logrotate -v -d -f /etc/logrotate.d/nginx
或
logrotate -v -f /etc/logrotate.d/nginx
当然对于日志量较大的程序,也可以结合定时任务使用,每小时或每隔几小时执行一次归档任务。
help 命令
[root@xiexianbin_cn mongodb]# logrotate --help
Usage: logrotate [OPTION...]
-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')
-s, --state=statefile Path of state file
-v, --verbose Display messages during rotation
--version Display version information
Help options:
-?, --help Show this help message
--usage Display brief usage message
[root@xiexianbin_cn mongodb]# logrotate --usage
Usage: logrotate [-dfv?] [-d|--debug] [-f|--force] [-m|--mail command] [-s|--state statefile] [-v|--verbose] [--version] [-?|--help] [--usage] [OPTION...]
[root@xiexianbin_cn mongodb]#
logrotate 生效时间
在 cat /etc/anacrontab 配置:
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs # 随机的延迟时间,表示最大是 45分钟
RANDOM_DELAY=45
# the jobs will be started during the following hours only # 开始时间:凌晨3点22分
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command # 间隔 延迟时间(cron.daily会在3:22+(5,45)) 脚本位置
1 5 cron.daily nice run-parts /etc/cron.daily # logrotate 在这里
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly[root@xiexianbin_cn ~]# 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 0Cron
Anacron
Minimum granularity is minute (i.e Jobs can be scheduled to be executed every minute)
Minimum granularity is only in days
Cron job can be scheduled by any normal user ( if not restricted by super user )
Anacron can be used only by super user ( but there are workarounds to make it usable by normal user )
Cron expects system to be running 24 x 7. If a job is scheduled, and system is down during that time, job is not executed.
Anacron doesn’t expect system to be running 24 x 7. If a job is scheduled, and system is down during that time, it start the jobs when the system comes back up.
Ideal for servers
Ideal for desktops and laptops
Use cron when a job has to be executed at a particular hour and minute
Use anacron when a job has to be executed irrespective of hour and minute
参考:https://www.thegeekstuff.com/2011/05/anacron-examples/
demo
mongodb
/var/log/mongodb/*.log {
daily
rotate 28
copytruncate
# delaycompress
compress
notifempty
missingok
sharedscripts
postrotate
/bin/kill -SIGUSR1 `cat /var/run/mongodb/mongod-configsvr.pid 2>/dev/null` 2> /dev/null
/bin/kill -SIGUSR1 `cat /var/run/mongodb/mongod-rs-1.pid 2>/dev/null` 2> /dev/null
/bin/kill -SIGUSR1 `cat /var/run/mongodb/mongod-rs-2.pid 2>/dev/null` 2> /dev/null
/bin/kill -SIGUSR1 `cat /var/run/mongodb/mongod-rs-3.pid 2>/dev/null` 2> /dev/null
sleep 3
rm -f $(ls /var/log/mongodb/*.log.$(date +%Y-%m-)* 2>/dev/null | sort) || true
endscript
}
java
/usr/local/tomcat/logs/catalina.out {
rotate 30
daily
copytruncate
dateext
compress
notifempty
missingok
}
说明:
copytruncate 的工作是先将日志文件 copy,然后再将原文件清空,可能会丢失部分日志,但 java 文件输出句柄不变。
java -jar 采用 > 重定向日志时,logrotate 采用 copytruncate 对其进行归档,新的日志文件会产生文件空洞。(shell 中的 > 相当于 java 中的 w)
java -jar 采用 >> 重定向日志时,不会产生文件空洞(推荐)。(shell 中的 » 相当于 java 中的 a)
haproxy
/var/log/haproxy {
missingok
notifempty
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
/bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true
/bin/kill -HUP `cat /var/run/syslog-ng.pid 2> /dev/null` 2> /dev/null || true
endscript
}