Auto suspend server -> bash script for Ubuntu which checks several conditions

Hey plex community :),

I wanted to use WOL to start my Plex server automatic if there is a incoming client connection and suspend the server, when no client is active. So where’s the problem?

I can start my server automatic, if there is a client connecting. (I use a Fritzbox router, which has the option to automatic wake network devices per magic packet, as soon as there is a incoming activity for that device.)

My server is running xUbuntu 18, but unfortunately, there is no way to control the auto suspend function if there is a client watching. So the server goes into suspend, even if someone is watching.

Therefore I made a simple bash script for using with Ubuntu: It’s called PlexChecker, and checks several conditions, before putting the server into standby: :star_struck:
It is NOT perfect or beautiful, I know that, but I wanted to share it, because I found nothing else which does the trick. You are also free to mod it :v:

Things that are checked before going into standby:

  1. CPU usage,RAM usage and Running time of the Plex Media Server process
  2. Active Plex Transcoder processes
  3. Active network connections of Plex processes
    (Transcoding and direct plays are covered)

It’s working fine so far, you have to run this as root, I run it hourly under the root cron :ok_hand:
Steps you have to do:

  1. Disable the builtin auto suspend function of xUbuntu / Ubuntu
  2. enable WOL
  3. Put the content into a PlexChecker.sh file and run it under root cron hourly

Here is the code:

#!/bin/bash
###########################################################
#Script for looking after active Plex sessions
#If nothing is streamed, Server goes into suspend, wol works automatic with a fritzbox
#This checks for:
#1. CPU usage,RAM usage and Running time of the Plex Media Server process
#2. Plex Transcoder processes
#3. Active network connections of Plex processes
#17.09.2019, muhagl, V1.0, tested under xUbuntu 18, run as root!
###########################################################

echo '[PlexChecker] Check for active Plex connections...'
logger '[PlexChecker] Check for active Plex connections...'

TranscoderActive=true
CpuUsage=0
RamUsage=0
RunningTime=1
NetworkConnections=100

echo '[PlexChecker] Checking...'
logger '[PlexChecker] Checking...'

### Check for active Plex Transcoder process:
pgrep -x "Plex Transcoder" >/dev/null && TranscoderActive=true || TranscoderActive=false
###

### Check if Plex Media Server process is running as active (time diff 3 Mins) 0=Idle, everything else=active:
timeval1=$(ps aux | grep "/usr/lib/plexmediaserver/Plex Media Server" | grep -v grep | awk {'print $10'})
sleep 180
timeval2=$(ps aux | grep "/usr/lib/plexmediaserver/Plex Media Server" | grep -v grep | awk {'print $10'})

#Remove :-sign from string
timeval1=${timeval1/:}
timeval2=${timeval2/:}
RunningTime=$((timeval2-timeval1))
###

### Check for network connections:
NetworkConnections=$(netstat -p | egrep -c *Plex) 
###

### Check CPU & RAM Usage of Plex Media Server process:
for i in {1..60}
do
	#Get CPU Usage
	cpuval1=$(ps aux | grep "/usr/lib/plexmediaserver/Plex Media Server" | grep -v grep | awk {'print $3'})
	#Round value	
	cpuval2=$(echo "($cpuval1+0.5)/1" | bc)
	
	#Get RAM Usage
	ramval1=$(ps aux | grep "/usr/lib/plexmediaserver/Plex Media Server" | grep -v grep | awk {'print $4'})
	#Round value	
	ramval2=$(echo "($ramval1+0.5)/1" | bc)

	#Save them
	CpuUsage=$((CpuUsage+cpuval2))
	RamUsage=$((RamUsage+ramval2))
	sleep 3
done

CpuUsage=$((CpuUsage/60))
RamUsage=$((RamUsage/60))
###

### If ElseIf clauses:		
if [ "$TranscoderActive" = true ] && [ "$CpuUsage" -ge 1 ] && [ "$RamUsage" -ge 4 ] && [ "$RunningTime" -ne 0 ] && [ "$NetworkConnections" -gt 15 ]; then
	echo '[PlexChecker][1] Streaming is active, I do nothing...'
	logger '[PlexChecker][1] Streaming is active, I do nothing...'

elif [ "$TranscoderActive" = false ] && [ "$CpuUsage" -ge 1 ] && [ "$RamUsage" -ge 4 ] && [ "$RunningTime" -ne 0 ] && [ "$NetworkConnections" -gt 15 ]; then
	echo '[PlexChecker][2] Streaming is active, I do nothing...'
	logger '[PlexChecker][2] Streaming is active, I do nothing...'

elif [ "$TranscoderActive" = false ] && [ "$CpuUsage" -eq 1 ] && [ "$RamUsage" -le 5 ] && [ "$RunningTime" -eq 0 ] && [ "$NetworkConnections" -le 10 ]; then
	echo '[PlexChecker] Suspend because nothing is active...'	
	logger '[PlexChecker] Suspend because nothing is active...'			
	echo '[PlexChecker] It is time to suspend :)'
	logger '[PlexChecker] It is time to suspend :)'
	systemctl suspend
else
	echo '[PlexChecker] Something is active but is not catched under the If Else clauses, do nothing...'	
	logger '[PlexChecker] Something is active but is not catched under the If Else clauses, do nothing...'
fi
###

Greets

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.