0

我使用 udev 和 bash 脚本在 Linux bash 中编写了一个程序。

连接 USB 时,会执行 udev 规则,该规则会调用脚本。该脚本读取 /var/log/messages 文件并从日志文件中获取有关连接的 USB 硬盘驱动器的信息并通过电子邮件发送信息。

程序运行良好,但我遇到了问题。当我尝试移除 USB 时,我必须摇晃它(因为它被牢固地注入内部),所以在移除过程中,它会移除、连接、移除连接,然后最终移除。因此,我的电子邮件格式受到干扰,我没有得到正确的信息。它还会生成多封无用的电子邮件,我得到以下日志:

May 04 13:06:13 coil sendEmail[12467]: Email was sent successfully!
May 4 13:06:13 coil vmbackup[12450]: USB Removed: Email sent to backupjobs@domain.com
May  4 13:06:16 coil kernel: [8474935.215393] usb 2-6: USB disconnect, address 126
May  4 13:06:16 coil kernel: [8474935.601292] usb 2-6: new high speed USB device using ehci_hcd and address 127
May  4 13:06:17 coil kernel: [8474935.868637] usb 2-6: configuration #1 chosen from 1 choice
May  4 13:06:17 coil kernel: [8474935.915429] scsi85 : SCSI emulation for USB Mass Storage devices
May  4 13:06:17 coil kernel: [8474935.982734] input: Western Digital My Book as /devices/pci0000:00/0000:00:1d.7/usb2/2-6/2-6:1.1/input/input82
May  4 13:06:17 coil kernel: [8474935.982808] generic-usb 0003:1058:1102.0050: input,hidraw0: USB HID v1.11 Device [Western Digital My Book] on usb-0000:00:1d.7-6/input1
May  4 13:06:17 coil kernel: [8474935.982808] generic-usb 0003:1058:1102.0050: input,hidraw0: USB HID v1.11 Device [Western Digital My Book] on usb-0000:00:1d.7-6/input1
May  4 13:06:17 coil kernel: [8474936.084957] usb 2-6: USB disconnect, address 127
May  4 13:06:17 coil kernel: [8474936.500051] usb 2-6: new high speed USB device using ehci_hcd and address 2
May  4 13:06:17 coil kernel: [8474936.769487] usb 2-6: configuration #1 chosen from 1 choice
May  4 13:06:17 coil kernel: [8474936.815499] scsi86 : SCSI emulation for USB Mass Storage devices
May  4 13:06:18 coil kernel: [8474936.882954] input: Western Digital My Book as /devices/pci0000:00/0000:00:1d.7/usb2/2-6/2-6:1.1/input/input83
May  4 13:06:18 coil kernel: [8474936.883044] generic-usb 0003:1058:1102.0051: input,hidraw0: USB HID v1.11 Device [Western Digital My Book] on usb-0000:00:1d.7-6/input1
May  4 13:06:22 coil kernel: [8474941.837814] scsi 86:0:0:0: Direct-Access     WD       My Book          1028 PQ: 0 ANSI: 4
May  4 13:06:22 coil kernel: [8474941.838208] sd 86:0:0:0: Attached scsi generic sg3 type 0
May  4 13:06:22 coil kernel: [8474941.838208] sd 86:0:0:0: Attached scsi generic sg3 type 0
May  4 13:06:23 coil kernel: [8474941.860051] sd 86:0:0:0: [sdc] 1953525168 512-byte logical blocks: (1.00 TB/931 GiB)
May  4 13:06:23 coil kernel: [8474941.873798] sd 86:0:0:0: [sdc] Write Protect is off
May  4 13:06:23 coil kernel: [8474941.955971]  sdc: sdc1
May  4 13:06:23 coil kernel: [8474942.014853] sd 86:0:0:0: [sdc] Attached SCSI disk
May 04 13:06:23 coil sendEmail[12495]: Email was sent successfully!
May 4 13:06:23 coil vmbackup[12478]: USB Added: Email sent to backupjobs@domain.com
May 04 13:06:26 coil sendEmail[12527]: Email was sent successfully!
May 4 13:06:26 coil vmbackup[12510]: USB Removed: Email sent to backupjobs@domain.com
May 04 13:06:35 coil sendEmail[12546]: Email was sent successfully!
May 4 13:06:35 coil vmbackup[12535]: USB Added: Email sent to backupjobs@domain.com
May 04 13:06:37 coil sendEmail[12576]: Email was sent successfully!
May 4 13:06:37 coil vmbackup[12559]: USB Removed: Email sent to backupjobs@domain.com
May 04 13:06:48 coil sendEmail[12596]: Email was sent successfully!
May 4 13:06:48 coil vmbackup[12585]: USB Added: Email sent to backupjobs@domain.com

现在我知道这不是编码问题。但我想知道我是否能以某种方式解决这个问题?并关心这种情况。

4

1 回答 1

0

你必须在你的脚本中做一些去弹跳。每当它被调用时,检查它是否最近被调用(< 1 或 2 秒),如果是,不要做任何事情。您可以将文件用作 /tmp 中的时间戳作为时间标记。

像这样的东西可能会起作用:

delta=2  # 2 seconds of debounce
stampfile=/tmp/stamp
now=$(date +%s)
then=$(< $stampfile)
[[ -z $then ]] && then=$now
diff=$((now-then))
if [[ $((diff < delta)) ]]; then
   exit
else
   echo $now > $stampfile
fi
于 2012-05-04T21:20:17.770 回答