Monitor + Autorestart Plex Media Server Service Failure

monit-logoLinux systems make excellent media servers. Since Plex Media Server has been unofficially available on the Raspberry Pi 2, Banana Pi and other ARMv7 devices, some users have wanted to monitor the service and restart it automatically.

Monit is the perfect service for that as it has advanced probing for system services. At the basic level it monitors for the existence of process id files which daemons user to show a process is running. Monit does also let you probe ports and urls giving additional layers of stability checking. For example, the Plex Media Server pid could exist but the Plex service is frozen. By probing the port and URL we can restart Plex on the Raspberry Pi 2 if the Plex web interface is unavailable.

This monitor plex media service failure and restart automatically tutorial was tested on Ubuntu and Debian systems and is based on the official documentation. This guide will work on Plex for x64 and x86 systems too. Make sure you have Monit installed.

If you are trying to figure out which hardware would work best for you, consider reading the Pi benchmarks.

Pi Unit
Processor
RAM
RAM Bus
Network
WiFi
USB
SATA
Cost
Raspberry Pi 3
1.2 GHz ARMv8
Quad Core
1 GB DDR2
450 MHz
100 Mbit
Yes
4
No
$35
Raspberry Pi 2
900 MHz ARMv7
Quad Core
1 GB DDR2
450 MHz
100 Mbit
No
4
No
$35.00
Raspberry Pi
700 MHz ARMv6
Single Core
512 MB SDRAM
400 MHz
100 Mbit
No
4
No
$25
Banana Pi
1 GHz ARMv7
Dual Core
1 GB DDR3
432 MHz
Gigabit
No
2
Yes
$36.99
Banana Pi Pro
1 GHz ARMv7
Dual Core
1 GB DDR3
432 MHz
Gigabit
Yes
2
Yes
$45.00

Monitor and Autorestart Plex Media Server Service Failure

Figure out which ports to monitor using netstat first.

Make sure Plex is running on your device and execute this netstat command to get all listening ports on your system and filter the Plex ones.

netstat -lntp | grep -i plex

Plex Media Server runs on more than just 32400 so we will use Monit to monitor these ports

tcp        0      0 0.0.0.0:32400           0.0.0.0:*               LISTEN      15053/Plex Media Se
tcp        0      0 0.0.0.0:32401           0.0.0.0:*               LISTEN      15053/Plex Media Se
tcp        0      0 0.0.0.0:32469           0.0.0.0:*               LISTEN      15096/Plex DLNA Ser
tcp        0      0 0.0.0.0:1909            0.0.0.0:*               LISTEN      15096/Plex DLNA Ser
tcp        0      0 0.0.0.0:39293           0.0.0.0:*               LISTEN      15061/Plex Plug-in

Now open a browser and go to your local ip: https://ip.address:2812 and enter your credentials. Then go back to the SSH terminal.

Create the Plex media server on ARM monit configuration

sudo nano /etc/monit/conf.d/plex

Paste the code below, note that most of these are optional but at a minimum you will want the first 4 lines.

Here is what the Plex monit configuration does

  • Monitors the existence of the plexmediaserver.pid file, if it is no longer present plex will be restarted
  • Tests if 32400, 32401, 32469 and 1909 port is open on the loopback address 5 times and if Plex isn’t running, Monit restarts the Plex service automatically
  • Tests if the default Plex management web interface is available
check process plexmediaserver with pidfile "/var/lib/plexmediaserver/Library/Application\ Support/Plex\ Media\ Server/plexmediaserver.pid"
    start program = "/usr/sbin/service plexmediaserver start" with timeout 60 seconds
    stop program  = "/usr/sbin/service plexmediaserver stop"
    if failed host 127.0.0.1 port 32400 with timeout 30 seconds for 5 cycles then restart
    if failed port 32401 with timeout 30 seconds for 5 cycles then restart
    if failed port 32469 with timeout 30 seconds for 5 cycles then restart
    if failed port 1909 with timeout 30 seconds for 5 cycles then restart
    if failed url http://127.0.0.1:32400/web/index.html with timeout 30 seconds for 5 cycles then restart

If you have issues with the monit configuration try changing these lines to specifically use the init.d script if you are on Debian or Ubuntu.

start program = "/etc/init.d/plexmediaserver start" with timeout 60 seconds
stop program  = "/etc/init.d/plexmediaserver stop"

If you find Plex is restarting when you don’t want it to then increase the timeout in the failed port lines

if failed host 127.0.0.1 port 32400 with timeout 60 seconds for 5 cycles then restart
if failed port 32401 with timeout 60 seconds for 5 cycles then restart
if failed port 32469 with timeout 60 seconds for 5 cycles then restart
if failed port 1909 with timeout 60 seconds for 5 cycles then restart
if failed url http://127.0.0.1:32400/web/index.html with timeout 60 seconds for 5 cycles then restart

You can also check the web interface and remove any ports that are causing trouble.

A simplified Plex Monit configuration would be something like this

check process plexmediaserver with pidfile "/var/lib/plexmediaserver/Library/Application\ Support/Plex\ Media\ Server/plexmediaserver.pid"
    start program = "/usr/sbin/service plexmediaserver start" with timeout 60 seconds
    stop program  = "/usr/sbin/service plexmediaserver stop"
    if failed url http://127.0.0.1:32400/web/index.html with timeout 30 seconds for 5 cycles then restart

Check the Monit configuration has valid syntax

sudo monit -t

Should see the message below, you may also see nothing which means the syntax is ok

Control file syntax OK

Then reload Monit to activate the configuration

sudo service monit reload

Go back to the monit web interface at https://ip.address:2812

You will now see a plexmediaserver process

monit plex media server arm

Back in SSH kill the Plex process like this (thanks to Stackoverflow) assuming your Plex server runs as the plex user.

sudo pkill -u plex

Then back in the Monit web interface you will see a screen like this, wait a minute and it should pop back up as running

monit plex media server failed arm

For those of you using reverse proxies, it is easy to add Monit to nginx or Apache.

Now your Plex Media Server service will auto restart on failure on your home server running Ubuntu, Debian or other Linux system on your x86, x64, Raspberry Pi 2, Banana Pi or Orange Pi.