如何获取导致文件系统事件的进程的 PID?

How would I get the PID of the process causing a file system event?

我正在考虑与 python-watchdog 一起工作。有没有办法确定哪个进程 (PID) 导致了检测到的事件?


背景:我正在寻找一种方法来检测由 vimpycharmkate 等某些编辑器引起的修改事件。简而言之,这些编辑器在保存更改时不会 "modify" 原始文件。相反,他们创建了一个新的并将其与旧的交换 - 以略有不同的各种方式。参见 related issue in python-watchdog。我没有重新配置这些编辑器,而是在寻找检测 [create/delete/move] 事件序列和 可靠地 重新解释为 "pseudo modify" 事件的方法原始文件。

以上演示可用here.

任何基于inotify, such as python-watchdog, will not be able to provide the process ID (PID). It is essentially a specification limitation of inotify itself. The only place to actually catch the PID is in the VFS-layer操作系统的解决方案。这意味着必须能够访问实际的文件系统实现 - 或者从头开始实现文件系统。

我实施 LoggedFS-python, a FUSE file system 正是出于这个目的。它正在将任何操作传递给底层 "actual" 文件系统,并且可以访问导致操作的 PID,以及其他附加信息。

loggedfs.loggedfs_notify 提供相关基础设施。