Ok, I am running Ubuntu 15.04 with Kodi 15.2 as an HTPC. I have always had problems with suspend and hibernate due to the fact that it appears that the two systems' power management cause some conflicts. I have had Ubuntu put my system to sleep while watching movies on Kodi, so I disabled the power management. I have had Kodi put my system to sleep while watching Netflix (either with Advanced Launcher or Chrome Launcher), so I disabled that (currently my HTPC runs 24/7). I am trying to come up with some sort of script that not only measures inactivity, but audio output as well. I am a total noob when it comes to scripting, but this is what I came up with so far:
LOG=/var/log/sleepcounter.log
sound=0 > /var/log/sleepcounter.log
while true: do
sleep 1
# check if sound is sent to speakers, add 1 if not
if pactl list | grep -v RUNNING > /dev/null; then
echo sound=$((sound += 1)) > /var/log/sleepcounter.log;
fi
done
This essentially counts seconds that audio is not playing. The problem then is it does not reset the counter to "0" when audio does play. I attempted to switch it around, and came up with a slightly different script that does NOT work:
LOG=/var/log/sleepcounter.log
sound=0 > /var/log/sleepcounter.log
read -d $'\x04' sound < "$LOG"
while true; do
sleep 1
# check if sound is sent to speakers, add 1 if not
if pactl list | grep RUNNING > /dev/null; then
echo sound=0 > /var/log/sleepcounter.log
else echo sound=$((sound += 1)) > /var/log/sleepcounter.log;
fi
done
This exact script does not write anything to the sleepcounter.log file, when I check it it's blank. I can't figure out how to make it default to sound=0 and maintain 0 while sound is playing, start counting when sound stops, followed by starting over from 0 when sound starts again...
I have this paired up with another script:
LOG=/var/log/sleepcounter.log
read -d $'\x04' sound < $LOG
while true; do
sleep 30
# check if there is any system activity
if [[ `xprintidle` > 600000 && "$sound" -ge 600 ]]; then
(sudo pm-suspend-hybrid);
fi
done
This one does work. If I manually set sleepcounter.log to sound=600 (or more) it puts the system to sleep when it has been inactive for 10 minutes. While this works, I am thinking there also might be a way to combine the two scripts into one, just to make it a bit simpler.
I know I am having a problem with referencing the variables, and this is my first time trying to write a script. If anyone could help me out, I would greatly appreciate it.