Suspend live from the command line
#1
I've got Live set up on my Revo which works great. This box is also the media server for other XBMC clients on the network. I'd like the system to suspend on idle timeout, but in this case the in built XBMC power management settings wont help as it will go into suspend even if other clients are accessing it's attached media drives.

I've been looking for a while now and can't really find a simple solution to enable autosuspend within the underlying Linux OS. Is there not a simple conf file setting somewhere to enable and set an idle timeout?

Cheers
Reply
#2
Ok, after a few days of investigation I couldn't find a ready made built in solution so I went with the DIY approach. Having not touched a 'nix system in 10 years I had to do some more research but I finally got it to basically do what I intended which is to go into suspension if there are no remote users logged in and if the CPU is idling. This thread and this thread were very helpful in pointing me in the right direction.

I installed two packages; powernap and sysstat using apt-get install. Powernap provides a basic service of watching any number of processes you specify and will execute your script if those processes haven't been running for a specified time period. It also watches the keyboard by default so will take no action if the keyboard is in use. You configure powernap with a simple config text file. I have no current need to keep any particular process running so I left that part undefined and changed the timeout to 20 minutes, so every 20 minutes as long as the keyboard is not in use powernap will execute my script. The sysstat package provides tools for monitoring system resources, I just used the sar command from it.

This is the powernap action script I came up with;

Code:
CPUDURATION=20       # duration in seconds to determine cpu usage
CPUTHRESHOLD=5     # in percent

LOGDIR='/etc/powernap'
LOGFILE="$LOGDIR/suspend_action.log"

exec 1>>$LOGFILE
# Send debug output to debug log
exec 2>>$LOGFILE".debug"

echo "Powernap action script called $(date)"

# see if there are any users logged in
# each line of output constitutes a user logged in plus 2 header lines
REMOTE_USERS=$[ `net status sessions | wc -l`-2 ]
echo "Users logged in = $REMOTE_USERS"
if [ $REMOTE_USERS -gt 0 ];then
    echo $'Users logged in so keep running\n'
    exit;
fi

# see if the cpu usage is greater than the allowed threshold
CPU_USAGE=`sar $CPUDURATION 1 | awk '/Average/ {printf "%d\n", 100-$8}'`
echo "CPU usage = $CPU_USAGE%"
if [ $CPU_USAGE -gt $CPUTHRESHOLD ];then
    echo $'CPU in use so keep running\n'
    exit;
fi

# no users logged in and cpu is idling so should be ok to suspend
echo $'No users and idle CPU so suspending\n'
pm-suspend>>$LOGFILE

exit 0

This will simply call 'net status sesions' to provide a list of currently logged in users over samba and exits if there are any. sar is then called over a period of 20 seconds and the average CPU% compared to a threshold of 5% and exits if it's greater. I chose 5% after running sar a few times in different states to see what the system load looked like. With xbmc just sitting in the menu the CPU was around 30-34%, 100% during the screen saver and 2-3% when xbmc had switched off the display. So 5% currently seems reasonable to assume nothing I'm interested in is running, but you must set the display timeout in the system settings and turn off the shutdown function timer for the above method to work.

I have a couple of other XBMC clients (two xbox's and a couple of laptops) that automatically send the wake on lan magic packets on startup to wake the system up if it happens to be sleeping.

I'm sure there are many other methods for achieving the same thing but hopefully this might provide you with one solution if you are looking for the same functionality.
Reply
#3
Very interesting script but there is problem, I use vdpau when watching a movie the computer shuts down so I tweaked a little bit your script to check is the GPU is in use or not, I will publish it when I finish to polish it a little bit.
XBMC SVN (updated every week) ubuntu Karmic 64bits
Intel E5200/ 3 Go Ram / Nvidia GT220 1Go / 6To (1.5To x 4 Raid 5)
Reply
#4
This is exactly what I need, and I also use vdpau to watch movies. Did you publish the script sensei73?
Reply

Logout Mark Read Team Forum Stats Members Help
Suspend live from the command line0