在使用文件时截断文件(Linux) [英] Truncating a file while it's being used (Linux)

查看:275
本文介绍了在使用文件时截断文件(Linux)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向stdout写入大量数据的进程,我正在将其重定向到日志文件.我想通过不时地将当前文件复制到一个新名称并将其截断来限制文件的大小.

I have a process that's writing a lot of data to stdout, which I'm redirecting to a log file. I'd like to limit the size of the file by occasionally copying the current file to a new name and truncating it.

我常用的截断文件的技术,例如

My usual techniques of truncating a file, like

cp /dev/null file

不起作用,大概是因为该进程正在使用它.

don't work, presumably because the process is using it.

有什么方法可以截断文件?还是将其删除,并以某种方式将流程的stdout与新文件相关联?

Is there some way I can truncate the file? Or delete it and somehow associate the process' stdout with a new file?

FWIW,这是我无法修改的第三方产品,无法更改其日志记录模型.

FWIW, it's a third party product that I can't modify to change its logging model.

EDIT 重定向似乎与上述副本具有相同的问题-下次将文件写入以下文件时,文件将恢复为先前的大小:

EDIT redirecting over the file seems to have the same issue as the copy above - the file returns to its previous size next time it's written to:

ls -l sample.log ; echo > sample.log ; ls -l sample.log ; sleep 10 ; ls -l sample.log
-rw-rw-r-- 1 user group 1291999 Jun 11  2009 sample.log
-rw-rw-r-- 1 user group 1 Jun 11  2009 sample.log
-rw-rw-r-- 1 user group 1292311 Jun 11  2009 sample.log

推荐答案

看看实用程序split(1),它是GNU Coreutils的一部分.

Take a look at the utility split(1), part of GNU Coreutils.

这篇关于在使用文件时截断文件(Linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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