【发布时间】:2021-08-26 20:50:38
【问题描述】:
我正在尝试编写一个脚本来监控 /proc/mounts 并在检测到只读文件系统时通知回来。
在 python 中,一种方法是将 /proc/mounts 的值存储在一个列表中,并在循环中继续执行cat /proc/mounts 并直接检查“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 来实现这一点。
【问题讨论】:
标签: python linux inotify procfs file-monitoring