2

I am running a plex media server for private use on a Ubuntu 16.04 Desktop VM at home. I use it while I'm away on work during the week.

Recently I've been plagued with connection issues. Sometimes it's plex itself that crashes and needs to be restarted and sometimes it's the internet connection (eth0) that needs to be restarted.

I need a little help with a script that I can call via cron to check if the server is remote accessible, if it can reach https://external.address:32400 (please note it only responds to https), if it's not accessible, restart the internet connection (eth0), then check again if it's remote accessible, if it's still not remote accessible then restart the plex media server.

Plex is installed as a service so the call service plexmediaserver restart is how I restart it. I guess as it's a desktop instalation to restart the network the script needs to use service network-manager restart.

I found this post and script but it's very old and outdated.

Hopefully someone can help me out with this.

Thanks in advance.

2 Answers2

1

ok, after doing a little more research on my problems, it turns out I have two different issues, sometimes the VM looses it's bridged connection and sometimes the plex media server crashes.

So, I have split the solution into two simple bash scripts that I call from cron.

The first checks if the internet is working, if not it restarts the VM. Simply restarting network-manager didn't work.

#!/bin/bash

PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/$

#Check if the vm can access google.com, if yes then exit
        if nc -zw1 google.com 80;
                then exit
#If it can't reach google.com restart the vm
                else shutdown -r now
        fi

The second script checks to see if it can access plex media server locally, if not then it restarts the plex service.

#!/bin/bash

PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/$

#Check to see that plex is acessable locally, if yes then exit
if curl -s --head  --request GET http://localhost:32400 | grep "200 OK" > /dev/$
  then exit
#If not then restart plex service 
else
   service plexmediaserver restart
fi

Thanks for your suggestions. This is far from an elegant solution, but as I'm pressed for time, it's a solution.

0

You can use this snippet I found here. You'd put in the IP address you're looking for, and then check the status code against the values found here. If get_status_code returns a code 200, you have remote access.

import httplib

def get_status_code(host, path="/"):
    """ This function retreives the status code of a website by requesting
        HEAD data from the host. This means that it only requests the headers.
        If the host cannot be reached or something else goes wrong, it returns
        None instead.
    """
    try:
        conn = httplib.HTTPConnection(host)
        conn.request("HEAD", path)
        return conn.getresponse().status
    except StandardError:
        return None
Community
  • 1
  • 1
Greg
  • 1,845
  • 2
  • 16
  • 26