Based on this thread I modified my /etc/acpi/powerbtn.sh script to account for LXDE sessions:
# getXuser gets the X user belonging to the display in $displaynum.
# If you want the foreground X user, use getXconsole!
getXuser() {
user=`pinky -fw | awk '{ if ($2 == ":'$displaynum'" || $(NF) == ":'$displaynum'" ) { print $1; exit; } }'`
if [ x"$user" = x"" ]; then
startx=`pgrep -n startx`
if [ x"$startx" != x"" ]; then
user=`ps -o user --no-headers $startx`
fi
fi
if [ x"$user" != x"" ]; then
userhome=`getent passwd $user | cut -d: -f6`
export XAUTHORITY=$userhome/.Xauthority
else
export XAUTHORITY=""
fi
export XUSER=$user
}
if [ -n $(pidof lxsession) ]; then
for x in /tmp/.X11-unix/*; do
displaynum=`echo $x | sed s#/tmp/.X11-unix/X##`
getXuser;
if [ x"$XAUTHORITY" != x"" ]; then
export DISPLAY=":$displaynum"
export _LXSESSION_PID=`pidof lxsession`
lxsession-logout
exit
fi
done
fi
# If all else failed, just initiate a plain shutdown.
/sbin/shutdown -h now "Power button pressed"
The script works fine when run in a terminal - both as a user and root - but won't work if run by ACPID.
The only case the script is triggered by ACPI is when I have a root session open in gnome-terminal.
Do you have any idea what could be wrong? Is there any other info I can provide that could help you figure out what's going on?
I tried setting the environment variables manually but if I do so the script only works until the first time I launch a command with root.
System info:
Ubuntu 12.04.2 LTS
single-user
running LXDE/Openbox
Edit:
I ran some diagnostics and found out that both XUSER and XAUTHORITY remain empty when run by ACPID. No idea why, though.