Bash 脚本日志文件轮转
我的 bash 脚本生成一个日志文件。现在我想实现一些日志文件轮换。
假设第一次它被称为somelog.log,下次它被重命名为somelog.log.1 > 和新的日志文件somelog.log。
第三次新日志又是somelog.log,但是somelog.log.1 重命名为 somelog.log.2,旧的 somelog.log 重命名为 somelog.log.1。
我会最多可以授予 5 个。
这是之前完成的吗(示例脚本),有什么建议吗?我很感激任何建议。
My bash script produces a log file. Now i'd like to implement some log file rotation.
Let's say the first time it's called somelog.log, the next time it's renamed to somelog.log.1 and the new log file somelog.log.
The third time the new log is somelog.log again, but somelog.log.1 is renamed to somelog.log.2 and the old somelog.log to somelog.log.1.
I would be able to grant a maximum of eg 5.
Is this done before (sample script), any suggestions. I appreciate any advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这个 bash 函数,它需要两个参数:
来源:
Try this bash function, it takes two parameters:
source:
我刚刚为此编写了一个 bash 脚本:
https://github.com/lingtalfi/logrotator
它基本上检查日志文件的大小,如果超过任意阈值,它将日志文件复制到日志目录中。
它对 cron 友好,或者您也可以手动使用它。
典型的命令如下所示:
I've just made a bash script for that:
https://github.com/lingtalfi/logrotator
It basically checks your log file's size, and if it exceeds an arbitrary threshold, it copies the log file into a log directory.
It's cron friendly, or you can use it manually too.
A typical command looks like that: