yum -y install logrotate
vim /etc/logrotate.conf
see "man logrotate" for details
# rotate log files weekly #每周轮换日志文件
weekly
# keep 4 weeks worth of backlogs #保留4周旋转的积压
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
/var/log/wtmp { #记录时间文件
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
# system-specific logs may be also be configured here.
vim /etc/logrotate.d/nginx #子文件
/var/log/nginx/*.log { #路径
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate #执行命令
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
vim /etc/anacrontab #logrotate 基于crontab运行
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
#每天开机5分钟检查cron.daily目录下所有程序/脚本是否运行。run-parts:运行目录下所有程序/脚本
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
cp /usr/sbin/logrotate /etc/cron.daily/
## **附件():**
# cat /etc/logrotate.conf
# 底下的设定是 "logrotate 的默认值" ,如果別的文件设定了其他的值,
# 就会以其它文件的设定为主
weekly
//默认每一周执行一次rotate轮转工作
rotate 4
//保留多少个日志文件(轮转几次).默认保留四个.就是指定日志文件删除之前轮转的次数,0 指没有备份
create
//自动创建新的日志文件,新的日志文件具有和原来的文件相同的权限;因为日志被改名,因此要创建一个新的来继续存储之前的日志
dateext
//这个参数很重要!就是切割后的日志文件以当前日期为格式结尾,如xxx.log-20131216这样,如果注释掉,切割出来是按数字递增,即前面说的 xxx.log-1这种格式
compress
//是否通过gzip压缩转储以后的日志文件,如xxx.log-20131216.gz ;如果不需要压缩,注释掉就行
include /etc/logrotate.d
# 将 /etc/logrotate.d/ 目录中的所有文件都加载进来
/
var
/log/wtmp {
//仅针对 /var/log/wtmp 所设定的参数
monthly
//每月一次切割,取代默认的一周
minsize 1M
//文件大小超过 1M 后才会切割
create 0664 root utmp
//指定新建的日志文件权限以及所属用户和组
rotate 1
//只保留一个日志.
}
# 这个 wtmp 可记录用户登录系统及系统重启的时间
# 因为有 minsize 的参数,因此不见得每个月一定会执行一次喔.要看文件大小。
nginx的日志切割(logrotate)
最新推荐文章于 2024-05-17 16:02:16 发布