使用 tee (或等效项)但限制最大文件大小或旋转到新文件
我想捕获 UNIX 进程的输出,但限制最大文件大小和/或旋转到新文件。
我见过 logrotate,但它不能实时工作。据我了解,这是一项并行运行的“清理”工作。
什么是正确的解决方案?我想我会编写一个小脚本来完成它,但我希望有一种使用现有文本工具的简单方法。
想象:
my_program | tee --max-bytes 100000 log/my_program_log
会给... 始终将最新日志文件写入为: log/my_program_log
然后,当它填充时...重命名为 log/my_program_log000001 并启动一个新的 log/my_program_log。
I would like to capture output from a UNIX process but limit max file size and/or rotate to a new file.
I have seen logrotate, but it does not work real-time. As I understand, it is a "clean-up" job that runs in parallel.
What is the right solution? I guess I will write a tiny script to do it, but I was hoping there was a simple way with existing text tools.
Imagine:
my_program | tee --max-bytes 100000 log/my_program_log
Would give...
Always writing latest log file as:
log/my_program_log
Then, as it fills... renamed to log/my_program_log000001 and start a new log/my_program_log.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
另一个解决方案是使用 Apacherotatelogs 实用程序。
或以下脚本:
Another solution will be to use Apache rotatelogs utility.
Or following script:
限制最大大小也可以通过 head 来完成:
请参阅此内容以了解 dd 的优点:https://unix.stackexchange.com/a /121888/
Limiting the max size can also be done with head:
See this for benefits over dd: https://unix.stackexchange.com/a/121888/
使用 split:
或者如果你不想看到输出,你可以直接通过管道来 split:
至于日志轮转,coreutils 中没有工具可以自动执行此操作。您可以创建一个符号链接并使用 bash 命令定期更新它:
use split:
Or if you don't want to see the output, you can directly pipe to split:
As for the log rotation, there's no tool in coreutils that does it automatically. You could create a symlink and periodically update it using a bash command:
在
apache2-utils
包中存在名为rotatelogs
的实用程序,它完全满足您的要求。概要:
rotatelogs [ -l ] [ -L 链接名称 ] [ -p 程序 ] [ -f ] [ -t ] [ -v ] [ -e ] [ -c ] [ -n 文件数量 ] 日志文件旋转时间|文件大小(B|K|M|G) [ 偏移量< /em> ]
示例:
您可以在此链接上阅读完整手册。
In package
apache2-utils
is present utility calledrotatelogs
, it fully meet to your requirements.Synopsis:
rotatelogs [ -l ] [ -L linkname ] [ -p program ] [ -f ] [ -t ] [ -v ] [ -e ] [ -c ] [ -n number-of-files ] logfile rotationtime|filesize(B|K|M|G) [ offset ]
Example:
Full manual you may read on this link.
或使用 awk
它将行保持在一起,因此最大值不准确,但这可能很好,特别是对于日志记录目的。可以使用awk的sprintf来格式化文件名。
这是一个可 pipable 脚本,使用 awk
将其保存到名为“bee”的文件中,运行“
chmod +x bee
”,您可以将其用作或将现有文件拆分为
or using awk
It keeps lines together, so the max is not exact, but this could be nice especially for logging purposes. You can use awk's sprintf to format the file name.
Here's a pipable script, using awk
save this to a file called 'bee', run '
chmod +x bee
' and you can use it asor to split an existing file as
要将大小限制为 100 字节,可以简单地使用 dd:
写入 100 字节时,dd 将关闭管道,并且 my_program 接收 EPIPE。
To limit the size to 100 bytes, you can simply use dd:
When 100 bytes are written, dd will close the pipe and my_program receives EPIPE.
解决这个问题最直接的方法可能是使用 python 和 日志模块 就是为此目的而设计的。创建一个从
stdin
读取并写入stdout
的脚本,并实现下面描述的日志轮换。“logging”模块提供它
完全符合您的要求。
来自 docs.python.org
The most straightforward way to solve this is probably to use python and the logging module which was designed for this purpose. Create a script that read from
stdin
and write tostdout
and implement the log-rotation described below.The "logging" module provides the
which does exactly what you are asking about.
From docs.python.org