1 工具目录
linux默认会安装logrotate工具,自身的boot.log就是通过它分割转储的。
[root@webmaster log]# ll| grep boot -rw-------. 1 root root 0 Jun 25 03:33 boot.log -rw-------. 1 root root 81499 Jun 23 03:20 boot.log-20180623 -rw-------. 1 root root 28340 Jun 24 03:38 boot.log-20180624 -rw-------. 1 root root 0 Jun 25 03:33 boot.log-20180625
主配置文件为/etc/logrotate.conf,还有个目录/etc/logrotate.d/。系统会按应用,在这个目录中创建转储配置文件。
-rw-r--r-- 1 root root 160 Jan 31 2017 chrony -rw-r--r-- 1 root root 194 Oct 20 2017 httpd -rw-r--r-- 1 root root 868 Feb 4 10:33 mysql -rw-r--r-- 1 root root 351 Oct 17 2017 nginx -rw-r--r-- 1 root root 203 Mar 7 21:37 php-fpm -rw-r--r--. 1 root root 136 Jun 10 2014 ppp -rw-r--r-- 1 root root 224 May 10 2017 syslog -rw-r--r-- 1 root root 100 Oct 18 2017 wpa_supplicant -rw-r--r-- 1 root root 100 Jan 30 21:51 yum
2 转储配置
2.1 nginx
[root@webmaster logrotate.d]# cat nginx /app/nginx/logs/*.log{ daily missingok rotate 30 compress delaycompress notifempty dateext sharedscripts postrotate if [ -f /app/nginx/logs/nginx.pid ]; then kill -USR1 `cat /app/nginx/logs/nginx.pid` fi endscript }
/app/nginx/logs/*.log
#文件匹配
daliy 按天分割
rotate 30 转储周期30天,过期日志自动删除
compress 通过gz格式压缩, 文件名如:jab.com.log-20180625.gz
dateext 文件名按日期命令,如jab.com.log-20180625.gz
postrotate 转储后执行命令,这个是给nginx发送reopen log file信号。
3 logrotate命令
[root@webmaster logs]# logrotate -? Usage: logrotate [OPTION...] <configfile> -d, --debug Don't do anything, just test (implies -v) -f, --force Force file rotation -m, --mail=command Command to send mail (instead of `/bin/mail') -s, --state=statefile Path of state file -v, --verbose Display messages during rotation -l, --log=STRING Log file --version Display version information Help options: -?, --help Show this help message --usage Display brief usage message
在写完一个配置文件后,要用debug检查一下,然后用verbose或者force切割试一下。
[root@webmaster logrotate.d]# logrotate -d /etc/logrotate.d/nginx reading config file /etc/logrotate.d/nginx Allocating hash table for state file, size 15360 B Handling 1 logs rotating pattern: /app/nginx/logs/*.log after 1 days (30 rotations) empty log files are not rotated, old logs are removed considering log /app/nginx/logs/error.log log does not need rotating (log has been already rotated)considering log /app/nginx/logs/jab.com.log log does not need rotating (log has been already rotated)not running postrotate script, since no logs were rotated [root@webmaster logrotate.d]# logrotate -f /etc/logrotate.d/nginx