You are not logged in.

#1 2024-02-19 14:37:40

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

udev rule for external monitor has stopped working suddenly

I'm at my wits end on this one. It was working for years and then it suddenly stopped. I have been trying to get it to work by trying to make my script, that uses xrandr to be quicker, because i know udev will kill scripts if it decides it is taking too long but that isnt working

a few things

1. If I run my systemd unit file, or the script manually, it works.
2. if I edit the udev rule to not call the script or systemd file, replacing it with a /usr/bin/touch /home/joe/ulog/testfile, the test file does appear

this is why im thinking this must be a timeout issue, and admittedly my script is not particularly nice but it worked for a long time so i just kept it around.

if I run the following I do not see my script show up (let me know if you want to see the log, its just a lot of unrelated stuff)

sudo udevadm control --log-priority=debug
sudo journalctl -f

i also tried 'err' instead of debug

here is the udev rule:

50-monitor-dp-disconnect.rules

 KERNEL="card1", ACTION=="change", SUBSYSTEM=="drm", RUN+="/usr/bin/systemctl start monitorexe"

here is the systemd unit file:
/etc/systemd/system/monitorexe.service

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

(remember if I run this manually itll work)

here is the script, I commented a lot out because im getting desperate

#!/bin/bash


LOGFILE="/home/joe/ulog/udevmonitorxs"

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


#centermon=$(cat /sys/devices/pci0000:00/0000:00:02.0/drm/card1/card1-DP-1/status)
#rightmon=$(cat /sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-DP-2/status)
#dpms=$(xset q | grep "DPMS is Enabled")
#echo $1 >> "$LOGFILE"
#dpmsmsg="DPMS is Enabled"
#if [[ "$dpms" != *$dpmsmsg* ]]; then
#    echo "dpms killed script" >> "$LOGFILE"
#    exit 0
#fi

#define monitors for xrandr
center=DP1
right=DP2-2
left=eDP1

#define resolutions
center_res=3840x2160
left_res=1920x1080

#echo "begin ifs" >> "$LOGFILE"

#/usr/bin/xrandr -v

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

su joe -c 'export DISPLAY=:0;export PULSE_RUNTIME_PATH="/run/user/$(id -u)/pulse/"; setsinkbyname.py hdmi'
su joe -c "export DISPLAY=:0; /usr/bin/nitrogen --restore"

echo "end" >> "$LOGFILE"

here is a cleaner version of the script that removes all the comments because the above may be difficult to read

#!/bin/bash


LOGFILE="/home/joe/ulog/udevmonitorxs"


date >> "$LOGFILE"


center=DP1
right=DP2-2
left=eDP1

center_res=3840x2160
left_res=1920x1080


 su joe -c "export DISPLAY=:0;/usr/bin/xrandr --output $center --primary --auto --mode $center_res"
 su joe -c "export DISPLAY=:0;/usr/bin/xrandr --output $left --auto --left-of $center --mode $left_res"





su joe -c 'export DISPLAY=:0;export PULSE_RUNTIME_PATH="/run/user/$(id -u)/pulse/"; setsinkbyname.py hdmi'
su joe -c "export DISPLAY=:0; /usr/bin/nitrogen --restore"

echo "end" >> "$LOGFILE"

here is the output of the systemd status (it doesnt trigger unless I manually do it, so the timestamp is probably not relevant)

systemctl status monitorexe
○ monitorexe.service
     Loaded: loaded (/etc/systemd/system/monitorexe.service; static)
     Active: inactive (dead)

Feb 19 08:45:11 t440pArch su[885228]: pam_unix(su:session): session closed for user joe
Feb 19 08:45:11 t440pArch su[885236]: (to joe) root on none
Feb 19 08:45:11 t440pArch su[885236]: pam_unix(su:session): session opened for user joe(uid=1000) by (uid=0)
Feb 19 08:45:11 t440pArch nitrogen[885238]: Attempting to freeze the notification queue for object GtkSettings[0x595966f289f0]; Property notification does not w>
Feb 19 08:45:11 t440pArch nitrogen[885238]: Attempting to thaw the notification queue for object GtkSettings[0x595966f289f0]; Property notification does not wor>
Feb 19 08:45:11 t440pArch nitrogen[885238]: Attempting to freeze the notification queue for object GtkSettings[0x595966f15920]; Property notification does not w>
Feb 19 08:45:11 t440pArch nitrogen[885238]: Attempting to thaw the notification queue for object GtkSettings[0x595966f15920]; Property notification does not wor>
Feb 19 08:45:11 t440pArch su[885236]: pam_unix(su:session): session closed for user joe
Feb 19 08:45:11 t440pArch systemd[1]: monitorexe.service: Deactivated successfully.
Feb 19 08:45:11 t440pArch systemd[1]: Finished monitorexe.service.

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[40728.612715] change   /devices/pci0000:00/0000:00:02.0/drm/card1 (drm)
UDEV  [40728.615086] change   /devices/pci0000:00/0000:00:02.0/drm/card1 (drm)
KERNEL[40733.142862] change   /devices/pci0000:00/0000:00:02.0/drm/card1 (drm)
UDEV  [40733.147426] change   /devices/pci0000:00/0000:00:02.0/drm/card1 (drm)
^C%                                                                            

kernel info:

uname --all
Linux t440pArch 6.6.17-1-lts #1 SMP PREEMPT_DYNAMIC Sat, 17 Feb 2024 08:28:50 +0000 x86_64 GNU/Linux

I'm open to trying anything because I just need this to work again. If anyone has any ideas let me know.

Last edited by blah123213 (2024-02-19 14:43:01)

Offline

#2 2024-02-19 15:10:12

seth
Member
Registered: 2012-09-03
Posts: 51,867

Online

#3 2024-02-19 15:25:45

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

Re: udev rule for external monitor has stopped working suddenly

I just tried this, I think this may actually work for me because since I am using dwm, I have an autostart script that this can run with upon starting x.

However, are there any drawbacks to using this? Is udev more efficient with anything, specifically battery or anything like that?

Any recommendations with this to make it match best practices? My initial tests with this are certainly positive fwiw

Offline

#4 2024-02-19 15:34:17

seth
Member
Registered: 2012-09-03
Posts: 51,867

Re: udev rule for external monitor has stopped working suddenly

You've to run it as a user daemon, but you can run it as a user daemon and don't have to hardcode or track an environment in a system daemon, su to the proper user and hope that xrandr finishes early enough.
You're just running it in the adequate environment automatically cool

The only downside is that it won't work w/ wayland, but neither does xrandr wink

Wrt to best practices: "depends" what you consider that to be.
You could write a systemd user service, and an X11 target for that and then activate the X11 target w/ your xinit script so that systemd will start the user services depending on it and that's the systemd best practice.
Or you can just add what you want to start to your startup script and completely sidetrack systemd and that's probably the least convoluted and fragile practice lol

Online

Board footer

Powered by FluxBB