gpt4 book ai didi

linux - Linux 中的 USB 插件/输出

转载 作者:太空宇宙 更新时间:2023-11-04 04:24:15 25 4
gpt4 key购买 nike

我使用 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

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

最佳答案

您必须在脚本中进行某种去抖动处理。每当调用它时,请检查最近是否调用过它(< 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

关于linux - Linux 中的 USB 插件/输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10455859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com