My HDMI udev rule is not working

,

I’m using the i3 community edition btw. I have this udev rule.

SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/local/bin/checkHDMI.sh"

Which doesn’t seem to be working. I want it to run the bash script, which sets up the monitor correctly with scaling. I don’t see any change in the monitor setup when I plug or unplug

here is the checkHDMI.sh

#!/bin/sh
hdmi_active=$(xrandr |grep ' connected' |grep 'HDMI' |awk '{print $1}')

if [[ $hdmi_active = "HDMI1" ]]
then
  # xrandr --output eDP1 --primary --mode 1920x1080 --pos 1920x0 --rotate inverted --output HDMI1 --mode 1920x1080 --pos 0x0 --rotate normal --output VIRTUAL1 --off
  xrandr \
  --fb 4320x1350 \
  --output HDMI1 --mode 1920x1080 --pos 0x0 --rotate normal --primary --scale 1.25x1.25 \
  --output eDP1 --mode 1920x1080 --pos 2400x0 --scale 1x1 --rotate inverted

else
  xrandr --output eDP1 --primary --mode 1920x1080 --pos 0x0 --rotate normal --output HDMI1 --off --output VIRTUAL1 --off
fi

I ran udevadm monitor to get the device info. Here is the output.

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

KERNEL[4690.281360] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV  [4690.297418] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
KERNEL[4695.429264] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV  [4695.447129] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)

I don’t see it working at all. I don’t even know how to use udevadm test either I don’t know what I tried worked

does it show anything when you run the script in terminal.
you did make the script executable(sudo chmod +x checkHDMI.sh)

From my very little bash scripting, could you adjust your script to the following and test again if this script is working at all?

#!/usr/bin/env bash

...
if [ "$hdmi_active" = "HDMI1" ];
then
...

Edit: I just tested only the script and apparently you don’t have to change it and your script should already work, right? However, the [[ are not valid POSIX-sh and should be omitted.

Another edit:
My guess is that this script is not run as your current user but as root in an environment where xrandr doesn’t run because the DISPLAY variable is unset or it can’t access the xserver.

The script works generally when I have it in my home directory under a folder called userScripts but it doesn’t seem to work when I use it udev

I have put it in a root directory /usr/local/bin so that the udev rule can run it. Any ideas on running it otherwise. If I cd into /usr/local/bin and run su which gives me root user access. I then run it as root user directly and not using sudo it still seems to work

How do you execute it in your home directory?

And the RUN+= path in your udev rule doesn’t look like it points to your home directory, so perhaps you should change it accordingly:

SUBSYSTEM=="drm", ACTION=="change", RUN+="~/userScripts/checkHDMI.sh"

This would not help. The problem is that when udev is running the script, it can’t connect to X.

This might help: i3, udev, & xrandr: Hotplugging & Output Switching
Specifically, give the environment variables:

KERNEL=="card0", SUBSYSTEM=="drm", ACTION=="change", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/ben/.Xauthority", RUN+="/bin/bash /path/to/script.sh"

(Please adjust to your user and possible Manjaro/Arch specifics.)