I have the following rule call my script when I dock or un-dock my ThinkPad:
# /lib/udev/rules.d/81-thinkpad-dock.rules
KERNEL=="dock.0", ATTR{docked}=="0", RUN+="/usr/bin/think-dock-hook off"
KERNEL=="dock.0", ATTR{docked}=="1", RUN+="/usr/bin/think-dock-hook on"
That is the script that will be called:
# /usr/bin/think-dock-hook
# Find the user who is currently logged in on the primary screen.
user="$(who -u | grep -F '(:0)' | head -n 1 | awk '{print $1}')"
su -c "bash -x /usr/bin/think-dock $setto" "$user" >> /root/think-dock.log 2>&1 &
And the script that is then called does something with xrandr
.
The thing is that I can run think-dock on
as my user (mu
) and it works. I can sudo -i
and run think-dock-hook on
and it works too. But when I let udev
run it, it just get the following error from xrandr
:
# output of bash -x think-dock on
+ xrandr --output LVDS1 --auto
Can't open display
Now if I call xrandr
from my sudo -i
shell, I get:
No protocol specified
No protocol specified
Can't open display :0
However, if I do su -c xrandr mu
from my sudo -i
shell, I get the expected output.
So I do not really understand, the script called from udev
fails.
DISPLAY
environment variableroot
(which udev runs as) has no defaultDISPLAY
set; even if he/she did,su -c
does not preserve the environment by defaultSo pass it along explicitly to bash, and that should solve your problem, e.g.: