我有一个带有集成 USB 集线器(和物理电源开关)的显示器连接到我的 pi,并且想要在打开和关闭显示器时执行两个不同的脚本。
打开显示器的第一部分工作正常,但是当再次关闭显示器时,不会触发任何内容。根据其他论坛帖子,我尝试了该规则的不同变体,但没有成功。
规则文件/etc/udev/rules.d
(包含所有三种变体)
SUBSYSTEM=="usb", ACTION=="add", ATTR{idProduct}=="2514", ATTR{idVendor}=="03f0", RUN+="/home/pi/monitor_on.sh"
SUBSYSTEM=="usb", ACTION=="remove", ATTR{idProduct}=="2514", ATTR{idVendor}=="03f0", RUN+="/home/pi/monitor_off.sh"
SUBSYSTEM=="usb", ACTION=="remove", ENV{ID_MODEL}=="2514", ENV{ID_VENDOR}=="03f0", RUN+="/home/pi/monitor_off.sh"
SUBSYSTEM=="usb", ACTION=="remove", ENV{idProduct}=="2514", ENV{idVendor}=="03f0", RUN+="/home/pi/monitor_off.sh"
测试udevadm test
告诉我,前两个删除规则应该触发,第三个则不会。
udevadm test --action="remove" /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.4
This program is for debugging only, it does not run any program
[...]
Reading rules file: /etc/udev/rules.d/99-monitor.rules
[...]
1-1.4: RUN '/home/pi/monitor_off.sh' /etc/udev/rules.d/99-monitor.rules:2
1-1.4: RUN '/home/pi/monitor_off.sh' /etc/udev/rules.d/99-monitor.rules:3
DEVPATH=/devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.4
DEVNAME=/dev/bus/usb/001/060
DEVTYPE=usb_device
DRIVER=usb
PRODUCT=3f0/2514/0
TYPE=9/0/2
BUSNUM=001
DEVNUM=060
MAJOR=189
MINOR=59
ACTION=remove
SUBSYSTEM=usb
USEC_INITIALIZED=1470318118448
ID_VENDOR=03f0
ID_VENDOR_ENC=03f0
ID_VENDOR_ID=03f0
ID_MODEL=2514
ID_MODEL_ENC=2514
ID_MODEL_ID=2514
ID_REVISION=0000
ID_SERIAL=03f0_2514
ID_BUS=usb
ID_USB_INTERFACES=:090001:090002:
ID_VENDOR_FROM_DATABASE=HP, Inc
ID_MODEL_FROM_DATABASE=4-port hub
ID_PATH=platform-fd500000.pcie-pci-0000:01:00.0-usb-0:1.4
ID_PATH_TAG=platform-fd500000_pcie-pci-0000_01_00_0-usb-0_1_4
ID_FOR_SEAT=usb-platform-fd500000_pcie-pci-0000_01_00_0-usb-0_1_4
TAGS=:seat:
run: '/home/pi/monitor_off.sh'
run: '/home/pi/monitor_off.sh'
Unload module index
Unloaded link configuration context.
然后关闭显示器,不会触发monitor_off.sh
脚本。daemon.log
有很多消息,不知道如何理解它们。以下是与 USB 集线器相关的内容(1-1.4)。
Sep 13 17:57:05 crumble systemd-udevd[5413]: 1-1.4: sd-device-monitor: Passed 326 byte to netlink monitor
Sep 13 17:57:05 crumble systemd-udevd[23020]: 1-1.4: Processing device (SEQNUM=3310, ACTION=unbind)
Sep 13 17:57:05 crumble systemd-udevd[23020]: 1-1.4: IMPORT builtin 'usb_id' /usr/lib/udev/rules.d/50-udev-default.rules:13
Sep 13 17:57:05 crumble systemd-udevd[23020]: 1-1.4: Failed to open USB device 'descriptors' file: No such file or directory
Sep 13 17:57:05 crumble systemd-udevd[23020]: 1-1.4: Failed to get idVendor attribute: No such file or directory
Sep 13 17:57:05 crumble systemd-udevd[23020]: 1-1.4: IMPORT builtin 'usb_id' fails: No such file or directory
Sep 13 17:57:05 crumble systemd-udevd[23020]: 1-1.4: Handling device node '/dev/bus/usb/001/060', devnum=c189:59, mode=0600, uid=0, gid=0
Sep 13 17:57:05 crumble systemd-udevd[23020]: 1-1.4: cannot stat() node '/dev/bus/usb/001/060' (No such file or directory)
Sep 13 17:57:05 crumble systemd-udevd[23020]: 1-1.4: sd-device: Created empty file '/run/udev/data/c189:59' for '/devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.4'
Sep 13 17:57:05 crumble systemd-udevd[23020]: 1-1.4: Device (SEQNUM=3310, ACTION=unbind) processed
Sep 13 17:57:05 crumble systemd-udevd[23020]: 1-1.4: sd-device-monitor: Passed 326 byte to netlink monitor
Sep 13 17:57:05 crumble systemd-udevd[5413]: 1-1.4: sd-device-monitor: Passed 326 byte to netlink monitor
Sep 13 17:57:05 crumble systemd-udevd[23020]: 1-1.4: Processing device (SEQNUM=3311, ACTION=remove)
Sep 13 17:57:05 crumble systemd-udevd[23020]: 1-1.4: Device (SEQNUM=3311, ACTION=remove) processed
Sep 13 17:57:05 crumble systemd-udevd[23020]: 1-1.4: sd-device-monitor: Passed 326 byte to netlink monitor
我还尝试使用ATTRS
与ATTR
or相反的方式ENV
,这确实会触发脚本,但是它会根据连接到集线器的内容多次触发脚本。
操作系统版本是raspbian buster。
答案1
最终通过以下规则解决了这个问题:
SUBSYSTEM=="usb", ACTION=="add", ATTR{idProduct}=="2514", ATTR{idVendor}=="03f0", RUN+="/home/pi/monitor_on.sh"
SUBSYSTEM=="usb", ACTION=="remove", ENV{DEVTYPE}=="usb_device", ENV{PRODUCT}=="3f0/2514/0", RUN+="/home/pi/monitor_off.sh"
看起来只有几个属性可用作环境变量,产品是其中之一,其中包含供应商、产品和其他内容(在我的例子中为 0)。添加 DEVTYPE 是必要的,以避免多次执行,因为还为 USB 端口设置了相同的产品。