Script to monitor Plex Process and Loopback

Server Version#: 1.32.5.7210
Player Version#:

TL;DR - Have you made a script or would you like to proofread a script to monitor Plex Media Server.exe process and loopback address?

I’m curious if anyone else has experience creating a script to monitor the Plex Process or loopback channel?

A little back story, I am setting up my server to shutdown automatically at 4:00am and turn back on at 6:00am. There have been a few times for one reason or another that the service doesn’t seem to be on or accessible remotely. I worked with ChatGPT to create the following script to monitor the process and loopback automatically.

Has anyone else developed something like this? If not and you’re script savvy, would you mind looking over the below code?

@echo off
setlocal enabledelayedexpansion

REM Set the path for the log file
set log_file=monitor_log.txt

:monitor
timeout /t 10 >nul 2>&1

REM Check if Plex Media Server process is running
tasklist /fi "IMAGENAME eq Plex Media Server.exe" | findstr /i "Plex Media Server.exe" >nul 2>&1
if not errorlevel 1 (
    echo %date% %time% - Plex Media Server process is running. >> %log_file%
) else (
    echo %date% %time% - Plex Media Server process is not running. Starting the process... >> %log_file%
    start "" "C:\Path\to\Plex Media Server.exe" >> %log_file%
    echo %date% %time% - Plex Media Server process has been started. >> %log_file%
)

REM Check if the loopback address with port 32400 is reachable
ping -n 1 127.0.0.1:32400 >nul 2>&1
if not errorlevel 1 (
    echo %date% %time% - Loopback address with port 32400 is reachable. >> %log_file%
) else (
    echo %date% %time% - Loopback address with port 32400 is not reachable. Restarting the process... >> %log_file%
    taskkill /f /im "Plex Media Server.exe" >nul 2>&1
    timeout /t 5 >nul 2>&1
    start "" "C:\Path\to\Plex Media Server.exe" >> %log_file%
    echo %date% %time% - Plex Media Server process has been restarted. >> %log_file%
)

endlocal
goto monitor

Furthermore, I am aware that there are some variables / strings that would need to be altered to fit my environment.

Thanks in advance!

Dumb question: Why are you shutting down the server from 4 - 6 Am?

This is the time of day where it is least used. This gives the device time to power off, apply updates if needed, etc. I don’t like having my devices running 24/7/365

Just to clarify; the intent is to monitor the connection status during the 22 hours of “up” time and restart the process automatically if needed.

Oh yeah, Windows, where you have to reboot all the time :rofl:. My PMS is on Synology which is Linux based. Aside from the occasional Synology updates it doesn’t need to reboot. I’ve had uptimes of 90 days before Synology drops an update on me necessitating a reboot. All my machines are Linux based and stay up 24/7/365 unless a reboot is absolutely necessary (and these can often be avoided on Linux too - Windows and Mac suffer from the “can’t replace a file that’s opened by another process”. Linux doesn’t have that problem).

It seems to me you might also want to try to determine if a reboot is really needed before just rebooting your system (powershell - How can I check for a pending reboot? - Stack Overflow).

Software like this goes usually under the name “watchdog”. There are quite a few out there.

1 Like

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