Seems there is no solution by using the audio, since a process locks down in ALSA until it sends a message to release it, so it's down to the programming in the player. For instance, manually pausing a video on youtube does not unlock it, but watching until the end does.
Anyway, another surrogate would be the network activity, and probably a better option too, as I had not thought about a situation where a user leaves the computer downloading something. Most players in websites gradually download the media file as one plays it, not everything at once, so it usually correlates with a video being played or not.
So, I've made a new script. This will suspend/shutdown after x minutes of mouse, keyboard or network inactivity, unless certain applications are running (like kodi). I felt the keyboard part was necessary, as I had the pop-up show when I was writing a document (fortunately just a pop-up, and not immediate shutdown
)
Code: Select all
#!/bin/ash
POWERTIMEOUT=30 # set to your liking in minutes
IDLE_TIME=$((POWERTIMEOUT*60000)) # Wanted trigger timeout in milliseconds.
TRIGGER=0
while true; do
sleep 60
NETUSAGE2="`cat /sys/class/net/*/statistics/rx_bytes | awk '{ SUM += $1} END { print SUM }'`"
TRIGGER=$((TRIGGER+1))
[ -z "$NETUSAGE1" ] && NETUSAGE1="$NETUSAGE2"
[ "$NETUSAGE2" -gt "$((NETUSAGE1+1024*500))" ] && TRIGGER=0 # set to your liking in KB
NETUSAGE1="$NETUSAGE2"
if [ $TRIGGER -ge $POWERTIMEOUT ]; then
RUNPS="`busybox ps`"
if [ "`echo "$RUNPS" | grep -w -E 'kodi|make|cc|gcc|imake|cmake|new2dir|xorriso|xorrecord|xine|gxine|petget|wget|axel|dotpup|mplayer|gmplayer|smplayer|mpv|vlc|gcurl|gimv|burniso2cd|growisofs|cdrecord|pcdripper|xfmedia|xmms|ripoff|pdvdrsab|pburn|mhwaveedit'`" != "" ] ; then
TRIGGER=0
else
idle=$(xprintidle)
if [ $idle -ge $IDLE_TIME ]; then
/etc/acpi/actions/acpi_poweroff.sh # don't shutdown without a warning - replace with /etc/acpi/actions/suspend.sh to suspend
TRIGGER=0 # in case of resume from suspend
fi
fi
fi
done
This script requires xprintidle, which can be downloaded through the puppy package manager (a few KB in size).
The definition for network inactivity can be adjusted. I've put 500 KB/min as a threshold to avoid false time resets, but can probably go lower.
This script can run on its own, or can work with the puppy event manager. For this, just copy lines 4-25, and paste them in the file /usr/local/pup_event/autoshutdown_daemon, replacing from line 9 onwards in the latter. The advantage of doing this way is that the desired time can be easily adjusted with the puppy event manager (and probably with the nicOS-PowerTimeout).
Personally I also edited acpi_poweroff.sh so that it would suspend as a default, but retaining the warning and option to cancel.
I really enjoyed the challenge of learning more about shell to write this, and is working for me so far in the last few days, but I'm still a complete novice in programming, so any suggestions, shortcomings or potential issues I overlooked would be welcome
EDIT-Had written the wrong lines to be copied into autoshutdown_daemon (should be 4-25, not 3-25, but the forum went down just when I tried to edit it)
EDIT 2-Changed line 8, so that all network interfaces will be taken into account, previously only eth0 was being considered