Testing to see if Plex is running every 20 minutes and restart if not

I posted a while back how to test if Plex was running on your Asustor NAS, and how to restart it.

 

This stopped working when I started using Plex Home features.  While working on a different problem Mark Walker showed me the correct curl part of the script I was using to test with.  This works on Plex Home.

 

The first part is making the actual testing script:

 

With WinSCP, make a new file and call it S56CkPlex.sh  (I put this script in a folder for misc stuff.  plexWatch, Maraschino, etc.  the path I use is:  /volume1/misc

 

This file should contain the following lines:

echo "Checking Plex..." >> 
/var/log/checkPlex.log
date >> 
/var/log/checkPlex.log
curl -I -m 8 "http://localhost:32400/web/index.html";
if [ "$?" -ne "0" 
]
then
     echo "Plex 
froze. Restarting.." >> /var/log/checkPlex.log
     date 
>> /var/log/checkPlex.log
     echo "Plex 
froze. Restarting.."
     
/usr/local/AppCentral/plexmediaserver/CONTROL/start-stop.sh 
restart
fi

The next part involved modifying the existing start-stop.sh.  You need to insert the bolded parts in this file:

 

case "$1" in

     start)

          do_start

     ;;

     stop)

          do_stop

     ;;

     restart)

          do_stop

          sleep 15

          do_start

     ;;

esac

exit 0

 

The last part is to put in a cronjob.  Go to this folder/file in WinSCP:

 

/var/spool/cron/crontabs/root

 

Put this line into the file:

 

*/20 * * * * /volume1/misc/S56CkPlex.sh

 

Make sure you save this.  Then, open Terminal within WinSCP and execute the following:

 

/etc/init.d/S41crond restart

 

This restarts the cron daemon and makes sure you job is running every 20 minutes.  Now, if you look at the log files created by this script you can check it's running.  After you get it going you can comment out the 1st 2 lines so it doesn't keep filling the logs with checking messages.  After this it just puts in restart messages.

 

Thanks to Mark Walker for providing me with an updated curl line for testing Plex is running.  the line I haad worked, but errored out....

This can be streamlined even further now, looking at the Asustor forums.

With this script in place of the one I have above, you will get a log entry in the system logs if there is a problem with Plex.  This can also be used to test remote access, if you use Asustor's DDNS.  Since Asustor has the app for iOS and Android called AiMaster for doing some remote functions via a phone or tablet, you can even get push notifications of the server having to restart, etc.

Here's the code:

#!/bin/sh

     /usr/sbin/syslog --log 0 --level 0 --user admin --event "Testing Plex Access" # put a # in front of this to stop it.  Remove this

                 comment.  It will test according to the cronjob you set up before.

     curl -I -m 8 "http://put.your.IP.here:32400/web/index.html"

     if [ "$?" -ne "0" ]

     then

          /usr/sbin/syslog --log 0 --level 0 --user admin --event "Remote Access to Plex is unavailable"

          /usr/local/AppCentral/plexmediaserver/CONTROL/start-stop.sh restart

     fi

Pay attention to the "#" in the second line.  Once you run this a few times, but a "#" at the front of the line, or delete it, to stop it from filling the log file every 20 minutes. 

Don't forget to edit the start-stop.sh file mentioned above so Plex can bet restarted.  Otherwise this script can give an error.

To test remote access, replace this:  http://put.your.IP.here:32400/web/index.html with your Asustor or other DDNS url.  Make sure you use whatever port you have forwarded on your router instead of 32400 if you have that one not forwarded.  (Like I do.)

HTH!

My hacked together version with Slack and log posting from an Unraid container. This runs every minute.

* * * * * /opt/plex_bouncer/plex_bouncer.sh

#!/bin/bash

# Check if Plex is running. If Plex isn't running, restart the docker container.

curl -I -m 8 "http://<PlexIP>:32400/web/index.html" > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
        #send slack, plex isn't running, restarting...
        ssh root@unraid.mydomain "docker restart 3a029cf63ed7"

        LAST_2HR_LOG=$(ssh root@unraid.mydomain "docker logs --timestamps --since 5h 3a029cf63ed7" 2>&1)

        #send slack
        curl -k -F username="plex" -F channel=plex -F icon_url="https://styles.redditmedia.com/t5_2ql7e/styles/communityIcon_mdwl2x2rtzb11.png" -F attachments="[{ 'color': '#FF0000','fields': [{'title': \"Plex was restarted...Log below\",'value': \"$LAST_2HR_LOG\"}] }]" -F token=<mytoken> https://slack.com/api/chat.postMessage > /dev/null 2>&1