使用 tee(或等效物)但限制最大文件大小或旋转到新文件 [英] Use tee (or equivalent) but limit max file size or rotate to new file

查看:21
本文介绍了使用 tee(或等效物)但限制最大文件大小或旋转到新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 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 并开始一个新的 log/my_program_log.

Then, as it fills... renamed to log/my_program_log000001 and start a new log/my_program_log.

推荐答案

use split:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆