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.


grep -F '(:0)'should begrep -F '(:0.0)'; also try sticking aDISPLAY=:0.0in front of/usr/bin/think-dock– izx Aug 28 '12 at 20:38DISPLAYseems good. Mywhodisplays(:0)although. So I do not think that grep would be better that way. – queueoverflow Aug 29 '12 at 19:38DISPLAYin front ofbash, it seems to work now. Thanks! – queueoverflow Aug 29 '12 at 19:50whohad0.0, but anyway. Since it worked, I'll add theDISPLAYpart as an answer; please accept it. Thanks! – izx Aug 29 '12 at 23:38