Check if a file is open in another process

前端 未结 2 451
面向向阳花
面向向阳花 2020-12-19 04:56

Is there any way to open a file with non-sharing exclusive read-write access?

A file change event from fs.watch does not necessarily mean that

相关标签:
2条回答
  • 2020-12-19 05:30

    You can use @ronomon/opened to check if a file is open in another process, if any applications have open handles or file descriptors to the file.

    It won't tell you which applications have the file open, only that the file is open in other applications.

    It works on Windows, macOS and Linux and requires privileges only on Linux.

    It uses a native binding on Windows to open the file with exclusive sharing mode to detect any sharing violations due to other processes with open handles.

    On macOS and Linux it wraps lsof.

    Compared to using an alternative such as flock, as far as I understand, flock is only advisory, so it will only work if all processes cooperate to check the lock, which is not the case most of the time if the processes are independent.

    0 讨论(0)
  • 2020-12-19 05:31

    See flock function in fs-ext package.

    0 讨论(0)
提交回复
热议问题