Linux系统自带logtotate 实现日志切分

Linux系统自带logtotate 实现日志切分

作者:myadmin |  时间:2015-08-21 |  浏览:3991 |  0 条评论

Linux系统自带logtotate 实现日志切分

这里以nginx的日志切分为例说明

第一步:创建nginx日志切分配置文件

[root@gua-9-126 ~]# cd /etc/logrotate.d/
[root@gua-9-126 logrotate.d]# vim nginx

然后写入如下配置,配置的含义有解释

/home/www/logs/task.access.log {
    #missingok
    #notifempty

    #执行频率 monthly/weekly/daily
    daily

    #保留X个文件
    rotate 20

    #切割后的新文件 权限和所有者/组
    create 0644 root root

    #切割后的文件按照日期命名
    dateext

    #开启日志压缩
    #compress

    #日志文件大于1M后才执行执行切割
    #minsize 1M


    #切割完成后 重启ngxin服务器
    sharedscripts
    postrotate
        if [ -f /usr/local/nginx/logs/nginx.pid ]; then
            kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
        fi
    endscript

}

保存退出

2、设定crontab定时任务

[root@gua-9-126 logrotate.d]# vim /etc/crontab 

加入如下配置

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

#nginx 日志切分 by:O(∩_∩)O  at:15.8.20
#59 23 * * * root /usr/sbin/logrotate -f /etc/logrotate.d/nginx > /dev/null 2>&1

#调试追踪脚本 此处需要指定脚本执行账户
59 23 * * * root /usr/sbin/logrotate -vf /etc/logrotate.d/nginx >> /home/logs/logtotate/crontab_nginx_`date +"\%Y\%m\%d_\%H\%M\%S"`.log 2>&1

保存退出

到此大功告成,看看验证自己的成果吧~

logrotate详细介绍文档: http://blog.sina.com.cn/s/blog_5f54f0be0101h6y8.html

Linux系统自带logtotate 实现日志切分

标签:

相关推荐
更多

发表评论