See SOLUTION at the end.
I have used this guide to write a script that activates when I open or close the laptop lid. My setup is a laptop monitor and an external monitor working well together.
Basically (everything works fine the first 3 steps, the confusion starts on step 4):
1) I have created the ~/export_x_info file about permission issues and running it on startup containing:
# Export the dbus session address on startup so it can be used by any other environment
sleep 5
touch $HOME/.Xdbus
chmod 600 $HOME/.Xdbus
env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.Xdbus
echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.Xdbus
# Export XAUTHORITY value on startup so it can be used by cron
env | grep XAUTHORITY >> $HOME/.Xdbus
echo 'export XAUTHORITY' >> $HOME/.Xdbus
2) I created /etc/acpi/lid.sh pointing to the actual script on my home directory containing these two lines:
#!/bin/bash
/home/myname/scripts/lid_event
3) On the lid_event I put the following:
grep -q closed /proc/acpi/button/lid/LID/state
if [ $? = 0 ]
then
/home/myname/scripts/close;
else
/home/myname/scripts/open;
fi
Until now everything is as expected. I have checked the "/proc/acpi/button/lid/LID/state" file and changes depending on lid being open or closed. All files I have created are executable so there is no problem there.
4) On my "close" script I have this ("open" script is similar so no need to mention it):
#This runs so that root can run the following command under the user's environment
source /home/myname/.Xdbus
#When laptop lid closes, close laptop screen.
DISPLAY=:0.0 su myname -c xrandr --output eDP1 --off
When I close the lid I want the laptop screen to close and have only the VGA screen up. The above command works great when manually executing it at the terminal or manually executing this script but when it's about ACPI it does not execute!
Now, the weird thing is if I put a simple command like "touch ~/file" on the above script, it executes perfectly!
It seems to be a specific problem with xrandr. I suspect that there might be some permission issues unresolved but I cannot find a solution.