用 logrotate 管理日志文件
Logrotate是基于CRON来运行的,其脚本是/etc/cron.daily/logrotate,日志轮转是系统自动完成的。 实际运行时,Logrotate会调用配置文件/etc/logrotate.conf。 可以在/etc/logrotate.d目录里放置自定义好的配置文件,用来覆盖Logrotate的缺省值。
cat /etc/cron.daily/logrotate #脚本是/etc/cron.daily/logrotate #!/bin/sh /usr/sbin/logrotate /etc/logrotate.conf EXITVALUE=$? if [ $EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" fi exit 0如果等不及cron自动执行日志轮转,想手动强制切割日志,需要加-f参数;不过正式执行前最好通过Debug选项来验证一下(-d参数),这对调试也很重要
logrotate /etc/logrotate.conf #加载指定的配置文件。 /usr/sbin/logrotate -f /etc/logrotate.d/nginx #强制立刻切割。 /usr/sbin/logrotate -d -f /etc/logrotate.d/nginx #强制但只调试验证 logrotate命令格式: logrotate [OPTION...] <configfile> -d, --debug :debug模式,测试配置文件是否有错误。 -f, --force :强制转储文件。 -m, --mail=command :压缩日志后,发送日志到指定邮箱。 -s, --state=statefile :使用指定的状态文件。
cat /etc/logrotate.d/program #配置内容 /var/log/wtmp { monthly create 0664 root utmp minsize 1M compress rotate 1 }