? logrotate是一个被设计来简化系统管理日志文件的工具,在系统运行时,如果产生大量的日志文件,可以使用该工具进行管理,如/var/log/*文件夹是存储系统和应用日志的目录,如果某些日志文件没有设置归档,可能会一直存储变大导致服务器磁盘空间不足。logrotate是开源的自由软件,可以在github上查看到详细介绍。Logrotate
? 我在centos6和Ubuntu18.04上看到都是默认安装好的了,如果没有安装,可以利用各个系统的安装源进行安装:
yum install logrotate #Centos使用安装源安装
apt install logrotate #Ubuntu使用安装源安装
###当然也可以使用github上的源码编译安装
如果安装好logrotate,会在/etc/目录下有logrotate.conf文件和logrotate.d目录,logrotate.conf是默认的配置文件,logrotate.d目录存储了一些自定义的配置文件。logrotate.conf详细如下所示:
# see "man logrotate" for details
# rotate log files weekly 轮询每周执行一次
weekly
# use the syslog group by default, since this is the owning group
# of /var/log/syslog.
su root syslog
# keep 2 weeks worth of backlogs 保留两个归档文件,因为每周执行一次。即只有两周的归档文件进行保留
rotate 2
# create new (empty) log files after rotating old ones 归档后创建新的文件
create
# uncomment this if you want your log files compressed 是否压缩归档的日志文件,这里注释了compress,默认就是不压缩
#compress
# packages drop log rotation information into this directory
include /etc/logrotate.d #包含/etc/logrotate.d目录下的配置规则
# no packages own wtmp, or btmp -- we‘ll rotate them here
/var/log/wtmp {
missingok
monthly
create 0664 root utmp
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0660 root utmp
rotate 1
}
# system-specific logs may be configured here
上面贴出了logrotate.conf的配置文件信息,我们也可以看到logrotate.d目录下定义的配置,如下面的syslog(/etc/logrotate.d/syslog)文件:
/var/log/cron #匹配/var/log/cron,/var/log/maillog,
/var/log/maillog #/var/log/messages等文件,也可以使用*进
/var/log/messages #行模糊匹配
/var/log/secure
/var/log/spooler
{
weekly #每周执行
compress #压缩,默认使用gzip进行压缩
sharedscripts
postrotate #postrotate...endscript是执行后运行的批处理命令
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true #-HUP参数为挂起进程,更改配置而不需停止并重新启动
endscript
}
? 有了配置文件,那么就可以让logrotate使用配置文件运行了。
logrotate -v /etc/logrotate.conf #根据配置文件执行,如果已经执行过了,默认不会再次执行,-v为--verbose
logrotate -vf /etc/logrotate.d/syslog #强制根据某一配置文件执行-f为--force
#参数可以使用--help查看
logrotate --help
Usage: logrotate [OPTION...] <configfile>
-d, --debug Don‘t do anything, just test (implies -v)
-f, --force Force file rotation
-m, --mail=command Command to send mail (instead of `/bin/mail‘)
-s, --state=statefile Path of state file
-v, --verbose Display messages during rotation
Help options:
-?, --help Show this help message
--usage Display brief usage message
#或者查看手册,里面也有配置的sample和各个参数介绍
man logrotate
参数 | 说明 |
---|---|
daily | 表示每天运行归档一次,除此还有‘weekly’,‘monthly’,‘yearly’ |
rotate 2 | 表示保留2个归档文件 |
dateext | 表示归档的文件以日期命名,如yyyyMMdd |
compress | 归档的文件启用压缩,默认为gzip压缩 |
nocompress | 不启用压缩 |
create mode owner group, create owner group | 归档日志文件后创建新文件的访问权限、所属用户和组等,如create 600 root root |
missingok | 如果归档的日志文件不存在,不提示错误信息 |
prerotate...endscript | 在归档前执行prerotate和endscript里面指定的批处理 |
postrotate...endscript | 在归档后执行postrotate和endscript里面指定的批处理 |
原文:https://www.cnblogs.com/quanbisen/p/13039284.html