13

Running: Xubuntu 14.04 32-bit.

I have read several outdated, not working, working different that I want answers here and elsewhere. I had a script that resulted in shutting my Laptop down at boot time after I set it to autostart.

I also tried it install complexshutdown despite that tool having died in 2011 and has a bug on Lauchpad that his exact option is not available. It not even installed on 14.04 here.

I am looking for a simple straightforward script that is actually tested and working that I can put into the autostart that is doing nothing but checking for activity (mouse/keyboard) and shutting down my laptop if there was none for 2 hours.

Thanks.

Zanna
  • 72,312
redanimalwar
  • 1,610
  • 3
  • 20
  • 36

5 Answers5

10

This is probably the best solution. No need for screensaver tweaking and running.

Install sudo apt-get install xprintidle

Put this script into autostart:

#!/bin/bash

idletime=$((1000*60*60*2)) # 2 hours in milliseconds

while true; do
    idle=`xprintidle`
    echo $idle
    if (( $idle > $idletime )); then
        #sudo shutdown -P now
        dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 "org.freedesktop.login1.Manager.PowerOff" boolean:true
    fi
    sleep 1
done

The comments and @Jobin 's answer did lead me to investigate myself again and I actually found a unfinished script with xprinttime but without any loop in the script. Probably not finished this because my linux/bash knowlege was not good at the time. I also did put the script I had in rc.local or something like that triggered the shutdown on boot. Thanks to @Jobin for the reminder how to add startup apps in XFCE, I already knew this but ... and credits for the dbus thing, never saw that, better then shutdown since it not requires root.

redanimalwar
  • 1,610
  • 3
  • 20
  • 36
7

This is an old question but I thought I would answer it with what works for me in Ubuntu 21.04. You can set an IdleAction in your systemd/logind.conf file.

Edit the file using:

sudo nano /etc/systemd/logind.conf

and add:

IdleAction=poweroff
IdleActionSec=120min

I have tested it with IdleActionSec=1min and the machine shuts down as expected.

Floyd Powers
  • 131
  • 1
  • 4
5

After having looked at a number of options for shutting down after a certain time of inactivity, it seems that xautolock is the easiest way. All credits to Sparhawk for mentioning about xautolock.

Thanks to Sneetsher for pointing out to xscreensaver. Using xscreensaver, I could manually specify what to do after a certain amount of time of inactivity. To use xscreensaver, you need to install it using:

sudo apt-get install xscreensaver

or install it from the software center and then run it once using:

xscreensaver-demo

or type "xscreensaver" on the dash and open "Screensaver".

This will create a ~/.xscreensaver file. Open it and search for the line:

programs:                                                                     \

and add:

dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 "org.freedesktop.login1.Manager.PowerOff" boolean:true \n\

just below the

programs:                                                                     \

line.

You can specify the time after which shutdown should be triggered by changing the line starting with timout. Modify it to

timeout:        2:00:00

to shutdown after two hours.

Have a look at my .xscreensaver file here.

This should poweroff your machine after two hours of inactivity or whatever time you specify in the script.

Notes:

  • Have a look at this question to see how to add xscreensaver on boot.

  • I tried using complex shutdown, but the bug here seems to affect me so could not happen. Otherwise, a graphical application would have been available for this.

  • This could be achieved using xautolock, however, as redanimalwar pointed, out a timout greater than 1 hour is not possible without modifying its source code and recompiling.

jobin
  • 28,567
2

This is my simplified script, this requires that you install the "xprintidle" package and modify the shutdown command so that is possible to run without sudo/password.

sudo chmod u+s /sbin/shutdown
sudo apt-get install xprintidle

Script

#!/bin/bash
idletime=$((15*60*1000)) # 15 min in milliseconds
idle=0

while [ $idle -lt $idletime ];do
    idle=`xprintidle`
    sleep 1
done
shutdown -P now
1

Modified @redanimalwar script to warn user and give a chance to cancel the shutdown. For tests, it is using 3 seconds and a simple message. Please adjust for your tastes. HTH.

#!/bin/bash
#https://askubuntu.com/questions/442795/
#http://ubuntuforums.org/showthread.php?t=2172828

function showProgress(){
#
# Force Zenity Status message box to always be on top.
sleep 1 && wmctrl -r "Progress Status" -b add,above &

(
echo "# Shutting Down in 5" ; sleep 2
echo "25"
echo "# Shutting Down in 4" ; sleep 2
echo "50"
echo "# Shutting Down in 3" ; sleep 2
echo "75"
echo "# Shutting Down in 2" ; sleep 2
echo "99"
echo "# Shutting Down in 1" ; sleep 2
echo "# Shutting Down now" ; sleep 2
echo "100"

) |
zenity --progress \
  --title="Progress Status" \
  --text="First Task." \
  --percentage=0 \
 --auto-close

return $?

}


#idletime=$((1000*60*60*2)) # 2 hours in milliseconds
idletime=$((1000*3)) # test, 3seconds

while true; do
    idle=`xprintidle`
    echo $idle
    if (( $idle > $idletime )); then
        showProgress && \
        #sudo shutdown -P now
        #dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 "org.freedesktop.login1.Manager.PowerOff" boolean:true
        #echo for tests. use command above to actually shutdown
        echo shuting down the system. Just kidding  ^_^ \
        && exit 0
    fi
    sleep 1
done