HDD Auto spinup on Plex container activity


mgutt

Recommended Posts

I'm trying to use this script to spin up the drives to avoid the Emby coverart loading time. However, when I try to run the script, I get the following error: /tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found

 

Right now, I'm just running it to test using the CA User Scripts "Run Script" option.

 

Any ideas why it's reporting mdcmd not being found? I did try to run mdcmd status | grep "rdevLastIO.1}=" | cut -d '=' -f 2 | tr -d '\n' from the webterminal and that worked fine.

Link to comment

That is the error: /tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found

 

Quote

Script location: /tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script
Note that closing this window will abort the execution of this script
Container's CPU load exceeded threshold
/tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found
/tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found
/tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found
/tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found
/tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found
/tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found
/tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found
Container's CPU load exceeded threshold
/tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found
/tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found
/tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found
/tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found
/tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found
/tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found
/tmp/user.scripts/tmpScripts/emby_monitoring_spinup/script: line 18: mdcmd: command not found

 

Only difference to your original script, was that I changed it to monitor for Emby rather than Plex.

 

Quote

 

#!/bin/bash

# make script race condition safe
if [[ -d "/tmp/${0///}" ]] || ! mkdir "/tmp/${0///}"; then exit 1; fi
trap 'rmdir "/tmp/${0///}"' EXIT

# ######### Settings ##################
spinup_disks='1,2,3,4,5,6,7' # Note: Usually parity disks aren't needed for Plex
cpu_threshold=1 # Disks spin up if Plex container's CPU load exceeds this value
# #####################################

# ######### Script ####################
while true; do
    plex_cpu_load=$(docker stats --no-stream | grep -i emby | awk '{sub(/%/, "");print $3}')
    if awk 'BEGIN {exit !('$plex_cpu_load' > '$cpu_threshold')}'; then
        echo "Container's CPU load exceeded threshold"
        for i in ${spinup_disks//,/ }; do
            disk_status=$(mdcmd status | grep "rdevLastIO.${i}=" | cut -d '=' -f 2 | tr -d '\n')
            if [[ $disk_status == "0" ]]; then
                echo "Spin up disk ${i}"
                mdcmd spinup "$i"
            fi
        done
    fi
done

 

 

Edited by PeterDB
Added the script for reference
  • Like 1
Link to comment
  • 1 year later...
On 10/13/2020 at 3:53 PM, mgutt said:

This script automatically spins up all defined Disks to remove spin up latency on playback. It should be executed through CA User Scripts on array startup. This script is inspired by @MJFOx's version. But instead checking the Plex log file, which needs to enable Debugging, this script monitors the CPU load of the Plex container (which rises if a Plex Client has been started):

#!/bin/bash

# make script race condition safe
if [[ -d "/tmp/${0///}" ]] || ! mkdir "/tmp/${0///}"; then exit 1; fi
trap 'rmdir "/tmp/${0///}"' EXIT

# ######### Settings ##################
spinup_disks='1,2,3,4,5,6,7' # Note: Usually parity disks aren't needed for Plex
cpu_threshold=1 # Disks spin up if Plex container's CPU load exceeds this value
# #####################################
# 
# ######### Script ####################
while true; do
    plex_cpu_load=$(docker stats --no-stream | grep -i plex | awk '{sub(/%/, "");print $3}')
    if awk 'BEGIN {exit !('$plex_cpu_load' > '$cpu_threshold')}'; then
        echo "Container's CPU load exceeded threshold"
        for i in ${spinup_disks//,/ }; do
            disk_status=$(mdcmd status | grep "rdevLastIO.${i}=" | cut -d '=' -f 2 | tr -d '\n')
            if [[ $disk_status == "0" ]]; then
                echo "Spin up disk ${i}"
                mdcmd spinup "$i"
            fi
        done
    fi
done

 

Explanation

- it requests the containers CPU load every ~2 seconds (answer time of "docker stats")

- if the load is higher than "cpu_treshold" (default is 1%) it checks the disks spinning status

- all sleeping "spinup_disks" will be spun up

 

Downside

- as long a Movie is running, all (unused) disks won't reach their sleep state (they spin down, but will be directly spun up again)

 

Monitoring

If you like to monitor the CPU load while (not) using Plex to find an optimal threshold value (or just for fun ;) ), open the WebTerminal and execute this (replace "1" against a threshold of your choice):

while true; do
    plex_cpu_load=$(docker stats --no-stream | grep -i plex | awk '{sub(/%/, "");print $3}')
    echo $plex_cpu_load
    if awk 'BEGIN {exit !('$plex_cpu_load' > 1)}'; then
        echo "Container's CPU load exceeded threshold"
    fi
done

On my machine Plex idles between 0.1 and 0.5% CPU load, why I choosed 1% as the default threshold.

 

Anyone have this script working under 6.10.0-RC5?   The command "mdcmd status" doesn't appear to output rdevLastIO anymore.  Also, from what I can tell ,"mdcmd spinup" does not work either.  

  • Like 1
Link to comment
  • 1 month later...
  • 4 months later...

I was looking for a solution to this since moving from Windows Server to Unraid, I use to have a PS script that ran on a loop looking for connections on port 32400, it would change to ESTABLISHED as the user connects, the script would then simply write an entry to a log file on the hdd array causing it to spin up. Because the Plex DB lives on the SSD most of the time a user will still be browsing the interface whilst the script is spinning the disks up ahead of their selection. I'm pretty new to Unraid and haven't written anything in bash before but I think the below would result in a similar kind of thing...

 

#!/bin/bash

mystate=`lsof -i -P -n | grep 192.168.1.1:32400`
current_time=$(date "+%Y.%m.%d-%H.%M.%S")

echo [BEGIN LSOF OUTPUT]
echo $mystate
echo [END LSOF OUTPUT]

if [[ $mystate == *"ESTABLISHED"* ]]; then
  echo "status=SOMEONE CONNECTED!"
  echo "write to logile on HDD array"
  echo $current_time > /mnt/user/media/logs/plextickle.log
else
  echo "status=NO CONNECTIONS!"
fi

 

I've just tested this by using the web terminal on the host and running watch -n 2 ./scriptname.sh (-n 2 sets it to run every 2 seconds which seems aggressive enough). Obviously you need to adjust the IP and the path you want it to write to (you could add multiple paths if you want, or specific disks to spin up rather than all disks in a share)  

Link to comment
17 hours ago, mgutt said:

Nice idea. How much CPU load does it produce?

I'm not really sure how to tell, I stopped all the containers and my overall CPU from the main page shows as fluctuating between 1-2% and this doesn't change whilst the script is running. I wouldn't expect the script to use much at all though. The other idea I had considered was using tail to watch the 'Plex Media Server.log' file (which on my setup is on a separate SSD) use this in conjunction with grep to find entries of 'Signed-in Token' and at that point redirect the log to the storage array to wake it up. It felt a bit more messy even though it could probably be done in one line of code if you knew what you were doing (which I don't!). I understand now I should probably be using a plug-in to run the scripts so will probably wrap the above script in a while loop with a 2s wait rather than using the watch command. I have a bigger problem in that my drives don't actually spin down and I think it's because I'm using a yottamaster enclosure and that perhaps I need some custom firmware to make it happen. 

 

Just throwing in an edit here as I decided to give the other idea a go:

 

tail -f  Plex\ Media\ Server.log | awk '/Signed-in Token/' > /mnt/user/media/logs/wakemyarray.log

 

This one line should watch the plex media server log for any user requesting a sign-in token and then redirect the log output to a file on the storage array which should hopefully wake it up.

 

Edited by matthawkp
Adding extra info
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.