Udev rule not running

I have a script that I want to run when I add a monitor. The script, monitor-rescale has the following contents:


xrandr=$(xrandr)

con_monitors=$(xrandr | grep -c " connected ")

if [[ $con_monitors -gt 1 ]]; then
    gsettings set org.gnome.desktop.interface text-scaling-factor 2
    gsettings --schemadir ~/.local/share/gnome-shell/extensions/dash-to-dock@micxgx.gmail.com/schemas/ set org.gnome.shell.extensions.dash-to-dock dash-max-icon-size 74

fi

if [[ $con_monitors -eq 1 ]]; then
     gsettings set org.gnome.desktop.interface text-scaling-factor 1.4
     gsettings --schemadir ~/.local/share/gnome-shell/extensions/dash-to-dock@micxgx.gmail.com/schemas/ set org.gnome.shell.extensions.dash-to-dock dash-max-icon-size 43

 fi

It has the necessary permissions. More specifically -rwxr-xr-x. 1 spurz spurz 637 Nov 6 17:59 monitor-scale.

I have created a udev rule in the following file /etc/udev/rules.d/99-monitor-hotplug.rules and the contents of that file are:

SUBSYSTEM=="drm", ACTION=="change", RUN+="/home/spurz/Documents/configs/monitor-scale"

I have run sudo udevadm control --reload after adding the udev rule but nothing happens when I unplug the monitor still.

The command udevadm monitor does register a monitor addition when I add the monitor(or when I remove it).
Some of the output when I add is:

KERNEL[87678.851509] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)
KERNEL[87678.871688] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:2.0 (usb)
KERNEL[87678.871723] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)
UDEV  [87678.874081] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)
UDEV  [87678.875158] add      /devices/pci0000:00/0000:00:14.0/usb1/1-1/1-1:2.0 (usb)
UDEV  [87678.876260] bind     /devices/pci0000:00/0000:00:14.0/usb1/1-1 (usb)
KERNEL[87679.003750] change   /devices/platform/USBC000:00/typec/port0 (typec)
KERNEL[87679.003775] add      /devices/platform/USBC000:00/typec/port0/port0-partner (typec)
KERNEL[87679.003922] change   /devices/platform/USBC000:00/power_supply/ucsi-source-psy-USBC000:001 (power_supply)
UDEV  [87679.004754] change   /devices/platform/USBC000:00/typec/port0 (typec)
UDEV  [87679.005514] change   /devices/platform/USBC000:00/power_supply/ucsi-source-psy-USBC000:001 (power_supply)
UDEV  [87679.005772] add      /devices/platform/USBC000:00/typec/port0/port0-partner (typec)
KERNEL[87679.841696] change   /devices/platform/USBC000:00/power_supply/ucsi-source-psy-USBC000:001 (power_supply)
UDEV  [87679.842789] change   /devices/platform/USBC000:00/power_supply/ucsi-source-psy-USBC000:001 (power_supply)
KERNEL[87680.054427] add      /devices/platform/USBC000:00/typec/port0/port0-partner/port0-partner.0 (typec)
KERNEL[87680.054451] bind     /devices/platform/USBC000:00/typec/port0/port0-partner/port0-partner.0 (typec)
UDEV  [87680.055271] add      /devices/platform/USBC000:00/typec/port0/port0-partner/port0-partner.0 (typec)
UDEV  [87680.055801] bind     /devices/platform/USBC000:00/typec/port0/port0-partner/port0-partner.0 (typec)
KERNEL[87680.288944] change   /devices/platform/USBC000:00/typec/port0/port0-partner (typec)
UDEV  [87680.289588] change   /devices/platform/USBC000:00/typec/port0/port0-partner (typec)
KERNEL[87680.452997] change   /devices/pci0000:00/0000:00:02.0/drm/card1 (drm)
UDEV  [87680.460055] change   /devices/pci0000:00/0000:00:02.0/drm/card1 (drm)
KERNEL[87680.461434] change   /devices/platform/USBC000:00/typec/port0/port0-partner/port0-partner.0 (typec)
KERNEL[87680.461451] add      /devices/virtual/usb_power_delivery/pd1 (usb_power_delivery)
UDEV  [87680.462228] change   /devices/platform/USBC000:00/typec/port0/port0-partner/port0-partner.0 (typec)
UDEV  [87680.462275] add      /devices/virtual/usb_power_delivery/pd1 (usb_power_delivery)
KERNEL[87681.455470] change   /devices/platform/USBC000:00/power_supply/ucsi-source-psy-USBC000:001 (power_supply)
KERNEL[87681.455512] change   /devices/platform/USBC000:00/typec/port0/port0-partner (typec)
UDEV  [87681.455962] change   /devices/platform/USBC000:00/power_supply/ucsi-source-psy-USBC000:001 (power_supply)

Is there something wrong with my udev rule?

I’m not sure about the udev rule, but it looks like it should work.

The script, however, has some problems. First, it has no shebang (#!/bin/bash), so the kernel doesn’t know how to execute it. More significantly, udev is going to run it as root and outside your session.

Also, I don’t think this line is doing anything useful:

xrandr=$(xrandr)

I’d make a systemd user service and start that from the udev rule:

RUN+="/usr/bin/systemctl --user -M spurz@ start monitor-scale.service"
[Unit]
Description=Monitor scale adjustment

[Service]
Type=oneshot
ExecStart=/home/spurz/Documents/configs/monitor-scale

Save that as ~/.config/systemd/user/monitor-scale.service

If you are running Xort ant not Wayland you create the file /etc/X11/xorg.conf.d/50-mode-1280x720.conf

# Set default screen resolution.

Section "Screen"
    Identifier    "Default Screen"
    SubSection    "Display"
        Modes    "1280x720"
    EndSubSection
EndSection

You can also try to add video=1280x720 to the kernel command line, but seems to be mostly ignored in graphical environment.

Otherwise Gnome and other desktop environment has settings for this.