Linux日志处理命令logrotate怎么用

发布时间:2022-02-19 11:12:10 作者:小新
来源:亿速云 阅读:163

这篇文章主要介绍了Linux日志处理命令logrotate怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

Linux使用某些软件的时候会产生日志文件,而这些软件本身对日志不进行分割或者压缩处理,久而久之会导致日志文件异常巨大,影响机器性能,配置不高的VPS上尤为严重。而logrotate就是管理这些日志文件的神器,可以对单个日志文件或者某个目录下的文件按时间/大小进行切割,压缩操作;指定日志保存数量;还可以在切割之后运行自定义命令

Linux日志处理命令logrotate怎么用

基本功能

logrotate 实用程序在管理日志方面非常出色。它可以轮转日志、压缩日志、通过电子邮件发送日志、删除日志、归档日志,并在你需要时开始记录最新的。

运行 logrotate 非常简单——只需要运行 logrotate -vs state-file config-file。在上面的命令中,v 选项开启详细模式,s 指定一个状态文件,最后的 config-file 是配置文件,你可以指定需要做什么。

实战演练

让我们看看在我们的系统上静默运行的 logrotate 配置,它管理我们在 /var/log 目录中找到的大量日志。查看该目录中的当前文件。你是否看到很多 *.[number].gz 文件?这就是 logrotate 正在做的。你可以在 /etc/logrotate.d/rsyslog 下找到此配置文件。我的配置文件如下:

/var/log/syslog{        rotate 7        daily        missingok        notifempty        delaycompress        compress        postrotate                reload rsyslog > /dev/null 2>&1 || true        endscript}/var/log/mail.info/var/log/mail.warn/var/log/mail.err/var/log/mail.log/var/log/daemon.log/var/log/kern.log/var/log/auth.log/var/log/user.log/var/log/lpr.log/var/log/cron.log/var/log/debug/var/log/messages{        rotate 4        weekly        missingok        notifempty        compress        delaycompress        sharedscripts        postrotate                reload rsyslog > /dev/null 2>&1 || true        endscript}

该文件首先定义了轮转 /var/log/syslog 文件的说明,这些说明包含在后面的花括号中。以下是它们的含义:

你能弄清楚下一节对上面配置中提到的所有文件做了什么吗?第二节中唯一多出的参数是 sharedscripts,它告诉 logrotate 在所有日志轮转完成之前不要运行 postrotate/endscript 中的部分。它可以防止脚本在每一次轮转时执行,只在最后一次轮转完成时执行。

看点新的东西

我使用下面的配置来处理我系统上的 Nginx 的访问和错误日志。

/var/log/nginx/access.log/var/log/nginx/error.log  {        size 1        missingok        notifempty        create 544 www-data adm        rotate 30        compress        delaycompress        dateext        dateformat -%Y-%m-%d-%s        sharedscripts        extension .log        postrotate                service nginx reload        endscript}

上面的脚本可以使用如下命令运行:

logrotate -vs state-file /tmp/logrotate

第一次运行该命令会给出以下输出:

reading config file /tmp/logrotateextension is now .logHandling 1 logsrotating pattern: /var/log/nginx/access.log/var/log/nginx/error.log   1 bytes (30 rotations)empty log files are not rotated, old logs are removedconsidering log /var/log/nginx/access.log  log needs rotatingconsidering log /var/log/nginx/error.log  log does not need rotatingrotating log /var/log/nginx/access.log, log->rotateCount is 30Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'dateext suffix '-2021-08-27-1485508250'glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'glob finding logs to compress failedglob finding old rotated logs failedrenaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508250.logcreating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4running postrotate script* Reloading nginx configuration nginx

第二次运行它:

reading config file /tmp/logrotateextension is now .logHandling 1 logsrotating pattern: /var/log/nginx/access.log/var/log/nginx/error.log   1 bytes (30 rotations)empty log files are not rotated, old logs are removedconsidering log /var/log/nginx/access.log  log needs rotatingconsidering log /var/log/nginx/error.log  log does not need rotatingrotating log /var/log/nginx/access.log, log->rotateCount is 30Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'dateext suffix '-2021-08-27-1485508280'glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'compressing log with: /bin/gziprenaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508280.logcreating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4running postrotate script* Reloading nginx configuration nginx

第三次运行它:

reading config file /tmp/logrotateextension is now .logHandling 1 logsrotating pattern: /var/log/nginx/access.log/var/log/nginx/error.log   1 bytes (30 rotations)empty log files are not rotated, old logs are removedconsidering log /var/log/nginx/access.log  log needs rotatingconsidering log /var/log/nginx/error.log  log does not need rotatingrotating log /var/log/nginx/access.log, log->rotateCount is 30Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'dateext suffix '-2021-08-27-1485508316'glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'compressing log with: /bin/gziprenaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508316.logcreating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4running postrotate script* Reloading nginx configuration nginx

状态文件的内容如下所示:

logrotate state -- version 2"/var/log/nginx/error.log" 2021-08-27-9:0:0"/var/log/nginx/access.log" 2021-08-27-9:11:56

感谢你能够认真阅读完这篇文章,希望小编分享的“Linux日志处理命令logrotate怎么用”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. Linux中logrotate日志轮询操作总结
  2. 使用linux的logrotate命令管理记录文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

linux logrotate

上一篇:Linux代码编辑器medit怎么用

下一篇:Linux系统中如何使用curl命令

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》