所用阿里云的服务器,带有logrotate 服务,所以怎么安装logrotate 就忽略了。
如果是yum方式安装的nginx,系统默认会自动通过logrotate这个日志管理软件,按天进行分割。
查询logrotate安装位置
rpm -ql logrotate
/etc/cron.daily/logrotate /etc/logrotate.conf /etc/logrotate.d /etc/rwtab.d/logrotate /usr/sbin/logrotate /usr/share/doc/logrotate-3.8.6 /usr/share/doc/logrotate-3.8.6/CHANGES /usr/share/doc/logrotate-3.8.6/COPYING /usr/share/man/man5/logrotate.conf.5.gz /usr/share/man/man8/logrotate.8.gz /var/lib/logrotate /var/lib/logrotate/logrotate.status
logrotate配置文件
logrotate的配置文件是/etc/logrotate.conf,
而/etc/logrotate.d/是用于存储其他配置文件的目录。该目录里的所有文件都会被主动的读入 /etc/logrotate.conf中执行。
logrotate的nginx配置
cat /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
}
说明:
daily:日志文件将按天轮循 weekly:日志文件将按周轮循 monthly:日志文件将按月轮循 nocompress:不需要压缩时,用这个参数 copytruncate:用于还在打开中的日志文件,把当前日志备份并截断 nocopytruncate:备份日志文件但是不截断 create mode owner group:转储文件,使用指定的文件模式创建新的日志文件 nocreate:不建立新的日志文件 delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩 nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。 size size 当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem). dateext:定义日志文件后缀是日期格式,也就是切割后文件是:xxx.log-20160402.gz这样的格式。如果该参数被注释掉,切割出来是按数字递增,即前面说的 xxx.log-1这种格式 delaycompress:总是与compress选项一起用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩将在下一次轮循周期进行。这在你或任何软件仍然需要读取最新归档时很有用 create 640 nginx adm:以指定的权限和用书属性,创建全新的日志文件,同时logrotate也会重命名原始日志文件。 rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
logrotate的默认切割时间
在 /etc/anacrontab 里面配置的。
重点修改:
START_HOURS_RANGE=0-10
cat /etc/anacrontab
默认配置
# /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=0-10 #period in days delay in minutes job-identifier command 1 5 cron.daily nice run-parts /etc/cron.daily 7 25 cron.weekly nice run-parts /etc/cron.weekly @monthly 45 cron.monthly nice run-parts /etc/cron.monthly



