日志文件包含了关于系统中发生的事件的有用信息,在排障过程中或者系统性能分析时经常被用到。对于忙碌的服务器,日志文件大小会增长极快,服务器会很快消耗磁盘空间,这成了个问题。除此之外,处理一个单个的庞大日志文件也常常是件十分棘手的事。

logrotate是个十分有用的工具,它可以自动对日志进行截断(或轮循)、压缩以及删除旧的日志文件。例如,你可以设置logrotate,让/var/log/foo日志文件每30天轮循,并删除超过6个月的日志。配置完后,logrotate的运作完全自动化,不必进行任何进一步的人为干预。另外,旧日志也可以通过电子邮件发送,不过该选项超出了本教程的讨论范围。

主流Linux发行版上都默认安装有logrotate包,如果出于某种原因,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/目录下。

样例一
在第一个样例中,我们将创建一个10MB的日志文件/var/log/log-file。我们将展示怎样使用logrotate来管理该日志文件。

我们从创建一个日志文件开始吧,然后在其中填入一个10MB的随机比特流数据。

touch /var/log/log-file

head -c 10M < /dev/urandom > /var/log/log-file

由于现在日志文件已经准备好,我们将配置logrotate来轮循该日志文件。让我们为该文件创建一个配置文件。

vim /etc/logrotate.d/log-file

/var/log/log-file {
monthly
rotate 5
compress
delaycompress
missingok
notifempty
create 644 root root
postrotate
/usr/bin/killall -HUP rsyslogd
endscript
}
这里:

monthly: 日志文件将按月轮循。其它可用值为‘daily’,‘weekly’或者‘yearly’。
rotate 5: 一次将存储5个归档日志。对于第六个归档,时间最久的归档将被删除。
compress: 在轮循任务完成后,已轮循的归档将使用gzip进行压缩。
delaycompress: 总是与compress选项一起用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩将在下一次轮循周期进行。这在你或任何软件仍然需要读取最新归档时很有用。
missingok: 在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误。
notifempty: 如果日志文件为空,轮循不会进行。
create 644 root root: 以指定的权限创建全新的日志文件,同时logrotate也会重命名原始日志文件。
postrotate/endscript: 在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种情况下,rsyslogd 进程将立即再次读取其配置并继续运行。
上面的模板是通用的,而配置参数则根据你的需求进行调整,不是所有的参数都是必要的。

样例二
在本例中,我们只想要轮循一个日志文件,然而日志文件大小可以增长到50MB。

vim /etc/logrotate.d/log-file

/var/log/log-file {
size=50M
rotate 5
create 644 root root
postrotate
/usr/bin/killall -HUP rsyslogd
endscript
}
样例三
我们想要让旧日志文件以创建日期命名,这可以通过添加dateext常熟实现。

vim /etc/logrotate.d/log-file

/var/log/log-file {
monthly
rotate 5
dateext
create 644 root root
postrotate
/usr/bin/killall -HUP rsyslogd
endscript
}
这将让归档文件在它们的文件名中包含日期信息。

排障
这里提供了一些logrotate设置的排障提示。

  1. 手动运行logrotate
    logrotate可以在任何时候从命令行手动调用。

要调用为/etc/lograte.d/下配置的所有日志调用logrotate:

logrotate /etc/logrotate.conf

要为某个特定的配置调用logrotate:

logrotate /etc/logrotate.d/log-file

  1. 演练
    排障过程中的最佳选择是使用‘-d’选项以预演方式运行logrotate。要进行验证,不用实际轮循任何日志文件,可以模拟演练日志轮循并显示其输出。

logrotate -d /etc/logrotate.d/log-file

正如我们从上面的输出结果可以看到的,logrotate判断该轮循是不必要的。如果文件的时间小于一天,这就会发生了。

  1. 强制轮循
    即使轮循条件没有满足,我们也可以通过使用‘-f’选项来强制logrotate轮循日志文件,‘-v’参数提供了详细的输出。

logrotate -vf /etc/logrotate.d/log-file

reading config file /etc/logrotate.d/log-file
reading config info for /var/log/log-file

Handling 1 logs

rotating pattern: /var/log/log-file forced from command line (5 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/log-file
log needs rotating
rotating log /var/log/log-file, log->rotateCount is 5
dateext suffix '-20140916'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /var/log/log-file.5.gz to /var/log/log-file.6.gz (rotatecount 5, logstart 1, i 5),
old log /var/log/log-file.5.gz does not exist
renaming /var/log/log-file.4.gz to /var/log/log-file.5.gz (rotatecount 5, logstart 1, i 4),
old log /var/log/log-file.4.gz does not exist
. . .
renaming /var/log/log-file.0.gz to /var/log/log-file.1.gz (rotatecount 5, logstart 1, i 0),
old log /var/log/log-file.0.gz does not exist
log /var/log/log-file.6.gz doesn't exist -- won't try to dispose of it
renaming /var/log/log-file to /var/log/log-file.1
creating new /var/log/log-file mode = 0644 uid = 0 gid = 0
running postrotate script
compressing log with: /bin/gzip

  1. Logrotate的记录日志
    logrotate自身的日志通常存放于/var/lib/logrotate/status目录。如果处于排障目的,我们想要logrotate记录到任何指定的文件,我们可以指定像下面这样从命令行指定。

logrotate -vf –s /var/log/logrotate-status /etc/logrotate.d/log-file

  1. Logrotate定时任务
    logrotate需要的cron任务应该在安装时就自动创建了,我把cron文件的内容贴出来,以供大家参考。

cat /etc/cron.daily/logrotate

!/bin/sh

Clean non existent log file entries from status file

cd /var/lib/logrotate
test -e status || touch status
head -1 status > status.clean
sed 's/"//g' status | while read logfile date
do
[ -e "$logfile" ] && echo "\"$logfile\" $date"
done >> status.clean
mv status.clean status

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf

小结一下,logrotate工具对于防止因庞大的日志文件而耗尽存储空间是十分有用的。配置完毕后,进程是全自动的,可以长时间在不需要人为干预下运行。本教程重点关注几个使用logrotate的几个基本样例,你也可以定制它以满足你的需求。

Linux日志文件总管——logrotate的更多相关文章

  1. [转]Linux日志文件总管——logrotate

    FROM : https://linux.cn/article-4126-1.html 日志文件包含了关于系统中发生的事件的有用信息,在排障过程中或者系统性能分析时经常被用到.对于忙碌的服务器,日志文 ...

  2. linux日志文件

    linux日志文件 在系统运行正常的情况下学习了解这些不同的日志文件有助于你在遇到紧急情况时从容找出问题并加以解决. /var/log/messages — 包括整体系统信息,其中也包含系统启动期间的 ...

  3. Linux - 日志文件

    Linux日志文件绝大多数存放在/var/log目录,其中一些日志文件由应用程序创建,其他的则通过syslog来创建. Linux系统日志文件通过syslog守护程序在syslog套接字/dev/lo ...

  4. Linux - 日志文件简介

    Linux日志文件绝大多数存放在/var/log目录,其中一些日志文件由应用程序创建,其他的则通过syslog来创建. Linux系统日志文件通过syslog守护程序在syslog套接字/dev/lo ...

  5. 位于/var/log目录下的20个Linux日志文件

    位于/var/log目录下的20个Linux日志文件[译] from:http://buptguo.com/2014/01/16/linux-var-log-files/ 原文地址:20 Linux ...

  6. Linux日志切割方法[Logrotate、python、shell实现方式]

    Linux日志切割方法[Logrotate.python.shell实现方式] ​ 对于Linux系统安全来说,日志文件是极其重要的工具.不知为何,我发现很多运维同学的服务器上都运行着一些诸如每天切分 ...

  7. [转帖]Linux日志文件utmp、wtmp、lastlog、messages

    Linux日志文件utmp.wtmp.lastlog.messages https://www.cnblogs.com/zhuiluoyu/p/6874255.html 1.有关当前登录用户的信息记录 ...

  8. 常用Linux日志文件功能

    /var/log目录下的20个Linux日志文件功能详解 :   如果愿意在Linux环境方面花费些时间,首先就应该知道日志文件的所在位置以及它们包含的内容.在系统运行正常的情况下学习了解这些不同的日 ...

  9. /var/log目录下的20个Linux日志文件功能详解

    如果愿意在Linux环境方面花费些时间,首先就应该知道日志文件的所在位置以及它们包含的内容.在系统运行正常的情况下学习了解这些不同的日志文件有助于你在遇到紧急情况时从容找出问题并加以解决. 以下介绍的 ...

随机推荐

  1. 【scrapy】关于xpath helper中能匹配,但是在程序里匹配为none的问题

    原本使用xpath helper匹配的时候还是好好的,然鹅,把这个.//*[@id='novelInfo']/table/tbody/tr[3]/td[1]/复制到代码里就不行了,debug的时候显示 ...

  2. Floyd算法简介

    参考:https://blog.csdn.net/qq_35644234/article/details/60875818 一.Floyd算法的介绍    1.算法的特点:    弗洛伊德算法是解决任 ...

  3. centos 7 下 Ceph 配置安装

    一.环境介绍 系统: CentOS Linux release 7.3.1611 (Core) 硬盘: 系统盘:300GB*2-raid 1 OSD:600GB*4-raid 5      ceph ...

  4. POJ-2386.Lakecounting(DFS求连通块)

    本题是一道连通块的入门题,用来练手,后续还会更新连通块的题目. 本题大意:一个n * m 的陆地上面有很多水洼,让你统计水洼的个数并输出. 本题思路:按照顺序遍历陆地,如果发现水洼就将它的八连块都进行 ...

  5. 方法装饰器(Decorator)

    代码: function enhance(target, key, descriptor) { const method = descriptor.value; let extraSpeed = 50 ...

  6. ngular ionic select ng-options 默认选择第一个值的写法

    1. html <select ng-model="selectOrderState" style="border:none;left:0" ng-opt ...

  7. python的定时任务模块--schedule

    首先先安装一下模块 下面我们简单的学习一下schedule模块 先简单的看个示例 import schedule def test(*args,**kwargs): print("hello ...

  8. 先安装win7时IIS的安装

    打开“控制面板”->选择“程序”->选择“打开或关闭windows功能”->在“Internet信息服务”中勾选以下勾选框

  9. how2j网站前端项目——天猫前端(第一次)学习笔记2

    今天早上开始首页内容.首页除了公共页面,还有许多自己的内容:导航和轮播.分类菜单.推荐产品展示,最后还有js的互动. 一.导航和轮播的学习 在自己做图片的轮播时,还是没有一次成功.存在了好几处问题: ...

  10. 针对appium的webdriver执行swipe无效的解决办法

    self.driver.swipe(x1,y1,x2,y1,t) 当时代码里有如上这么一句,当时源码是这么说的: # convenience method added to Appium (NOT S ...