如何使用 logrotate 命令保持日志文件更新
发布时间:2022-09-22 12:52:54 所属栏目:应用 来源:
导读: 导读
使用此保持日志文件更新。logrotate实用程序在管理日志方面非常出色。它可以轮转日志、压缩日志、通过电子邮件发送日志、删除日志、归档日志,并在你需要时开始记录最新的。
日志非常适合找
使用此保持日志文件更新。logrotate实用程序在管理日志方面非常出色。它可以轮转日志、压缩日志、通过电子邮件发送日志、删除日志、归档日志,并在你需要时开始记录最新的。
日志非常适合找
导读 使用此保持日志文件更新。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/logrotate extension?is?now?.log ? Handling?1?logs ? rotating?pattern:?/var/log/nginx/access.log /var/log/nginx/error.log???1?bytes?(30?rotations) empty?log?files?are?not?rotated,?old?logs?are?removed considering?log?/var/log/nginx/access.log ??log?needs?rotating considering?log?/var/log/nginx/error.log ??log?does?not?need?rotating rotating?log?/var/log/nginx/access.log,?log->rotateCount?is?30 Converted?'?-%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?failed glob?finding?old?rotated?logs?failed renaming?/var/log/nginx/access.log?to?/var/log/nginx/access-2021-08-27-1485508250.log creating?new?/var/log/nginx/access.log?mode?=?0544?uid?=?33?gid?=?4 running?postrotate?script *?Reloading?nginx?configuration?nginx 第二次运行它: reading?config?file?/tmp/logrotate extension?is?now?.log ? Handling?1?logs ? rotating?pattern:?/var/log/nginx/access.log /var/log/nginx/error.log???1?bytes?(30?rotations) empty?log?files?are?not?rotated,?old?logs?are?removed considering?log?/var/log/nginx/access.log ??log?needs?rotating considering?log?/var/log/nginx/error.log ??log?does?not?need?rotating rotating?log?/var/log/nginx/access.log,?log->rotateCount?is?30 Converted?'?-%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/gzip renaming?/var/log/nginx/access.log?to?/var/log/nginx/access-2021-08-27-1485508280.log creating?new?/var/log/nginx/access.log?mode?=?0544?uid?=?33?gid?=?4 running?postrotate?script *?Reloading?nginx?configuration?nginx 第三次运行它: reading?config?file?/tmp/logrotate extension?is?now?.log ? Handling?1?logs ? rotating?pattern:?/var/log/nginx/access.log /var/log/nginx/error.log???1?bytes?(30?rotations) empty?log?files?are?not?rotated,?old?logs?are?removed considering?log?/var/log/nginx/access.log ??log?needs?rotating considering?log?/var/log/nginx/error.log ??log?does?not?need?rotating rotating?log?/var/log/nginx/access.log,?log->rotateCount?is?30 Converted?'?-%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/gzip renaming?/var/log/nginx/access.log?to?/var/log/nginx/access-2021-08-27-1485508316.log creating?new?/var/log/nginx/access.log?mode?=?0544?uid?=?33?gid?=?4 running?postrotate?script *?Reloading?nginx?configuration?nginx (编辑:开发网_商丘站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐