【问题标题】:monitoring proc mounts using poll or select使用 poll 或 select 监控 proc 挂载
【发布时间】: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


    【解决方案1】:

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

    import select
    
    f = open("e.txt")
    while True:
        r,w,x = select.select([],[],[f])
        f.seek(0)
        print f.read()
    

    【讨论】:

      猜你喜欢
      • 2011-07-01
      • 2011-03-20
      • 2010-12-10
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      • 2014-05-31
      相关资源
      最近更新 更多