Home of the Squeezebox™ & Transporter® network music players.
Results 1 to 2 of 2
  1. #1

    Prevent suspend / keep awake - shell script

    I've just moved from a Windows XP Squeezebox server to a new Ubuntu machine and needed to sort out the power management.

    Ubuntu power management appears to simply look for keyboard and mouse activity to determine if the machine is in use, and consequentially will suspend even if playing music. On XP I used the SrvrPowerCtrl plugin, but that was sometimes irritating in so far as it was unaware of keyboard and mouse activity!

    This script essentially checks to see if any Squeezebox players are on, and also checks disk activity. The amount of data read/written to disk over 10 minutes is checked to see if it exceeds a threshold. If the computer needs to remain on the mouse is momentarily moved from its current location and back again to reset Ubuntu's suspend timer. The mouse move is practically invisible to the user.

    The script may also be of interest to anyone wanting to script the CLI using netcat.

    Tony



    Code:
    #!/bin/bash
    
    # Moves mouse to reset sleep timer if at least one player is powered or disk activity 
    # exceeds threshold
    
    # Requires iostat and xdotool 
    #(to install iostat - sudo apt install sysstat  -  sudo apt install xdotool)
    
    # Tony Wilson 0319
    
    IP="127.0.0.1 9090"	
    threshold=10000    	# you may need to tweak this!
    
    while true
    do
      			#find number of players
      echo
      echo $(date '+%H:%M:%S %d-%m-%Y')
      nplayers=$(echo "player count ?" | nc -q1 $IP | tail -c 2 )
      #echo $nplayers
    
      n=0
      power=0
    	#loop through players to determine power status
    
    	while [ $n -lt $nplayers ]
    	do
    		pmac=$(echo "player id $n ?" | nc -q1 $IP | tail -c 28 | sed -r 's/%3A/:/g' )
    		pname=$(echo "player name $n ?" | nc -q1 $IP | cut -c 15- | sed -r 's/%20/ /g' )
    		pstate=$(echo "$pmac power ?" | nc -q1 $IP | tail -c 8 )
    		
    		printf "%-16s %-10s" "$pname"  "$pstate"
    		echo
    
    		pstate=$(echo $pstate  | tail -c 2 )
    		power=$((power+pstate)) #count players powered on
    		n=$(($n + 1))
    	done
    		
    		sda_r0=$sda_r
    		sda_w0=$sda_w
    		sdb_r0=$sdb_r
    		sdb_w0=$sdb_w
    					#use iostat to check disk activity
    
    		sda_r=$(iostat -d sda | grep sda | awk '{ print $5; }')
    		sda_w=$(iostat -d sda | grep sda | awk '{ print $6; }')
    		sdb_r=$(iostat -d sdb | grep sdb | awk '{ print $5; }')
    		sdb_w=$(iostat -d sdb | grep sdb | awk '{ print $6; }')
    
    		diff=$((sda_r-sda_r0  + sda_w-sda_w0 + sdb_r-sdb_r0  + sda_b-sda_b0))
    
    		echo $diff kB read/write over 10 minutes
    
    		if [ $diff -gt $threshold ]
    			then 
    			power=$((power+1))
    		fi
    	
    		
    		#echo $power
    		if [ $power -gt 0 ]		
    			then
    			eval $(xdotool getmouselocation --shell)
    			echo Move Mouse!
    			xdotool mousemove 100 100
    			sleep 0.1
    			xdotool mousemove $X $Y
    		fi
    sleep 600 # 10 minutes
    done

  2. #2
    Senior Member
    Join Date
    May 2017
    Posts
    404
    Nice solution
    SqueezeBoxes: 1x Transporter (Living room) 1x SB2 (shed), 1x Radio (Kitchen), 1x Boom (Dining room), 1x piCorePlayer (jacuzzi), 1x piCorePlayer (Garden) 1x OSMC + Squeezelite (Movie room), 1x Touch (Study 2), few spare unit's
    Server: LMS on Pi3 7.9.1. on PcP 3.21
    Network: AVM Fritzbox, Netgear Smart Switch 24p, 3x Ubiquity

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •