551天前2021-09-18 11:38:07 |    抢沙发  2183 
logrotate 不但可以用于分割nginx日志 它能分割所有的linux日志

创建脚本:

vim /etc/logrotate.d/nginx
写入脚本内容:

/var/log/nginx/*.log{
    daily
    rotate 30
    copytruncate
    missingok
    compress
    dateext
    notifempty
    sharedscripts
    postrotate
        kill -USR1 `cat /var/run/nginx.pid`
    endscript
    #postrotate
    #    sudo nginx -s reload>/dev/null 2>&1
    #endscript
 }
立刻执行脚本:

logrotate -vf /etc/logrotate.d/nginx
添加计划任务:

crontab -e
写入:

0 0 * * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginx
上面是每天0点执行脚本


参数注释:

/var/log/nginx/*.log{   #日志存储位置
    daily   #按天切割
    rotate 30   #保留30天的日志备份
    copytruncate    #这种方式操作的时候, 拷贝和清空之间有一个时间差,可能会丢失部分日志数据。
    missingok      #日志不存在分析,分析下一个
    compress    #转存之后压缩.tar.gz
    dateext     #日志切割后,文件以当前日志为结尾,例如:access-logs-20181125
    notifempty  #空文件不转储
    sharedscripts   #整个日志组运行一次脚本
    postrotate  #这里到包裹的而是要执行的脚本命令
        kill -USR1 `cat /var/run/nginx.pid`
    endscript
    #postrotate #从这里开始其实是不需要的只是另外一种实现让nginx重新创建日志文件的方式
    #    sudo nginx -s reload>/dev/null 2>&1
    #endscript
 }
这个方法能切割任意日志文件

发表评论

暂无评论

登录

忘记密码 ?

切换登录

注册

扫一扫二维码分享