使用tee(或等效文件),但限制最大文件大小或旋转到新文件 [英] Use tee (or equivalent) but limit max file size or rotate to new file
问题描述
我想从UNIX进程捕获输出,但限制最大文件大小和/或旋转到新文件.
I would like to capture output from a UNIX process but limit max file size and/or rotate to a new file.
我看过logrotate,但是它不能实时工作.据我了解,这是一个并行运行的清理"作业.
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.
想象:
my_program | tee --max-bytes 100000 log/my_program_log
会给... 始终将最新的日志文件写入为: 日志/my_program_log
Would give... Always writing latest log file as: log/my_program_log
然后,将其填满...重命名为log/my_program_log000001并启动新的日志/my_program_log.
Then, as it fills... renamed to log/my_program_log000001 and start a new log/my_program_log.
推荐答案
使用拆分:
my_program | tee >(split -d -b 100000 -)
或者,如果您不想看到输出,则可以直接通过管道进行拆分:
Or if you don't want to see the output, you can directly pipe to split:
my_program | split -d -b 100000 -
至于日志轮转,coreutils中没有工具可以自动进行轮转.您可以创建符号链接并使用bash命令定期更新它:
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:
while ((1)); do ln -fns target_log_name $(ls -t | head -1); sleep 1; done
这篇关于使用tee(或等效文件),但限制最大文件大小或旋转到新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!