0

我正在尝试提出一个脚本来监视 /proc/mounts 并在检测到只读文件系统时通知回来。

在 python 中,一种方法是将 /proc/mounts 的值存储在列表中,并cat /proc/mounts在循环中继续执行 a 并直接检查“ro”实体。但是我想使用 poll 或 select 来代替它,因为这样很有效并且仅在事件发生时才采取行动。

我看到民意调查更适合选择。据我了解,我们可以将 /proc/mounts 的 fd 放在 exceptfds 中进行选择,当有异常时我们会收到通知(请在此处更正我,行中的更改会发出异常信号?)。只是为了测试,我在一个普通文件上尝试它 - 现在,当我打开并e.txt对此文件进行更改时,是否预计会打印整个文件?目前,它没有。我错过了什么?

import select

f = open("e.txt")
while True:
    r,w,x = select.select([],[],[f])
    f.seek(0)
    print f.read()
    < check for ro entity and do further>

对于 /proc/mounts 监控:

import select

f = open("/proc/mounts")
while True:
    r,w,x = select.select([],[],[f])
    f.seek(0)
    print f.read()
    < check for ro entity and do further>

如何使用 select 或 poll 来实现这一点。

4

1 回答 1

0

这确实适用于 /proc/mounts 监控。对行的任何更改都被视为异常,因此将 /proc/mounts 的 fd 添加到 exceptfds(这是 select of 中的第三个参数select.select([],[],[f])

import select

f = open("e.txt")
while True:
    r,w,x = select.select([],[],[f])
    f.seek(0)
    print f.read()
于 2021-06-15T03:22:16.110 回答