欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > linux中的日志分割

linux中的日志分割

2025/5/13 21:43:58 来源:https://blog.csdn.net/m0_56693018/article/details/147818883  浏览:    关键词:linux中的日志分割

1.问题背景,nginx日志过大不好删除

[root@localhost cron.daily]# cd /lk/nginx/log/
[root@localhost log]# ll
总用量 2386188
-rw-r--r--. 1 root root 2078699697 5月   9 13:02 access.log
-rw-r--r--. 1 root root      11138 5月   6 10:28 error.log
[root@localhost log]# du -sh *
2.3G    access.log
12K    error.log
[root@localhost log]# pwd
/lk/nginx/log
[root@localhost log]#

2,要求:日志只保留7天,按天分割
 

 3,实现步骤

1,安装软件

yun -y install crond && systemctl restart crond && systemctl enable crond

2,创建日志切割配置 

vi /etc/logrotate.d/nginx

/lk/nginx/log/access.log {
    daily
    rotate 4
    missingok
    compress
    delaycompress
    notifempty
    create 0640 root root
    dateext
    dateformat -%Y%m%d
}

 3,测试配置

sudo logrotate -d /etc/logrotate.d/nginx

这将模拟日志切割,且不会实际修改文件。如果配置正确且没有错误,你可以去掉 -d 参数,执行实际的日志切割:-f 强制切割

sudo sudo logrotate -f /etc/logrotate.d/nginx

4. 自动化

设置时间

crontab -e

59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx

这表示 logrotate 会在每天在 23:59 自动执行日志轮转 

确保 Cron 服务正在运行

sudo systemctl status crond && systemctl restart crond

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词