本文介绍的日志轮循脚本,全部在/etc/logrotate.d目录中。
用命令查看下目录位置:
 
复制代码 代码示例:
[root@jbxue logrotate.d]# pwd
/etc/logrotate.d
检查下脚本文件是否存在:
 
复制代码 代码示例:
[root@jbxue logrotate.d]# ls 
nginx  php-fpm  mysql 
mysql  nginx  php-fpm
1,nginx日志轮循脚本
 
复制代码 代码示例:
[root@jbxue logrotate.d]# cat nginx 
/data/nginx/logs/*.log {
 daily
 rotate 7
 missingok
 notifempty
 sharedscripts
 postrotate
     if [ -f /var/run/nginx.pid ]; then
         kill -USR1 `cat /var/run/nginx.pid`
     fi
 endscript
 }
2,php日志轮循脚本
 
复制代码 代码示例:
[root@jbxue logrotate.d]# cat php-fpm 
/usr/local/webserver/php/var/log/*.log {
 daily
 rotate 7
 missingok
 notifempty
 sharedscripts
 postrotate
     if [ -f /var/run/nginx.pid ]; then
         kill -USR1 `cat /usr/local/webserver/php/var/run/php-fpm.pid`
     fi
 endscript
 }
3,mysql日志轮循脚本
 
复制代码 代码示例:
[root@jbxue logrotate.d]# cat mysql 
/home/mysql_slow/mysql_slow.log {
    daily
    rotate 7
    dateext
    compress
    missingok
    #notifempty
    sharedscripts
    create 644 mysql mysql
    postrotate
        /data/mysql/bin/mysqladmin flush-logs
    endscript
}
在mysql的日志轮询脚本中,运行时需要指定用户名和密码,或在my.cnf中加入:
 
复制代码 代码示例:
[mysqladmin]
user = root
password = ****1601
[root@jbxue logrotate.d]# crontab -l
59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx
59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/php-fpm
59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/mysql