You are not logged in.

#1 2017-03-20 22:54:42

coinpouch
Member
Registered: 2017-03-20
Posts: 10

Monitor udev rule not working - event seems correct, script works too

Hi everyone (first post here)!

I was trying to get my monitor/s between home/work/etc. to automatically set up based on a script that runs when a monitor plug/unplug (change) event is detected. The script itself works (when not including the initial if block with the forking) when I run it manually after dis/connecting, but the rule does not seem to.

The script was copied from https://tyler.vc/auto-monitor-detection … -with-udev

#!/usr/bin/bash

# udev will wait for our script to finish before the monitor is available
# for use, so we will use the `at` command to run our command again as
# another user:
if [ "$1" != "forked" ]; then
    echo "$(dirname $0)/$(basename $0) forked" | at now
    exit
fi

# udev runs as root, so we need to tell it how to connect to the X server:
export DISPLAY=:0
export XAUTHORITY=/home/tylermenezes/.Xauthority

# Find out the device path to our graphics card:
cardPath=/sys/$(udevadm info -q path -n /dev/dri/card0)

# Detect if the monitor is connected and, if so, the monitor's ID:
conHdmi=$(xrandr | sed -n '/HDMI1 connected/p')
shaHdmi=$(sha1sum $cardPath/card0-HDMI-A-1/edid | cut -f1 -d " ")

# The useful part: check what the connection status is, and run some other commands
if [ -n "$conHdmi" ]; then
    if [ "$shaHdmi" = "xxxxxxxxxxxxxxxx" ]; then    # Office PC
        xrandr --output eDP1 --auto --output HDMI1 --auto --right-of eDP1
    else                                            # Probably a projector
        xrandr --output eDP1 --auto --output HDMI1 --auto --same-as eDP1
    fi
else
    xrandr --output eDP1 --auto --output HDMI1 --off
fi

Here's the single line in my /etc/udev/rules.d/95-monitors.rules file:

ACTION=="change", SUBSYSTEM=="drm", RUN+="bash /home/<username>/<irrelevant path>/display-auto.sh"

Running:

sudo udevadm test /sys/devices/pci0000:00/0000:00:02.0/drm/card0
Lists the aforementioned rules file as one of the rules files being read.

Running udevadm monitor then unplugging/replugging a monitor gives the following:
Unplugging:

KERNEL[29107.090839] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV  [29107.095254] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)

Replugging:

KERNEL[29110.196800] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV  [29110.200417] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)

I've also had many shutdowns/restarts since I first tried getting the rule working, so that hasn't helped either.

I'm not too sure how to debug the issue as I've also looked through similar threads on the forum/elsewhere of people who had problems getting their udev rule working, and any proposed solutions in those cases didn't seem to have worked for me.

Any pointers on what I should try/look into to resolve the issue would be greatly appreciated!

Last edited by coinpouch (2017-03-21 00:42:40)

Offline

#2 2017-03-21 00:00:32

damjan
Member
Registered: 2006-05-30
Posts: 462

Re: Monitor udev rule not working - event seems correct, script works too

what does the script do?
it will run in the context of udev, and that's far from the context your X apps run for example.

Offline

#3 2017-03-21 00:41:31

coinpouch
Member
Registered: 2017-03-20
Posts: 10

Re: Monitor udev rule not working - event seems correct, script works too

That makes sense - I've modified the original post to show the script being used, but for reference, it was the one at this link: https://tyler.vc/auto-monitor-detection … -with-udev.

Offline

Board footer

Powered by FluxBB