1

I am having a monitor with an integrated USB hub (and a physical power-switch) attached to my pi and want to execute two different scripts, when the monitor is turned on and off.

The first part for turning the monitor on is working fine, but when turning it off again, nothing is triggered. Based on other forum posts I tried different variations of the rule, but non were successful.

The rules file in /etc/udev/rules.d (with all three variations)

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"

Testing with udevadm test tells me that the first two remove-rules should trigger, the third one does not.

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.

Turning the monitor off then, does not trigger the monitor_off.sh script. daemon.log has plenty of messages, not sure how to make sense out of them. Below are the ones related to the usb hub (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

I have also tried using ATTRS as opposed to ATTR or ENV and that does trigger the script, however it triggers it multiple times dependent on what is connected to the hub.

OS version is raspbian buster.

3
  • Is the remove rule triggered when you physically disconnect the USB cable? Commented Sep 14, 2023 at 11:12
  • Also, I believe the correct syntax is ENV{PRODUCT}, not ENV{idProduct}. Commented Sep 14, 2023 at 11:22
  • Physically disconnecting the cable has the same effect as flipping the switch: add rule works, remove doesn't. I took the ID_VENDOR and ID_MODEL from the output of udevadm test, changing ID_MODEL to PRODUCT did not solve the issue.
    – treuss
    Commented Sep 14, 2023 at 11:51

1 Answer 1

0

Eventually got it solved with these rules:

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"

It looks like there are only a few attributes available as environment variables, PRODUCT being on of them, which contains vendor, model and something else (0 in my case). The addition of DEVTYPE was necessary to avoid multiple executions as the same PRODUCT value was also set for the USB Ports.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.