了解如何在Linux中使用Logrotate管理各种日志
Logrotate设计用于管理生成大量日志记录的Ubuntu系统。它允许自动轮换,压缩,删除和邮寄日志记录。每个日志文件也可以每天,每周,每月或在增长时进行处理。本文介绍了-“了解如何在Linux中使用logrotate管理各种日志”
要安装logrotate,请使用以下命令–
$ sudo apt-get install logrotate
样本输出应如下所示–
Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: libecap3 squid-common squid-langpack Use 'sudo apt autoremove' to remove them. The following NEW packages will be installed: logrotate 0 upgraded, 1 newly installed, 0 to remove and 250 not upgraded. Need to get 37.6 kB of archives. After this operation, 116 kB of additional disk space will be used. Get:1 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 logrotate amd64 3.8.7-2ubuntu2 [37.6 kB] Fetched 37.6 kB in 0s (86.9 kB/s) Selecting previously unselected package logrotate. (Reading database ... 239112 files and directories currently installed.) Preparing to unpack .../logrotate_3.8.7-2ubuntu2_amd64.deb ... Unpacking logrotate (3.8.7-2ubuntu2) ... Processing triggers for man-db (2.7.5-1) ... .................................................................................
要获取有关logrotate的更多选项,请使用以下命令-
$ logrotate --help
样本输出应如下所示–
Usage: logrotate [OPTION...] -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 Help options: -?, --help Show this help message --usage Display brief usage message
logrotate的用法应该是这样–
Usage: logrotate [-dfv?] [-d|--debug] [-f|--force] [-m|--mail=command] [-s|--state=statefile] [-v|--verbose] [--version] [-?|--help] [--usage] [OPTION...] <configfile>
示例
要获取有关logrotate文件的信息,请使用以下命令–
$ cd /etc/logrotate.d/ /etc/logrotate.d$ ls
样本输出应如下所示–
apache2 dbconfig-common munin-node speech-dispatcher upstart apport dpkg mysql-server squid yum apt jenkins pm-utils squidguard cacti lightdm ppp ufw cups-daemon munin rsyslog unattended-upgrades
要获取jenkins的配置,请使用以下命令–
$ sudo nano /etc/logrotate.d/jenkins
样本输出应如下所示–
/var/log/jenkins/jenkins.log { weekly copytruncate missingok rotate 52 compress delaycompress notifempty size 10k dateext maxage 10 compresscmd /bin/bzip2 }
如何在Ubuntu上安装Jenkins
以上结果的摘要应如下所示:
每周-如果当前工作日小于最后一次轮循的工作日,或者如果超过一周又超过一周,则日志记录将被圈出。
copytruncate – 在复制完副本后将已建立的日志文件截断在适当的位置,而不是重新定位旧的日志文件并有选择地增长一个全新的日志文件。
旋转52 – 日志记录比被删除或通过mail指令精确发送到交易的时间早52个实例。
compress–在默认情况下,使用gzip压缩旧版本的日志记录。
delaycompress – 将上一个日志文件的压缩延迟到下一个循环周期。
notifempty – 如果日志为空,则不循环。
大小10k – 如果文件大小等于(或比10k更突出),则logrotate运行。
dateext– 归档日志文件的旧版本,添加日期扩展名(例如YYYYMMDD),而不是随便 添加数字。
最大10 – 删除10天以上的轮换日志。
compresscmd– 指定用于压缩日志文件的命令。
玉米工作
要获得logrotate的每日玉米作业,请使用以下命令,如下所示–
$ 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的状态,请使用以下命令-
$ cat /var/lib/logrotate/status
输出样本应如下所示–
logrotate state -- version 2 "/var/log/syslog" 2017-1-23-9:14:34 "/var/log/cacti/cacti.log" 2017-1-23-9:14:34 "/var/log/mail.log" 2017-1-23-9:14:34 "/var/log/kern.log" 2017-1-23-9:14:34 "/var/log/cups/error_log" 2016-12-17-14:5:58 "/var/log/mysql.log" 2017-1-23-9:0:0 "/var/log/ufw.log" 2017-1-23-9:0:0 "/var/log/cacti/rrd.log" 2017-1-9-11:0:0 "/var/log/lightdm/seat0-greeter.log" 2017-1-23-9:14:34 "/var/log/cacti/poller-error.log" 2017-1-9-11:0:0 "/var/log/munin/munin-update.log" 2017-1-17-9:58:3 "/var/log/speech-dispatcher/speech-dispatcher.log" 2017-1-23-9:0:0 "/var/log/debug" 2017-1-23-9:0:0 "/var/log/yum.log" 2017-1-17-9:0:0 "/var/log/munin/munin-node.log" 2017-1-23-9:14:34 ........................................................................
在以上文章中,我们了解了–了解如何在Linux中使用logrotate管理各种日志。在我们的下一篇文章中,我们将提出更多基于Linux的技巧。继续阅读。