教程集 www.jiaochengji.com
教程集 >  脚本编程  >  shell  >  正文 分享nginx/php/mysql日志轮循的脚本

分享nginx/php/mysql日志轮循的脚本

发布时间:2014-12-04   编辑:jiaochengji.com
本文分享下,nginx、php、mysql中进行日志轮循的脚本,部分节选自软件的默认配置脚本,很实用,有需要的朋友参考下吧。

本文介绍的日志轮循脚本,全部在/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

您可能感兴趣的文章:
分享nginx/php/mysql日志轮循的脚本
shell脚本:MySQL慢查询日志和错误日志按天轮询
分享一个shell for循环+case的脚本(监控程序状态)
shell awk读取Nginx 5分钟内的访问日志数
php系统日志切割的实例
分析apache日志中蜘蛛爬行记录数量的shell脚本(图文)
删除及设置linux日志笔记
mysql超出最大连接数的原因剖析
Linux系统下nginx php实现清理服务器网站日志
logrotate对MySQL日志轮替的实例分享

[关闭]
~ ~