You are not logged in.

#1 2022-04-30 21:01:39

blah123213
Member
Registered: 2022-04-28
Posts: 5

Udev rule not working/triggering. Run script when monitor is unplugged

here is the output from udevadm monitor:

udevadm monitor
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent

KERNEL[81607.851777] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV  [81607.855618] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
KERNEL[81609.348389] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV  [81609.351185] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)

I know that the specific device in question is card0-DP-1 but here is the info

udevadm info -a -p /devices/pci0000:00/0000:00:02.0/drm/card0

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '/devices/pci0000:00/0000:00:02.0/drm/card0':
    KERNEL=="card0"
    SUBSYSTEM=="drm"
    DRIVER==""
    ATTR{engine/bcs0/.defaults/heartbeat_interval_ms}=="2500"
    ATTR{engine/bcs0/.defaults/max_busywait_duration_ns}=="8000"
    ATTR{engine/bcs0/.defaults/stop_timeout_ms}=="100"
    ATTR{engine/bcs0/capabilities}==""
    ATTR{engine/bcs0/class}=="1"
    ATTR{engine/bcs0/heartbeat_interval_ms}=="2500"
    ATTR{engine/bcs0/instance}=="0"
    ATTR{engine/bcs0/known_capabilities}==""
    ATTR{engine/bcs0/max_busywait_duration_ns}=="8000"
    ATTR{engine/bcs0/mmio_base}=="0x22000"
    ATTR{engine/bcs0/name}=="bcs0"
    ATTR{engine/bcs0/stop_timeout_ms}=="100"
    ATTR{engine/rcs0/.defaults/heartbeat_interval_ms}=="2500"
    ATTR{engine/rcs0/.defaults/max_busywait_duration_ns}=="8000"
    ATTR{engine/rcs0/.defaults/stop_timeout_ms}=="100"
    ATTR{engine/rcs0/capabilities}==""
    ATTR{engine/rcs0/class}=="0"
    ATTR{engine/rcs0/heartbeat_interval_ms}=="2500"
    ATTR{engine/rcs0/instance}=="0"
    ATTR{engine/rcs0/known_capabilities}==""
    ATTR{engine/rcs0/max_busywait_duration_ns}=="8000"
    ATTR{engine/rcs0/mmio_base}=="0x2000"
    ATTR{engine/rcs0/name}=="rcs0"
    ATTR{engine/rcs0/stop_timeout_ms}=="100"
    ATTR{engine/vcs0/.defaults/heartbeat_interval_ms}=="2500"
    ATTR{engine/vcs0/.defaults/max_busywait_duration_ns}=="8000"
    ATTR{engine/vcs0/.defaults/stop_timeout_ms}=="100"
    ATTR{engine/vcs0/capabilities}==""
    ATTR{engine/vcs0/class}=="2"
    ATTR{engine/vcs0/heartbeat_interval_ms}=="2500"
    ATTR{engine/vcs0/instance}=="0"
    ATTR{engine/vcs0/known_capabilities}=="hevc sfc"
    ATTR{engine/vcs0/max_busywait_duration_ns}=="8000"
    ATTR{engine/vcs0/mmio_base}=="0x12000"
    ATTR{engine/vcs0/name}=="vcs0"
    ATTR{engine/vcs0/stop_timeout_ms}=="100"
    ATTR{engine/vecs0/.defaults/heartbeat_interval_ms}=="2500"
    ATTR{engine/vecs0/.defaults/max_busywait_duration_ns}=="8000"
    ATTR{engine/vecs0/.defaults/stop_timeout_ms}=="100"
    ATTR{engine/vecs0/capabilities}==""
    ATTR{engine/vecs0/class}=="3"
    ATTR{engine/vecs0/heartbeat_interval_ms}=="2500"
    ATTR{engine/vecs0/instance}=="0"
    ATTR{engine/vecs0/known_capabilities}=="sfc"
    ATTR{engine/vecs0/max_busywait_duration_ns}=="8000"
    ATTR{engine/vecs0/mmio_base}=="0x1a000"
    ATTR{engine/vecs0/name}=="vecs0"
    ATTR{engine/vecs0/stop_timeout_ms}=="100"
    ATTR{gt_RP0_freq_mhz}=="1300"
    ATTR{gt_RP1_freq_mhz}=="400"
    ATTR{gt_RPn_freq_mhz}=="200"
    ATTR{gt_act_freq_mhz}=="200"
    ATTR{gt_boost_freq_mhz}=="1300"
    ATTR{gt_cur_freq_mhz}=="200"
    ATTR{gt_max_freq_mhz}=="1300"
    ATTR{gt_min_freq_mhz}=="200"
    ATTR{power/control}=="auto"
    ATTR{power/rc6_enable}=="1"
    ATTR{power/rc6_residency_ms}=="67931246"
    ATTR{power/runtime_active_time}=="0"
    ATTR{power/runtime_status}=="unsupported"
    ATTR{power/runtime_suspended_time}=="0"

  looking at parent device '/devices/pci0000:00/0000:00:02.0':
    KERNELS=="0000:00:02.0"
    SUBSYSTEMS=="pci"
    DRIVERS=="i915"
    ATTRS{ari_enabled}=="0"
    ATTRS{boot_vga}=="1"
    ATTRS{broken_parity_status}=="0"
    ATTRS{class}=="0x030000"
    ATTRS{consistent_dma_mask_bits}=="40"
    ATTRS{d3cold_allowed}=="1"
    ATTRS{device}=="0x0416"
    ATTRS{dma_mask_bits}=="40"
    ATTRS{driver_override}=="(null)"
    ATTRS{enable}=="1"
    ATTRS{irq}=="34"
    ATTRS{local_cpulist}=="0-7"
    ATTRS{local_cpus}=="ff"
    ATTRS{msi_bus}=="1"
    ATTRS{msi_irqs/34}=="msi"
    ATTRS{numa_node}=="-1"
    ATTRS{power/autosuspend_delay_ms}=="10000"
    ATTRS{power/control}=="on"
    ATTRS{power/runtime_active_time}=="73913164"
    ATTRS{power/runtime_status}=="active"
    ATTRS{power/runtime_suspended_time}=="0"
    ATTRS{remove}=="(write-only)"
    ATTRS{rescan}=="(write-only)"
    ATTRS{reset}=="(write-only)"
    ATTRS{revision}=="0x06"
    ATTRS{subsystem_device}=="0x220e"
    ATTRS{subsystem_vendor}=="0x17aa"
    ATTRS{vendor}=="0x8086"

  looking at parent device '/devices/pci0000:00':
    KERNELS=="pci0000:00"
    SUBSYSTEMS==""
    DRIVERS==""
    ATTRS{power/control}=="auto"
    ATTRS{power/runtime_active_time}=="0"
    ATTRS{power/runtime_status}=="unsupported"
    ATTRS{power/runtime_suspended_time}=="0"
udevadm info -a -p /devices/pci0000:00/0000:00:02.0/drm/card0/card0-DP-1

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '/devices/pci0000:00/0000:00:02.0/drm/card0/card0-DP-1':
    KERNEL=="card0-DP-1"
    SUBSYSTEM=="drm"
    DRIVER==""
    ATTR{dpms}=="On"
    ATTR{edid}==""
    ATTR{enabled}=="enabled"
    ATTR{power/control}=="auto"
    ATTR{power/runtime_active_time}=="0"
    ATTR{power/runtime_status}=="unsupported"
    ATTR{power/runtime_suspended_time}=="0"
    ATTR{status}=="connected"

  looking at parent device '/devices/pci0000:00/0000:00:02.0/drm/card0':
    KERNELS=="card0"
    SUBSYSTEMS=="drm"
    DRIVERS==""
    ATTRS{engine/bcs0/.defaults/heartbeat_interval_ms}=="2500"
    ATTRS{engine/bcs0/.defaults/max_busywait_duration_ns}=="8000"
    ATTRS{engine/bcs0/.defaults/stop_timeout_ms}=="100"
    ATTRS{engine/bcs0/capabilities}==""
    ATTRS{engine/bcs0/class}=="1"
    ATTRS{engine/bcs0/heartbeat_interval_ms}=="2500"
    ATTRS{engine/bcs0/instance}=="0"
    ATTRS{engine/bcs0/known_capabilities}==""
    ATTRS{engine/bcs0/max_busywait_duration_ns}=="8000"
    ATTRS{engine/bcs0/mmio_base}=="0x22000"
    ATTRS{engine/bcs0/name}=="bcs0"
    ATTRS{engine/bcs0/stop_timeout_ms}=="100"
    ATTRS{engine/rcs0/.defaults/heartbeat_interval_ms}=="2500"
    ATTRS{engine/rcs0/.defaults/max_busywait_duration_ns}=="8000"
    ATTRS{engine/rcs0/.defaults/stop_timeout_ms}=="100"
    ATTRS{engine/rcs0/capabilities}==""
    ATTRS{engine/rcs0/class}=="0"
    ATTRS{engine/rcs0/heartbeat_interval_ms}=="2500"
    ATTRS{engine/rcs0/instance}=="0"
    ATTRS{engine/rcs0/known_capabilities}==""
    ATTRS{engine/rcs0/max_busywait_duration_ns}=="8000"
    ATTRS{engine/rcs0/mmio_base}=="0x2000"
    ATTRS{engine/rcs0/name}=="rcs0"
    ATTRS{engine/rcs0/stop_timeout_ms}=="100"
    ATTRS{engine/vcs0/.defaults/heartbeat_interval_ms}=="2500"
    ATTRS{engine/vcs0/.defaults/max_busywait_duration_ns}=="8000"
    ATTRS{engine/vcs0/.defaults/stop_timeout_ms}=="100"
    ATTRS{engine/vcs0/capabilities}==""
    ATTRS{engine/vcs0/class}=="2"
    ATTRS{engine/vcs0/heartbeat_interval_ms}=="2500"
    ATTRS{engine/vcs0/instance}=="0"
    ATTRS{engine/vcs0/known_capabilities}=="hevc sfc"
    ATTRS{engine/vcs0/max_busywait_duration_ns}=="8000"
    ATTRS{engine/vcs0/mmio_base}=="0x12000"
    ATTRS{engine/vcs0/name}=="vcs0"
    ATTRS{engine/vcs0/stop_timeout_ms}=="100"
    ATTRS{engine/vecs0/.defaults/heartbeat_interval_ms}=="2500"
    ATTRS{engine/vecs0/.defaults/max_busywait_duration_ns}=="8000"
    ATTRS{engine/vecs0/.defaults/stop_timeout_ms}=="100"
    ATTRS{engine/vecs0/capabilities}==""
    ATTRS{engine/vecs0/class}=="3"
    ATTRS{engine/vecs0/heartbeat_interval_ms}=="2500"
    ATTRS{engine/vecs0/instance}=="0"
    ATTRS{engine/vecs0/known_capabilities}=="sfc"
    ATTRS{engine/vecs0/max_busywait_duration_ns}=="8000"
    ATTRS{engine/vecs0/mmio_base}=="0x1a000"
    ATTRS{engine/vecs0/name}=="vecs0"
    ATTRS{engine/vecs0/stop_timeout_ms}=="100"
    ATTRS{gt_RP0_freq_mhz}=="1300"
    ATTRS{gt_RP1_freq_mhz}=="400"
    ATTRS{gt_RPn_freq_mhz}=="200"
    ATTRS{gt_act_freq_mhz}=="200"
    ATTRS{gt_boost_freq_mhz}=="1300"
    ATTRS{gt_cur_freq_mhz}=="200"
    ATTRS{gt_max_freq_mhz}=="1300"
    ATTRS{gt_min_freq_mhz}=="200"
    ATTRS{power/control}=="auto"
    ATTRS{power/rc6_enable}=="1"
    ATTRS{power/rc6_residency_ms}=="67956833"
    ATTRS{power/runtime_active_time}=="0"
    ATTRS{power/runtime_status}=="unsupported"
    ATTRS{power/runtime_suspended_time}=="0"

  looking at parent device '/devices/pci0000:00/0000:00:02.0':
    KERNELS=="0000:00:02.0"
    SUBSYSTEMS=="pci"
    DRIVERS=="i915"
    ATTRS{ari_enabled}=="0"
    ATTRS{boot_vga}=="1"
    ATTRS{broken_parity_status}=="0"
    ATTRS{class}=="0x030000"
    ATTRS{consistent_dma_mask_bits}=="40"
    ATTRS{d3cold_allowed}=="1"
    ATTRS{device}=="0x0416"
    ATTRS{dma_mask_bits}=="40"
    ATTRS{driver_override}=="(null)"
    ATTRS{enable}=="1"
    ATTRS{irq}=="34"
    ATTRS{local_cpulist}=="0-7"
    ATTRS{local_cpus}=="ff"
    ATTRS{msi_bus}=="1"
    ATTRS{msi_irqs/34}=="msi"
    ATTRS{numa_node}=="-1"
    ATTRS{power/autosuspend_delay_ms}=="10000"
    ATTRS{power/control}=="on"
    ATTRS{power/runtime_active_time}=="73948827"
    ATTRS{power/runtime_status}=="active"
    ATTRS{power/runtime_suspended_time}=="0"
    ATTRS{remove}=="(write-only)"
    ATTRS{rescan}=="(write-only)"
    ATTRS{reset}=="(write-only)"
    ATTRS{revision}=="0x06"
    ATTRS{subsystem_device}=="0x220e"
    ATTRS{subsystem_vendor}=="0x17aa"
    ATTRS{vendor}=="0x8086"

  looking at parent device '/devices/pci0000:00':
    KERNELS=="pci0000:00"
    SUBSYSTEMS==""
    DRIVERS==""
    ATTRS{power/control}=="auto"
    ATTRS{power/runtime_active_time}=="0"
    ATTRS{power/runtime_status}=="unsupported"
    ATTRS{power/runtime_suspended_time}=="0"

here is the rule (/etc/udev/rules.d)

Note I have tried multiple values in the KERNEL section, "card0" and "card0-DP-1" specifically, but also a bunch of other random stuff

KERNEL=="card0", ACTION=="change", SUBSYSTEM=="drm", RUN+="/usr/bin/dwm_undock"
KERNEL=="card0", ACTION=="remove", SUBSYSTEM=="drm", RUN+="/usr/bin/dwm_undock"

Nothing ever seems to trigger the script. It is worth noting that this script works, because I couldnt get by without it, I use it every single day to manage my monitors with xrandr, if it didnt work... well then I pretty much wouldnt be able to accomplish anything. I even tried replacing the run command with:

RUN+="echo hey1 >> /home/myusername/udevlog"

that also does not trigger.



When I run a test, using the following, it indicates that it should be running the script (I think), I have tried the card0-DP-1 (shown here) as well as removing that leaving only card0. Similar results.

sudo udevadm test --action=change /devices/pci0000:00/0000:00:02.0/drm/card0/card0-DP-1
[sudo] password for myusername:
This program is for debugging only, it does not run any program
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.

Trying to open "/etc/systemd/hwdb/hwdb.bin"...
Trying to open "/etc/udev/hwdb.bin"...
Trying to open "/usr/lib/systemd/hwdb/hwdb.bin"...
Trying to open "/usr/lib/udev/hwdb.bin"...
=== trie on-disk ===
tool version:          250
file size:        11819111 bytes
header size             80 bytes
strings            2415199 bytes
nodes              9403832 bytes
Load module index
Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy
Found container virtualization none.
Loaded timestamp for '/etc/systemd/network'.
Loaded timestamp for '/usr/lib/systemd/network'.
Parsed configuration file /usr/lib/systemd/network/99-default.link
Created link configuration context.
Loaded timestamp for '/etc/udev/rules.d'.
Reading rules file: /usr/lib/udev/rules.d/01-md-raid-creating.rules
Reading rules file: /usr/lib/udev/rules.d/10-dm.rules
Reading rules file: /usr/lib/udev/rules.d/11-dm-lvm.rules
Reading rules file: /usr/lib/udev/rules.d/13-dm-disk.rules
Reading rules file: /usr/lib/udev/rules.d/39-usbmuxd.rules
Reading rules file: /usr/lib/udev/rules.d/40-gphoto.rules
Reading rules file: /usr/lib/udev/rules.d/40-usb-media-players.rules
Reading rules file: /usr/lib/udev/rules.d/40-usb_modeswitch.rules
Reading rules file: /usr/lib/udev/rules.d/45-i2c-tools.rules
Reading rules file: /etc/udev/rules.d/50-monitor-dp-disconnect.rules
Reading rules file: /usr/lib/udev/rules.d/50-trezor.rules
Reading rules file: /usr/lib/udev/rules.d/50-udev-default.rules
Reading rules file: /usr/lib/udev/rules.d/51-android.rules
Reading rules file: /usr/lib/udev/rules.d/60-autosuspend.rules
Reading rules file: /usr/lib/udev/rules.d/60-block.rules
Reading rules file: /usr/lib/udev/rules.d/60-cdrom_id.rules
Reading rules file: /usr/lib/udev/rules.d/60-drm.rules
Reading rules file: /usr/lib/udev/rules.d/60-evdev.rules
Reading rules file: /usr/lib/udev/rules.d/60-fido-id.rules
Reading rules file: /usr/lib/udev/rules.d/60-input-id.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-alsa.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-input.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-storage-tape.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-storage.rules
Reading rules file: /usr/lib/udev/rules.d/60-persistent-v4l.rules
Reading rules file: /usr/lib/udev/rules.d/60-rfkill.rules
Reading rules file: /usr/lib/udev/rules.d/60-sensor.rules
Reading rules file: /usr/lib/udev/rules.d/60-serial.rules
Reading rules file: /usr/lib/udev/rules.d/60-tpm-udev.rules
Reading rules file: /usr/lib/udev/rules.d/60-vboxdrv.rules
Reading rules file: /usr/lib/udev/rules.d/61-kde-bluetooth-rfkill.rules
Reading rules file: /usr/lib/udev/rules.d/63-md-raid-arrays.rules
Reading rules file: /usr/lib/udev/rules.d/64-btrfs-dm.rules
Reading rules file: /usr/lib/udev/rules.d/64-btrfs-zoned.rules
Reading rules file: /usr/lib/udev/rules.d/64-btrfs.rules
Reading rules file: /usr/lib/udev/rules.d/64-md-raid-assembly.rules
Reading rules file: /usr/lib/udev/rules.d/65-libwacom.rules
Reading rules file: /usr/lib/udev/rules.d/65-sane.rules
Reading rules file: /usr/lib/udev/rules.d/66-saned.rules
Reading rules file: /usr/lib/udev/rules.d/69-dm-lvm.rules
Reading rules file: /usr/lib/udev/rules.d/69-libftdi.rules
Reading rules file: /usr/lib/udev/rules.d/69-libmtp.rules
Reading rules file: /usr/lib/udev/rules.d/69-md-clustered-confirm-device.rules
Reading rules file: /usr/lib/udev/rules.d/70-camera.rules
Reading rules file: /usr/lib/udev/rules.d/70-infrared.rules
Reading rules file: /usr/lib/udev/rules.d/70-joystick.rules
Reading rules file: /usr/lib/udev/rules.d/70-memory.rules
Reading rules file: /usr/lib/udev/rules.d/70-mouse.rules
Reading rules file: /usr/lib/udev/rules.d/70-power-switch.rules
Reading rules file: /usr/lib/udev/rules.d/70-touchpad.rules
Reading rules file: /usr/lib/udev/rules.d/70-uaccess.rules
Reading rules file: /usr/lib/udev/rules.d/71-seat.rules
Reading rules file: /usr/lib/udev/rules.d/73-seat-late.rules
Reading rules file: /usr/lib/udev/rules.d/75-net-description.rules
Reading rules file: /usr/lib/udev/rules.d/75-probe_mtd.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-broadmobi-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-cinterion-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-dell-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-dlink-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-ericsson-mbm.rules
Configuration file /usr/lib/udev/rules.d/77-mm-fibocom-port-types.rules is marked executable. Please remove executable permission bits. Proceeding anyway.
Reading rules file: /usr/lib/udev/rules.d/77-mm-fibocom-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-foxconn-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-gosuncn-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-haier-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-huawei-net-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-longcheer-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-mtk-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-nokia-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-quectel-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-sierra.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-simtech-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-telit-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-tplink-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-ublox-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-x22x-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/77-mm-zte-port-types.rules
Reading rules file: /usr/lib/udev/rules.d/78-sound-card.rules
Reading rules file: /usr/lib/udev/rules.d/80-drivers.rules
Reading rules file: /usr/lib/udev/rules.d/80-libinput-device-groups.rules
Reading rules file: /usr/lib/udev/rules.d/80-mm-candidate.rules
Reading rules file: /usr/lib/udev/rules.d/80-net-setup-link.rules
Reading rules file: /usr/lib/udev/rules.d/80-udisks2.rules
Reading rules file: /usr/lib/udev/rules.d/81-net-dhcp.rules
Reading rules file: /usr/lib/udev/rules.d/84-nm-drivers.rules
Reading rules file: /usr/lib/udev/rules.d/85-nm-unmanaged.rules
Reading rules file: /usr/lib/udev/rules.d/85-regulatory.rules
Reading rules file: /usr/lib/udev/rules.d/85-tlp.rules
Reading rules file: /usr/lib/udev/rules.d/90-alsa-restore.rules
Reading rules file: /usr/lib/udev/rules.d/90-daxctl-device.rules
Reading rules file: /usr/lib/udev/rules.d/90-libinput-fuzz-override.rules
Reading rules file: /usr/lib/udev/rules.d/90-nm-thunderbolt.rules
Reading rules file: /usr/lib/udev/rules.d/90-pipewire-alsa.rules
Reading rules file: /usr/lib/udev/rules.d/90-pulseaudio.rules
Reading rules file: /usr/lib/udev/rules.d/90-udisks2-zram.rules
Reading rules file: /usr/lib/udev/rules.d/90-vconsole.rules
Reading rules file: /usr/lib/udev/rules.d/95-dm-notify.rules
Reading rules file: /usr/lib/udev/rules.d/95-upower-hid.rules
Reading rules file: /usr/lib/udev/rules.d/95-upower-wup.rules
Reading rules file: /usr/lib/udev/rules.d/96-e2scrub.rules
Reading rules file: /usr/lib/udev/rules.d/99-fuse.rules
Reading rules file: /usr/lib/udev/rules.d/99-fuse3.rules
Reading rules file: /etc/udev/rules.d/99-monitor-end.rules
Reading rules file: /usr/lib/udev/rules.d/99-systemd.rules
Reading rules file: /etc/udev/rules.d/99-usb-rules.rules
card0-DP-1: /usr/lib/udev/rules.d/60-drm.rules:3 Importing properties from results of builtin command 'path_id'
card0-DP-1: sd-device: Created db file '/run/udev/data/+drm:card0-DP-1' for '/devices/pci0000:00/0000:00:02.0/drm/card0/card0-DP-1'
DEVPATH=/devices/pci0000:00/0000:00:02.0/drm/card0/card0-DP-1
ACTION=change
SUBSYSTEM=drm
ID_PATH=pci-0000:00:02.0
ID_PATH_TAG=pci-0000_00_02_0
TAGS=:seat:master-of-seat:
CURRENT_TAGS=:seat:master-of-seat:
ID_FOR_SEAT=drm-pci-0000_00_02_0
USEC_INITIALIZED=74246401047
Unload module index
Unloaded link configuration context.

I have also reloaded the rules and restarted my computer just as a sanity check. Im not sure what is wrong here. I have made other udev rules before but I dont know what I am doing wrong here.  To me, it seems that when I unplug my mini dp cable from my laptop, it should trigger as per the monitor result

Offline

#2 2022-05-01 13:48:47

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 16,186

Re: Udev rule not working/triggering. Run script when monitor is unplugged

For the simple case of wanting to run a test command, you are assuming the RUN clause is a shell which it isn't so constructs like >> have no meaning, the correct use would be

RUN+="/usr/bin/sh -c 'echo hey1 >> /home/myusername/udevlog'"

As for the script, you might want to post it and make sure you're setting DISPLAY and XAUTHORITY envs properly as a udev rule runs as root and outside of your normal session's scope, see https://wiki.archlinux.org/title/Udev#S … mouse_plug for how that could look like. Might also have to add a few secs of sleeps before doing any xrandr operation, as chances are udev triggers the script and it's rules before xorg itself is aware of them.

Last edited by V1del (2022-05-01 13:50:48)

Offline

#3 2022-05-01 16:44:24

blah123213
Member
Registered: 2022-04-28
Posts: 5

Re: Udev rule not working/triggering. Run script when monitor is unplugged

V1del wrote:

For the simple case of wanting to run a test command, you are assuming the RUN clause is a shell which it isn't so constructs like >> have no meaning, the correct use would be

RUN+="/usr/bin/sh -c 'echo hey1 >> /home/myusername/udevlog'"

As for the script, you might want to post it and make sure you're setting DISPLAY and XAUTHORITY envs properly as a udev rule runs as root and outside of your normal session's scope, see https://wiki.archlinux.org/title/Udev#S … mouse_plug for how that could look like. Might also have to add a few secs of sleeps before doing any xrandr operation, as chances are udev triggers the script and it's rules before xorg itself is aware of them.

I was able to get output based on what you gave.

One of the choices I made after posting this is to shift from running a script to running a systemd unit file instead. The reason being is that I had suspicion that the script was being killed by udev. In the man pages it has this ominous section about scripts that shouldn't run too long. What is "too long" you say? I have no idea. my script didnt even last half a second but who knows.

However.... The unit didnt trigger either, even if I was able to run the unit file on my own and see that everything was working. So, I ended up doing something that I know is not completely efficient... but it works for right now, and I am looking for feedback on it.

What I will be changing:

Moving the display info to the systemd unit itself
Trying to get the unit file to be called the normal way by udev + systemd

But anyway, here is what I got, and as of right now it works:


I doubt I need

/usr/bin/sh -c

but I was getting annoyed so I just left it there out of desperation lol

It is likely superfluous

KERNEL=="card0", ACTION=="change", SUBSYSTEM=="drm", RUN+="/usr/bin/sh -c 'systemctl start monitorexe'"

here is the current script I am using. Note: this is a rewrite of my script that I was using when I first made this post. Reason being is that my old script used a lot of command1 & command2 kind of logic, which apparently udev doesnt like. I guess it is somewhat of a multithreaded approach? I didnt know that when originally doing this though.


As you can see here with the xrandr commands for exporting display 0 and dropping root sith su myuser -c, I am side stepping the usual way things are done by setting the xauthority and the display in the unit file itself. I will eventually change this but if anyone feels like posting the correct way, ill probably just copy what you post when I update this. I know ive seen the correct way somewhere on the net, but I need to google it again.

##!/bin/bash


LOGFILE="/home/myusername/ulog/udevmonitors"

echo >> "$LOGFILE"
date >> "$LOGFILE"


centermon=$(cat /sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-DP-1/status)
rightmon=$(cat /sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-DP-2/status)

#define monitors for xrandr
center=DP-1
right=DP-2-2
left=eDP-1

#define resolutions
center_res=3840x2160
left_res=1920x1080

echo "begin ifs" >> "$LOGFILE"

#/usr/bin/xrandr -v

if [[ "connected" == "$centermon" ]];then
 su myusername -c "export DISPLAY=:0;/usr/bin/xrandr --output $center --primary --auto --mode $center_res >> $LOGFILE 2>&1"
 su myusername -c "export DISPLAY=:0;/usr/bin/xrandr --output $left --auto --left-of $center --mode $left_res >> $LOGFILE 2>&1"
 su myusername -c "export DISPLAY=:0;/usr/bin/xrandr --output $right --auto  --right-of $center --mode $center_res >> $LOGFILE 2>&1"
elif [[ "disconnected" == "$centermon" ]];then
 echo "begin disconnected" >> "$LOGFILE"
 su myusername -c "export DISPLAY=:0;/usr/bin/xrandr --output $center --off >> $LOGFILE 2>&1"
 su myusername -c "export DISPLAY=:0;/usr/bin/xrandr --output $right --off >> $LOGFILE 2>&1"
 su myusername -c "export DISPLAY=:0;/usr/bin/xrandr --output $left --auto --primary --mode $left_res >> $LOGFILE 2>&1"
 echo "end disconnected" >> "$LOGFILE"
fi

su myusername -c "export DISPLAY=:0; /usr/bin/nitrogen --restore >> $LOGFILE 2>&1"

echo "end" >> "$LOGFILE"

here is the systemd unit file. I doubt the bash thing is needed

[Service]
Type=oneshot
ExecStart=/bin/bash /usr/bin/bmonrule

all in all, this was just a sloppy experiment that I eventually got to work, I know there are a lot of areas where this needs to be better, but for now I am just gonna let it be. If anyone else feels like correcting the obvious stuff, you may save myself and other people some research time.

Thanks!

Offline

Board footer

Powered by FluxBB