在使用文件时截断文件 (Linux) [英] Truncating a file while it's being used (Linux)
问题描述
我有一个进程将大量数据写入标准输出,我将其重定向到日志文件.我想通过偶尔将当前文件复制到新名称并截断它来限制文件的大小.
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.
有什么办法可以截断文件吗?或者删除它并以某种方式将进程的标准输出与新文件相关联?
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屋!