suspend your ubuntu linux if it’s inactive and nobody’s listening music on it

First you need a script which can suspend your machine from the command line:

me@kv-laptop:~$ cat /usr/bin/power-cmd
#!/bin/bash
#    gnome-power-cmd replacement
#    Copyright (C) 2009 A. Bram Neijt <bram @neijt.nl>
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see http ://www.gnu.org/licenses.

case $1 in
suspend)
echo Suspending
    dbus-send —print-reply \
        —system \
        —dest=org.freedesktop.UPower \
        /org/freedesktop/UPower \
        org.freedesktop.UPower.Suspend
;;
hibernate)
echo Hibernating
    dbus-send —print-reply \
        —system \
        —dest=org.freedesktop.UPower \
        /org/freedesktop/UPower \
        org.freedesktop.UPower.Hibernate
;;
*)
echo Not supported command: ‘”’$1’”’
echo Usage: $0 ‘<suspend |hibernate>’
exit 1
;;
esac

Then you need a script which records music player status (I use Audacious and Radiotray):

me@kv-laptop:~$ cat .bin/paactivity
#!/bin/bash

export DISPLAY=:0

pacmd list-clients | egrep -i “radiot” > /dev/null
RADIO=$?
audtool2 playback-playing
AUDA=$?

echo “radio: $RADIO auda: $AUDA” > /tmp/lastrun


if [ $AUDA -eq 1 ] && [ $RADIO -eq 1 ];
then
    rm /tmp/.active-audio
else
        touch /tmp/.active-audio
fi

You have to put this sctipt into your crontab. (crontab -e -> */2 * * * *  /path_to/paactivity)

Then you need to start xautolock at login (apt-get install xautolock):

me@kv-laptop:~$ cat .bin/inactivity
#!/bin/bash

exec xautolock -time 8 -locker “if [ ! -e /tmp/.active-audio ] ; then /usr/bin/power-cmd suspend; fi”

And finally you need to configure Gnome to start this at login, (Settings/Preferences/Startup Applications/New -> /path_to/inactivity).