mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
translated
This commit is contained in:
parent
bc55b40cc9
commit
649df93f39
@ -1,120 +0,0 @@
|
|||||||
translating---geekpi
|
|
||||||
|
|
||||||
Ultimate guide to configure logrotate utility
|
|
||||||
============================================================
|
|
||||||
|
|
||||||
|
|
||||||
Generally speaking Logs are very much important part of any troubleshooting activity, However these logs grows in size with time. In this case we need to perform log cleanup manually in order to reclaim the space and its tedious task to administer these logs. To overcome this we can configure logrotate utility available in Linux which automatically does rotation, compression , removal and mailing of logfile.
|
|
||||||
|
|
||||||
We can configure logrotate utility so that each log file may be handled daily, weekly, monthly,or when it grows too large.
|
|
||||||
|
|
||||||
How logrotate utility works:
|
|
||||||
|
|
||||||
By default, the logrotate command runs as a cron job once a day from `/etc/cron.daily`, and it helps you set a policy where log-files that grow beyond a certain age or size are rotated.
|
|
||||||
|
|
||||||
Command: `/usr/sbin/logrotate`
|
|
||||||
|
|
||||||
Configuration File : `/etc/logrotate.conf`
|
|
||||||
|
|
||||||
This is main configuration file for logrotate utility.The logrotate configuration files are also stored for specific services in the directory “`/etc/logrotate.d/`”. Make sure below code exists in `/etc/logrotate.conf` for reading out service specific log rotation configuration.
|
|
||||||
|
|
||||||
` include /etc/logrotate.d`
|
|
||||||
|
|
||||||
Logrotate History: `/var/lib/logrotate.status`
|
|
||||||
|
|
||||||
Important logrotate utility options:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
compress --> Compresses all noncurrent versions of the log file
|
|
||||||
daily,weekly,monthly -->Rotating log files on the specified schedule
|
|
||||||
delaycompress -->Compresses all versions but current and next-most-recent
|
|
||||||
endscript --> Marks the end of a prerotate or postrotate script
|
|
||||||
errors "emailid" --> Email error notification to specified emailaddr
|
|
||||||
missingok --> Do not complain if log file is missing
|
|
||||||
notifempty --> Does not rotate the log file if it is empty
|
|
||||||
olddir "dir" --> Specifies that older verions of the log file be placed in "dir"
|
|
||||||
postrotate --> Introduce a script to be run after log has been rotated
|
|
||||||
prerotate -->Introduce a script to be run before any changes are made
|
|
||||||
rotate 'n' -->Include 'n' versions of the log in the rotation scheme
|
|
||||||
sharedscripts -->Runs scripts only once for the entire log group
|
|
||||||
size='logsize' -->Rotates if log file size > 'logsize (eg 100K, 4M)
|
|
||||||
```
|
|
||||||
|
|
||||||
Let’s configure logrotate utility for our own sample log file “`/tmp/sample_output.log`”.
|
|
||||||
|
|
||||||
Step1: Add below lines of code in the “`/etc/logrotate.conf`” file.
|
|
||||||
|
|
||||||
```
|
|
||||||
/tmp/sample_output.log {
|
|
||||||
size 1k
|
|
||||||
create 700 root root
|
|
||||||
rotate 4
|
|
||||||
compress
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
In the above configuration code:
|
|
||||||
|
|
||||||
* size 1k – logrotate runs only if the file size is equal to (or greater than) this size.
|
|
||||||
* create – rotate the original file and create the new file with specified permission, user and group.
|
|
||||||
* rotate – limits the number of log file rotation. So, this would keep only the recent 4 rotated log files.
|
|
||||||
* compress– This will compress the file.
|
|
||||||
|
|
||||||
Step 2: Normally, you would have to wait a day until logrotate is started from `/etc/cron.daily`. As an alternative, you can run it from the command line using the following command:
|
|
||||||
|
|
||||||
```
|
|
||||||
/usr/sbin/logrotate /etc/logrotate.conf
|
|
||||||
```
|
|
||||||
|
|
||||||
Output Before execution of logrotate command:
|
|
||||||
|
|
||||||
```
|
|
||||||
[root@rhel1 tmp]# ls -l /tmp/
|
|
||||||
total 28
|
|
||||||
-rw-------. 1 root root 20000 Jan 1 05:23 sample_output.log
|
|
||||||
```
|
|
||||||
|
|
||||||
Output After execution of logrotate command:
|
|
||||||
|
|
||||||
```
|
|
||||||
[root@rhel1 tmp]# ls -l /tmp
|
|
||||||
total 12
|
|
||||||
-rwx------. 1 root root 0 Jan 1 05:24 sample_output.log
|
|
||||||
-rw-------. 1 root root 599 Jan 1 05:24 sample_output.log-20170101.gz
|
|
||||||
[root@rhel1 tmp]#
|
|
||||||
```
|
|
||||||
|
|
||||||
So this confirms successful implementation of logrotate utility.
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
作者简介:
|
|
||||||
|
|
||||||
Hi there! I am Manmohan Mirkar. I'm so happy you're here! I began this journey in Linux over 10 years ago and I would have never dreamed that I'd be where I am today. My passion is to help you get Knowledge on Linux.Thank you for being here!
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: http://www.linuxroutes.com/configure-logrotate/
|
|
||||||
|
|
||||||
作者:[Manmohan Mirkar][a]
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]:http://www.linuxroutes.com/author/admin/
|
|
||||||
[1]:http://www.linuxroutes.com/configure-logrotate/#
|
|
||||||
[2]:http://www.linuxroutes.com/configure-logrotate/#
|
|
||||||
[3]:http://www.linuxroutes.com/configure-logrotate/#
|
|
||||||
[4]:http://www.linuxroutes.com/configure-logrotate/#
|
|
||||||
[5]:http://www.linuxroutes.com/configure-logrotate/#
|
|
||||||
[6]:http://www.linuxroutes.com/configure-logrotate/#
|
|
||||||
[7]:http://www.linuxroutes.com/configure-logrotate/#
|
|
||||||
[8]:http://www.linuxroutes.com/configure-logrotate/#
|
|
||||||
[9]:http://www.linuxroutes.com/configure-logrotate/#
|
|
||||||
[10]:http://www.linuxroutes.com/configure-logrotate/#
|
|
||||||
[11]:http://www.linuxroutes.com/configure-logrotate/#
|
|
||||||
[12]:http://www.linuxroutes.com/author/admin/
|
|
||||||
[13]:http://www.linuxroutes.com/configure-logrotate/#respond
|
|
||||||
[14]:http://www.linuxroutes.com/configure-logrotate/#
|
|
@ -64,7 +64,7 @@ RAID 分区拥有高级功能,如冗余和更好的性能。所以让我们来
|
|||||||
via: http://www.linuxroutes.com/linux-raid-1/
|
via: http://www.linuxroutes.com/linux-raid-1/
|
||||||
|
|
||||||
作者:[Manmohan Mirkar][a]
|
作者:[Manmohan Mirkar][a]
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
译者:[geekpi](https://github.com/geekpi)
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
@ -0,0 +1,125 @@
|
|||||||
|
配置 logrotate 的最终指导
|
||||||
|
============================================================
|
||||||
|
|
||||||
|
|
||||||
|
Generally speaking Logs are very much important part of any troubleshooting activity, However these logs grows in size with time. In this case we need to perform log cleanup manually in order to reclaim the space and its tedious task to administer these logs. To overcome this we can configure logrotate utility available in Linux which automatically does rotation, compression , removal and mailing of logfile.
|
||||||
|
|
||||||
|
We can configure logrotate utility so that each log file may be handled daily, weekly, monthly,or when it grows too large.
|
||||||
|
|
||||||
|
How logrotate utility works:
|
||||||
|
|
||||||
|
By default, the logrotate command runs as a cron job once a day from `/etc/cron.daily`, and it helps you set a policy where log-files that grow beyond a certain age or size are rotated.
|
||||||
|
一般来说,日志是任何故障排除中非常重要的一部分,但这些日志会随着时间增长。在这种情况下,我们需要手动执行日志清理,以回收空间以及管理这些日志的繁琐的任务。为了克服这个问题,我们可以在 Linux 中配置 logrotate 程序,它可以自动执行日志文件的轮换、压缩、删除和邮件发送。
|
||||||
|
|
||||||
|
我们可以配置 logrotate 程序,以便每个日志文件可以每天、每周、每月或当它变得太大时处理。
|
||||||
|
|
||||||
|
logrotate 是如何工作的:
|
||||||
|
|
||||||
|
默认情况下,logrotate 命令作为 cron 作业从 /etc/cron.daily 中每天运行一次,它会帮助你设置一个策略,其中超过某个时间或大小的日志文件被轮换。
|
||||||
|
|
||||||
|
命令: `/usr/sbin/logrotate`
|
||||||
|
|
||||||
|
配置文件: `/etc/logrotate.conf`
|
||||||
|
|
||||||
|
这是 logrotate 的主配置文件。logrotate 还在 “`/etc/logrotate.d/`” 中存储了特殊服务配置。确保下面的那行包含在 `/etc/logrotate.conf` 中,以读取特定服务日志配置。
|
||||||
|
|
||||||
|
` include /etc/logrotate.d`
|
||||||
|
|
||||||
|
logrotate 历史: `/var/lib/logrotate.status`
|
||||||
|
|
||||||
|
重要的 logrotate 选项:
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
compress --> 压缩日志文件的所有非当前版本
|
||||||
|
daily,weekly,monthly --> 按指定计划上轮换日志文件
|
||||||
|
delaycompress --> 压缩所有版本,除了当前和下一个最近的
|
||||||
|
endscript --> 标记 prerotate 或 postrotate 脚本的结束
|
||||||
|
errors "emailid" --> 给指定邮箱发送错误通知
|
||||||
|
missingok --> 如果日志文件丢失,不要显示错误
|
||||||
|
notifempty --> 如果日志文件为空,则不轮换日志文件
|
||||||
|
olddir "dir" --> 指定日志文件的旧版本放在 “dir” 中
|
||||||
|
postrotate --> 引入一个在日志被轮换后执行的脚本
|
||||||
|
prerotate --> 引入一个在日志被轮换前执行的脚本
|
||||||
|
rotate 'n' --> 在轮换方案中包含日志的 N 个版本
|
||||||
|
sharedscripts --> 对于整个日志组只运行一次脚本
|
||||||
|
size='logsize' --> 在日志大小大于 logsize(例如100K,4M)时轮换
|
||||||
|
```
|
||||||
|
|
||||||
|
让我们为我们自己的示例日志文件 “`/tmp/sample_output.log`” 配置 logrotate。
|
||||||
|
|
||||||
|
第一步:在 “`/etc/logrotate.conf`” 中添加下面的行。
|
||||||
|
|
||||||
|
```
|
||||||
|
/tmp/sample_output.log {
|
||||||
|
size 1k
|
||||||
|
create 700 root root
|
||||||
|
rotate 4
|
||||||
|
compress
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
在上面的配置文件中:
|
||||||
|
|
||||||
|
* size 1k - logrotate 仅在文件大小等于(或大于)此大小时运行。
|
||||||
|
* create - 轮换原始文件并创建具有指定权限、用户和组的新文件。
|
||||||
|
* rotate - 限制日志文件轮转的数量。因此,这将只保留最近的4个轮转的日志文件。
|
||||||
|
* compress - 这将压缩文件。
|
||||||
|
|
||||||
|
第二步:通常,你需要等待一天直到 logrotate 在 `/etc/cron.daily` 中执行。如此之外,你可以用下面的命令在命令行中运行:
|
||||||
|
|
||||||
|
```
|
||||||
|
/usr/sbin/logrotate /etc/logrotate.conf
|
||||||
|
```
|
||||||
|
|
||||||
|
在执行 logrotate 命令之前的输出:
|
||||||
|
|
||||||
|
```
|
||||||
|
[root@rhel1 tmp]# ls -l /tmp/
|
||||||
|
total 28
|
||||||
|
-rw-------. 1 root root 20000 Jan 1 05:23 sample_output.log
|
||||||
|
```
|
||||||
|
|
||||||
|
在执行 logrotate 之后的输出:
|
||||||
|
|
||||||
|
```
|
||||||
|
[root@rhel1 tmp]# ls -l /tmp
|
||||||
|
total 12
|
||||||
|
-rwx------. 1 root root 0 Jan 1 05:24 sample_output.log
|
||||||
|
-rw-------. 1 root root 599 Jan 1 05:24 sample_output.log-20170101.gz
|
||||||
|
[root@rhel1 tmp]#
|
||||||
|
```
|
||||||
|
|
||||||
|
这样就能确认 logrotate 成功实现了。
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
作者简介:
|
||||||
|
|
||||||
|
大家好!我是 Manmohan Mirkar。我很高兴你们在这里!我在 10 多年前开始使用 Linux,我从来没有想过我会到今天这个地步。我的激情是帮助你们获取 Linux 知识。谢谢你们在这!
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: http://www.linuxroutes.com/configure-logrotate/
|
||||||
|
|
||||||
|
作者:[Manmohan Mirkar][a]
|
||||||
|
译者:[geekpi](https://github.com/geekpi)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]:http://www.linuxroutes.com/author/admin/
|
||||||
|
[1]:http://www.linuxroutes.com/configure-logrotate/#
|
||||||
|
[2]:http://www.linuxroutes.com/configure-logrotate/#
|
||||||
|
[3]:http://www.linuxroutes.com/configure-logrotate/#
|
||||||
|
[4]:http://www.linuxroutes.com/configure-logrotate/#
|
||||||
|
[5]:http://www.linuxroutes.com/configure-logrotate/#
|
||||||
|
[6]:http://www.linuxroutes.com/configure-logrotate/#
|
||||||
|
[7]:http://www.linuxroutes.com/configure-logrotate/#
|
||||||
|
[8]:http://www.linuxroutes.com/configure-logrotate/#
|
||||||
|
[9]:http://www.linuxroutes.com/configure-logrotate/#
|
||||||
|
[10]:http://www.linuxroutes.com/configure-logrotate/#
|
||||||
|
[11]:http://www.linuxroutes.com/configure-logrotate/#
|
||||||
|
[12]:http://www.linuxroutes.com/author/admin/
|
||||||
|
[13]:http://www.linuxroutes.com/configure-logrotate/#respond
|
||||||
|
[14]:http://www.linuxroutes.com/configure-logrotate/#
|
Loading…
Reference in New Issue
Block a user