How can I get the idle time via dbus while running from /etc/rc.local?

557 Views Asked by At

I have a rudimentary bash script that checks if the system has been idle for more than an hour, and if so, shuts it down:

#!/bin/bash

while true; do
get_idle_time=`dbus-send --print-reply --dest=org.gnome.Mutter.IdleMonitor /org/gnome/Mutter/IdleMonitor/Core org.gnome.Mutter.IdleMonitor.GetIdletime`
idle_time=$(echo $get_idle_time | awk '{print $NF}')
if [[ $idle_time -ge 3600000 ]]; then
shutdown
fi
done

I added this script to /etc/rc.local so it would always be active:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

bash /home/paulky/shutdown.sh

exit 0

Although bash /home/paulky/shutdown.sh from a terminal works while I'm logged in, doing it from /etc/rc.local seems to prevents Ubuntu from getting to the login screen, and F2 shows this error message repeating itself infinitely:

Failed to open connection to "session" message bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11.

I've tried prepending export DISPLAY=:0 to the start of the script, to no avail.

0

There are 0 best solutions below