如何检测Linux中是否有其他进程正在写入文件?

How to detect whether a file is being written by any other process in Linux?

如何检测Linux中是否有其他进程正在写入文件?

Before a program open a file to processes it, it wants to ensure no other processes are writing to it. Here, we are sure after the files are written and closed, they will not be written any more. Hence, one-time checking will be good enough.

在程序打开文件进行处理之前,它要确保没有其他进程正在写入该文件。 在这里,我们确定在写入和关闭文件之后,它们将不再被写入。 因此,一次检查就足够了。

You may not know whether the file is being written. However, you may use lsof to check whether the file is opened by any other processes. Programs like cp will close the file after the work is done. So you may bet on it for most situations.

您可能不知道是否正在写入文件。 但是,您可以使用lsof来检查文件是否被其他进程打开。 诸如cp程序将在工作完成后关闭文件。 因此,在大多数情况下,您都可以下注。

For a file without any other process opening it:

对于没有其他任何打开过程的文件:

$ lsof -f -- ~/.bashrc

With one process openning for reading:

随着一个过程的开放阅读:

$ lsof -f -- ~/.bashrcCOMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
less    14558  zma    4r   REG  253,3     1356 27787271 /home/zma/.bashrc

With one process openning for writing:

随着一个开放的写作过程:

$ lsof -f -- ~/.bashrc
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
vim     14840  zma   10w   REG  253,3     1355 27787271 /home/zma/.bashrc
Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。

翻译自: https://www.systutorials.com/how-to-detect-whether-a-file-is-being-written-by-any-other-process-in-linux/

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值