You are not logged in.
Hello, so I've been trying to create a udev rule such that when I connect an HDMI cable to my thinkpad laptop (amd, internal gpu), the laptop screen will turn off and the external monitor will be the primary display. This udev rule will then execute a script to achieve this.
Here is the udev rule /etc/udev/rules.d/99-monitor-hotplug.rules
ACTION=="change", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", RUN+="/home/archi/.scripts/monitor-hotplug.sh"and here is the monitor-hotplug.sh script:
#!/bin/bash
sleep 3
status=$(cat /sys/class/drm/card0/*HDMI*/status)
if [ "${status}" = "connected" ]; then
xrandr --output HDMI-A-0 --auto --primary --output eDP --off
else
xrandr --output eDP --auto --primary --output HDMI-A-0 --off
fiMy laptop monitor is named `eDP` and my external monitor connected by hdmi is `HDMI-A-0`. I've also restarted `systemd-udevd.service`, and tried rebooting the laptop several times.
However, this is not working as expected:
1. The rule is not enforced when I restarted the computer, such that I need to run `sudo systemctl restart systemd-udevd.service` in order to get the rule working.
2. The script doesn't work as expected: When I first connect the hdmi cable -> nothing happens in both screens. When I disconnect the hdmi cable -> the laptop screen turns off! when I reconnect the hdmi cable it works again.
Can somebody help me? Thank you!!
Last edited by t0mby (2023-02-23 12:45:52)
Offline
If you're using X11 anyway, https://aur.archlinux.org/packages/x-on-resize
The rule acts on *change* and a change is probably not happening during the boot.
You're also not supposed to run lasting jobs via udev rules (no "sleep")
Offline
If you're using X11 anyway, https://aur.archlinux.org/packages/x-on-resize
The rule acts on *change* and a change is probably not happening during the boot.
You're also not supposed to run lasting jobs via udev rules (no "sleep")
Thank you very much for your suggestion! I've installed x-on-resize, but don't understand how it works... the man page was also not very clear to me.
Regarding your last point, instead of udev rules, would it be more appropriate to use systemd services instead for example?
Thank you once again.
Offline
x-on-resize -c /path/to/scriptThe script can eg. parse "xrandr -q" and act accordingly.
Offline
x-on-resize -c /path/to/scriptThe script can eg. parse "xrandr -q" and act accordingly.
I see. I've tried that command with my script (the one in the OP), but it gives me an error each time I connect/disconnect the hdmi cable:
sh: line 1: monitor-hotplug.sh: command not foundI've also tried to create another script file, with just a simple `echo` or nothing at all, and the error is still the same. I've chmod both files.
Do you know what could be wrong?
Last edited by t0mby (2023-02-23 14:21:41)
Offline
Please post how you exactly invoke x-on-resize.
Offline
Please post how you exactly invoke x-on-resize.
Like this:
x-on-resize -c /home/archi/.scripts/monitor-hotplug.sh The monitor-hotplug.sh script is the one in the OP.
If it helps, I installed x-on-resize with yay, but it works fine I think. When i run
x-on-resize -v it prints x-on-resize v 0.2
Last edited by t0mby (2023-02-23 14:38:31)
Offline
Then why would it say "monitor-hotplug.sh: command not found" w/o the complete path?
And why as "command not found"?
stat /bin/shOffline
Then why would it say "monitor-hotplug.sh: command not found" w/o the complete path?
And why as "command not found"?stat /bin/sh
Okay, now it keeps printing "config" each time I run the x-on-resize command... so strange. But nothing happens in both screens.
Offline
That means you're not passing a value to "-c" at all.
echo $SHELL
stat /bin/shLast guess (never tried): use a script w/o a dash "monitor_hotplug.sh"
Offline
That means you're not passing a value to "-c" at all.
echo $SHELL stat /bin/shLast guess (never tried): use a script w/o a dash "monitor_hotplug.sh"
Perfect! working like a charm now! Just some problems with resolution since my external monitor is 4k an my screen is 1080p, but it already detects and changes accordingly.
Offline