9

Running: Xubuntu 14.04 32bit.

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 tryed it install complexshutdown despite that this tool died in 2011 and has a bug on Lauchpad that his exact option is not available. It not even installed on 14.04 here.

So if I may ask this question again without being punished with "duplicate of" ...

I am looking for a simple straight forward 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.

6
  • 1
    Are you using any screen saver (xscreensaver..)? – user.dz May 3 '14 at 7:36
  • 1
    xautolock is another option. – Sparhawk May 3 '14 at 7:37
  • No I do not use any screen-saver but I don't mind using one to get it to trigger the shutdown. I already got the basic idea but I asked for a working script. And I already had some script with xautolock that was not working currently so if one of you both like to answer with a working script and get the 50 reputation that would be great ;) – redanimalwar May 4 '14 at 5:55
  • So what if your computer has no mouse/keyboard activities but your network or cpu is busy eg downloading, crunching numbers..? – Flint May 5 '14 at 13:55
  • @Flint I don't care about that. If I want to download overnight or something I can kill the script b4 that. – redanimalwar May 6 '14 at 5:33
9

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.

6
  • 1
    Actually, it should be 2*60*60*1000? thank you nice to know about xprintidle, simpler script. As 2 hours is a long time you raise sleep delay (60sec or even more). – user.dz May 5 '14 at 22:35
  • I tested counter using while sleep 1; do xprintidle; done , it works perfectly before I Ctrl+C it was 1154009 ~19min. I thought both (as xscreensaver) uses same X server properties/variable. Does xscreensaver works well, does it pop up? – user.dz May 6 '14 at 8:15
  • 1
    Maybe I touched my touchpad without realizing it. Was laying in bed with my laptop at the time ;) It works now. – redanimalwar May 8 '14 at 7:04
  • Where is autostart? – Mohammad Etemaddar May 12 '19 at 14:20
  • 1
    @MohammadEtemaddar You have to put the .sh script somewhere and make it executable (chmod +x script.sh). Then create a .desktop file under ~/.config/autostart putting in Exec the path to your .sh script and Terminal=false. I have edited the answer to explain how to properly run the script at startup, hope that the edit will get accepted – robertspierre Feb 24 at 17:29
5
+50

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.

8
  • 1
    " -time Specifies the primary timeout interval. The default is 10 minutes, the minimum is 1 minute, and the maximum is 1 hour." – redanimalwar May 5 '14 at 12:29
  • @redanimalwar: My bad, I did not happen to see that. Will check for alternatives. – jobin May 5 '14 at 12:35
  • @redanimalwar: Have modified my answer to use xscreensaver to poweroff, have a look. – jobin May 5 '14 at 13:45
  • Still looking for a better solution then this, I wish the script in my own answer would be working. I will award you the 50 if nothing better comes up or anyway. And you should probably mention that one should set the screensaver to "single" and then select the new command in the list to be used instead of a screensaver in the screensaver settings. You can run the xscreensaver-demo command to bring it up a 2nd time after adding the command. I am curious what mechanism or command the screensaver uses to measure idle time. Maybe I will recompile xautolock, this 1 hour limit makes no sense to me. – redanimalwar May 6 '14 at 5:49
  • @redanimalwar: I am able to poweroff even if the screensaver is "random" and not "single". Why do you want it to be set to "single"? – jobin May 7 '14 at 12:19
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.