logrotate 程序是一个日志文件管理工具。用来把旧的日志文件删除,并创建新的日志文件,我们把这个过程叫做“转储”。我们可以根据日志文件的大小,也可以根据其天数来转储,这个过程一般通过 cron 程序来执行,比如系统每天的定时任务会执行一次logrotate操作来完成系统日志的转储。logrotate 的默认配置文件是 /etc/logrotate.conf,一般自定义一个新的配置文件来完成相应日志切割管理
compress 通过gzip 压缩转储以后的日志
nocompress 不需要压缩时,用这个参数
copytruncate 用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate 备份日志文件但是不截断
create mode owner group 转储文件,使用指定的文件模式创建新的日志文件
nocreate 不建立新的日志文件
delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。
errors address 专储时的错误信息发送到指定的Email 地址
ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty 如果是空文件的话,不转储
mail address 把转储的日志文件发送到指定的E-mail 地址
nomail 转储时不发送日志文件
olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir 转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
daily 指定转储周期为每天
weekly 指定转储周期为每周
monthly 指定转储周期为每月
rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
tabootext [+] list 让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~
size size 当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem).
dateext 切换后的日志文件会附加上一个短横线和YYYYMMDD格式的日期,没有这个配置项会附加一个小数点加一个数字序号.
dateformat 配合dateext使用可以为切割后的日志加上YYYYMMDD格式的日期
weekly 指定所有的日志文件每周转储一次。
rotate 4 指定转储文件的保留 4份。
errors root 指定错误信息发送给root。
create 指定 logrotate 自动建立新的日志文件,新的日志文件具有和原来的文件一样的权限。
#compress 指定不压缩转储文件,如果需要压缩,去掉注释就可以了。
include 默认include文件夹下的所有配置文件都生效。
4、系统自带的系统日志切割配置/etc/logrotate.d/syslog
该配置只是对messages、maillog、messages、secure、spooler系统日志有效。
[root@some-host ~]# cat /etc/logrotate.d/syslog
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
新建配置文件/etc/logrotate_php_nginx.conf
[root@SH-BA-01-WEB01 ~]# cat /etc/logrotate_php_nginx.conf
# see "man logrotate" for details
# rotate log files weekly
#weekly
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# use date as a suffix of the rotated file
dateext
# uncomment this if you want your log files compressed
#compress
# RPM packages drop log rotation information into this directory
#include /etc/logrotate.d
# no packages own wtmp and btmp -- we'll rotate them here
# ... ...
# ... ...
# system-specific logs may be also be configured here.
/opt/log/php-fpm/php_errors.log
/opt/log/php-fpm/php-fpm.log
/opt/log/php-fpm/www.log.slow
/opt/log/nginx/nginx_error.log
{
size 0
missingok
rotate 4
nocompress
dateext
dateformat -%Y%m%d
copytruncate
}
/opt/log/nginx/xxxxx.xxxxxxx.com
/opt/log/nginx/xxx.xxxx.com
/opt/log/nginx/xxxx.xxx.com
/opt/log/nginx/xxxxxx.xxxxxxxxxx.com
/opt/log/nginx/xxxx.xxxxxxx.com
/opt/log/nginx/xxxxx.xxxxxx.com
/opt/log/nginx/x.xxxxxx.com
/opt/log/nginx/xxxxxx.xxxxxxx.com
{
size 0
missingok
rotate 4
nocompress
dateext
dateformat -%Y%m%d
copytruncate
}
59 23 * * * root sh
/usr/sbin/logrotate
/etc/logrotate_php_nginx
.conf >
/dev/null
2>&1
转载于:https://blog.51cto.com/caduke/1664297