logrotate简述
logrotate程序是一个日志文件管理工具。用于分割日志文件,删除旧的日志文件,并创建新的日志文件,起到“转储”作用。可以节省磁盘空间。
通过在/etc/logrotate.d/ 目录下进行自定义配置相关参数,从而达到想要的切割日志方案。
准备环境
1.检查当前系统是否存在logrotate
rpm -qa logrotate
2.安装logrotate
yum -y install logrotate
注:一般centos系统默认会安装有logrotate
logrotate目录
/etc/logrotate.conf # logrotate主配置文件
/usr/sbin/logrotate # logrotate二进制文件
/etc/logrotate.d/ # 自定义logrotate配置文件
/var/lib/logrotate/logrotate.status # logrotate管理日志执行记录的状态文件
说明:如果您在logrotate.d
目录下配置了Nginx的轮转规则,那么需要等待logrotate.conf
指定的执行周期。默认logrotate.conf指定周期是一周。
可以查看logrotate.conf文件 cat /etc/logrotate.conf
# see "man logrotate" for details
# global options do not affect preceding include directives
# rotate log files weekly
weekly # 每周执行一次轮转存储# keep 4 weeks worth of backlogs
rotate 4 # 保存4个文件,意思就是只保存四周的日志文件也就是一个月# create new (empty) log files after rotating old ones
create # 轮转后,创建一个新的空文件,指定logrotate自动建立新的日志文件,新的日志文件具有和原来的文件一样的权限。# use date as a suffix of the rotated file
dateext # 使用当期日期作为轮转日志的后缀名# uncomment this if you want your log files compressed
#compress# packages drop log rotation information into this directory
include /etc/logrotate.d # 将 /etc/logrotate.d/ 目录中的所有文件都加载进来# system-specific logs may be also be configured here.
logrotate命令参数说明
-d, --debug :debug模式,测试配置文件是否有错误。
-f, --force :强制转储文