我写了这样的 udev 规则:
SUBSYSTEM=="usb", ACTION=="add", RUN+="//root/usbmon/usb add %b"
SUBSYSTEM=="usb", ACTION=="remove", RUN+="//root/usbmon/usb remove %b"
现在,无论何时连接或移除 USB,此 USB 脚本都会发送电子邮件。
但问题是每次它都会发送 2 封电子邮件。这意味着规则执行 2 次。这是我在日志中得到的结果
Apr 26 14:52:00 box2 sendEmail[3374]: Email was sent successfully!
USB Added: Email sent to [email protected] Using Udev Rule at 04-26-14-52-00.
Apr 26 14:52:00 box2 sendEmail[3394]: Email was sent successfully!
USB Added: Email sent to [email protected] Using Udev Rule at 04-26-14-52-00.
我不知道为什么它会执行 2 次,我如何才能让它停止工作一次?我已经验证了问题出在 udev 端,但不知道如何修复它。这是连接 USB 时 udev 监视器的输出:
KERNEL[1335803018.283341] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4 (usb)
KERNEL[1335803018.284456] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0 (usb)
KERNEL[1335803018.284835] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6 (scsi)
KERNEL[1335803018.284863] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/scsi_host/host6 (scsi_host)
KERNEL[1335803023.524167] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0 (scsi)
KERNEL[1335803023.524735] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0 (scsi)
KERNEL[1335803023.525043] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_disk/6:0:0:0 (scsi_disk)
KERNEL[1335803023.525154] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_device/6:0:0:0 (scsi_device)
KERNEL[1335803023.525505] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_generic/sg6 (scsi_generic)
KERNEL[1335803023.525792] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/bsg/6:0:0:0 (bsg)
KERNEL[1335803023.526534] add /devices/virtual/bdi/8:80 (bdi)
UDEV [1335803023.526895] add /devices/virtual/bdi/8:80 (bdi)
KERNEL[1335803023.543043] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/block/sdf (block)
KERNEL[1335803023.543166] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/block/sdf/sdf1 (block)
UDEV [1335803028.541785] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4 (usb)
UDEV [1335803038.850181] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0 (usb)
UDEV [1335803038.850423] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6 (scsi)
UDEV [1335803038.850541] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0 (scsi)
UDEV [1335803038.850568] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/scsi_host/host6 (scsi_host)
UDEV [1335803038.851736] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0 (scsi)
UDEV [1335803038.851912] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_disk/6:0:0:0 (scsi_disk)
UDEV [1335803038.861401] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_device/6:0:0:0 (scsi_device)
UDEV [1335803038.862757] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/bsg/6:0:0:0 (bsg)
UDEV [1335803038.863880] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_generic/sg6 (scsi_generic)
UDEV [1335803039.002743] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/block/sdf (block)
UDEV [1335803039.072845] add /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/block/sdf/sdf1 (block)
现在上面的输出清楚地显示 udev 在 10 秒后再次连接设备,那就是我再次收到电子邮件的时间(10 秒后)。此外,当脚本不可执行时,我在日志文件中收到以下消息 2 次:
Apr 30 10:22:37 box2 udevd-work[1990]: exec of program '/root/usbmon/usb' failed
Apr 30 10:22:37 box2 udevd-work[1991]: exec of program '/root/usbmon/usb' failed
现在我想知道我是否需要更改这些设置以使 udev 运行一次。我已经在 2 台服务器上验证了同样的事情。CentOS 和 ubuntu。
答案1
ENV{DEVTYPE}=="usb_device"
通过添加规则解决了该问题。
答案2
我猜你的规则与/dev/sdf
设备匹配,但也与第一个分区匹配/dev/sdf1
,所以它会被触发两次。
如果您只想匹配/dev/sdf1
,您可以尝试类似的操作KERNEL=="sd*1"
。