Log Rotation with logrotate

文章目录

    Ubuntu 自带了 logrotate 程序,其他系统需要自己安装。

    查看 /etc/logrotate.d/nginx 这个配置文件

    # 测试方法
    # /usr/sbin/logrotate -f /etc/logrotate.d/nginx
    /data/logs/*.log {
            # 每天 rotate
            daily
            # 忽略丢失的日志文件,处理下一个
            missingok
            # 日志最多被 rotate 的次数
            rotate 10
            compress
            delaycompress
            notifempty
            create 0640 root root
            # 当有多个日志匹配时,下面的脚本只执行一次,防止跑多次
            sharedscripts
            # nginx will re-open its logs in response to the USR1 signal
            postrotate
                    [ -s /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
            endscript
    }
    

    注意权限问题

    例如:nginx log rotate 失败

    使用 sudo 的原因是,印象中 logrotate 默认是以 root 执行的

    sudo /usr/sbin/logrotate -f /etc/logrotate.d/nginx
    error: error creating output file /var/log/nginx/access.log.1.gz: Permission denied
    error: error creating output file /var/log/nginx/error.log.1.gz: Permission denied
    

    调整 nginx 日志目录权限之后,就一切正常了。

    sudo chown -R www-data:adm /var/log/nginx/
    

    2019-11-28 更新

    又发现了上面的无权限问题,发现是 /var/log/nginx/ 目录的权限被修改了。重新 chown 就能解决。

    但是,不知道为何这个目录的权限会发生改变。

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式