我正在尝试提出一个脚本来监视 /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 来实现这一点。