Linux中logrotate的使用
来源:互联网 发布:.net core 2.0 知乎 编辑:程序博客网 时间:2024/05/29 04:34
logrotate是linux下日志截断(或轮替)、压缩以及删除工具,如让/var/log/foo日志文件每30天轮替,并删除超过6个月的日志。配置完后,logrotate的运作完全自动化,不必进行任何进一步的人为干预。
主流Linux发行版上都默认安装有logrotate包,如果没有,可以使用apt-get或yum等包管理工具安装。
安装
Debian或Ubuntu
# apt-get install logrotate cron
Fedora、CentOS或RHEL
# yum install logrotate crontabs
配置及使用
logrotate安装完成配置文件是/etc/logrotate.conf,默认会导入所有/etc/logrotate.d/*,所以,一般一类日志会在单独的文件中配置。
下面是一个nginx日志轮替配置:
/var/log/nginx/*log { create 0644 nginx nginx daily rotate 10 missingok notifempty compress delaycompress sharedscripts postrotate /etc/init.d/nginx reopen_logs endscript}
解释下上面的配置:
/var/log/nginx/*log:glob通配符,告诉logrotate要轮替那些日志文件
create 644 nginx nginx: 以指定的权限创建新的日志文件,同时logrotate也会重命名原始日志文件。
daily: 日志文件将每天轮替。其它可用值为‘daily’,‘weekly’或者‘yearly’。
rotate 10: 除当天的外,保留五天的日志。时间较久的日志将被删除。
missingok: 忽略logrotate运行时,例如“文件无法找到”之类的错误。
notifempty: 如果日志文件为空,不会进行轮替。
compress: 在轮替任务完成后,对日志使用gzip进行压缩
delaycompress: 与compress选项一起用,delaycompress告诉logrotate不要将最近一次的日志归档压缩,压缩将在下一次轮替时进行。
sharedscripts:所有的日志文件都轮替完毕后统一执行一次脚本,而不是每次都执行。
postrotate/endscript: 在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。上面配置中,告诉nginx重新读取日志。
其他选项
logrotate在轮替日志时,默认会给旧文件加上 .1、 .2、 .3 这样的后缀,.1 是昨天的,.2 是前天的。但如果想要以日期命名,可以在配置文件中加上 dateext,如下:
/var/log/redis/*.log {
weekly
missingok
dateext
copytruncate
rotate 12
compress
notifempty
}
有些软件会在运行时一直打开日志文件,对于这种情况,可以加上copytruncate选项
虽然大多数时候都是按日期切分日志,但同样也会遇到诸如单日日志文件特别小,没几行等情况,这时候可能需要按大小切分,size 可以帮助实现,如下:
/var/www/log/production.log { size=1024M missingok copytruncate #清空原有文件,而不是创建一个新文件 rotate 12 compress notifempty}
如上配置就是每1GB切分一次日志。
运行logrotate
logrotate [OPTION...] <configfile> -d, --debug Don't do anything, just test (implies -v) -f, --force Force file rotation -m, --mail=command Command to send mail (instead of `/usr/bin/mail') -s, --state=statefile Path of state file -v, --verbose Display messages during rotation --version Display version information
想要运行logrotate,直接指定配置文件即可,如轮替nginx日志,则可以运行
# logrotate /etc/logrotate.d/nginx
当配置文件指定为/etc/logrotate.conf,则轮替所有日志:
# logrotate /etc/logrotate.conf
而大多数情况,都会吧logrotate配置成cron任务,定时运行,而安装的时候也会默认安装cron任务,而将logrotate自身的日志存储在/var/lib/logrotate/status中:
# cat /etc/cron.daily/logrotate#!/bin/sh# Clean non existent log file entries from status filecd /var/lib/logrotatetest -e status || touch statushead -1 status > status.cleansed 's/"//g' status | while read logfile datedo [ -e "$logfile" ] && echo "\"$logfile\" $date"done >> status.cleanmv status.clean statustest -x /usr/sbin/logrotate || exit 0/usr/sbin/logrotate /etc/logrotate.conf
- Linux中logrotate的使用
- linux logrotate工具使用
- Linux下的logrotate
- logrotate工具的使用
- logrotate工具的使用
- 使用logrotate来回滚linux上的日志
- linux logrotate
- linux logrotate
- Linux系统中Logrotate工具用法汇总
- 使用logrotate 管理日志文件 (Linux相关)
- Linux下使用logrotate实现日志切换
- linux 使用logrotate做日志轮替
- linux:使用logrotate管理nginx日志文件
- logrotate使用
- logrotate使用
- How to use logrotate to manage log files in linux(如何在Linux中使用logrotate去管理日志文件)
- linux 下rsyslog 和 logrotate 的配置
- apache中log的logrotate设置
- C++11 强类型enum
- 2016.12.14重学C语言记录篇--2
- Unity性能优化之CPU篇
- 数据持久化__归档
- docker初使用小结
- Linux中logrotate的使用
- wepack英文文档阅读笔记--简介与模块系统
- socket TCP编程中connect的一些坑 .
- JDK环境变量以及tomcat配置,maven配置
- 虚拟器安装过程
- PHP ajax访问服务器 $_SESSION数据丢失解决方案
- Object.defineproperty实现数据和视图的联动
- maven常见问题
- 冒泡排序