udev规则执行2次

我已经写了一个这样的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 box2@dmz.domain.com 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 box2@dmz.domain.com Using Udev Rule at 04-26-14-52-00. 

我不知道为什么它执行2次,我怎么能阻止它一次工作? 我已经validation了这个问题是在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台服务器上validation过相同的事情。 CentOS和Ubuntu。

该问题已通过在规则中添加ENV{DEVTYPE}=="usb_device"来解决。

我想你的规则匹配/dev/sdf设备,但也是第一个分区/dev/sdf1 ,所以它被触发两次。

如果你只想匹配/dev/sdf1 ,你可以尝试像/dev/sdf1 KERNEL=="sd*1"