So, every once and a while I will get in bed, turn on my TV and go to watch some mundane reruns of a show before I go to sleep. But it doesn’t work, my Plex server has crashed. I’m too lazy to figure out why this happened, so I simply restart the program, go back to bed and watch TV.
It happened to me again tonight. I’m still too lazy to figure out why it happens. I’ll go months without the problem, and sometimes it happens twice a week. With rolling releases, who has the time? It could be the same problem reoccurring, or a new one causing the crash. IDK.
In any case, I asked OpenAI Chat ChatGPT (openai.com) to help me. The AI wrote this script for Windows. I just made the batch file, copied the text over and set it to start on Startup. Here comes John Connors…
@echo off
rem Set the directory where the log file will be saved
set logdir=C:\plex_monitor
rem Create the log directory if it does not exist
if not exist "%logdir%" mkdir "%logdir%"
:loop
rem Get the current date and time
for /f "tokens=1-6 delims= " %%a in ('date /t') do set date=%%a%%b%%c
for /f "tokens=1-3 delims=:" %%a in ('time /t') do set time=%%a%%b%%c
rem Check if the Plex Media Server is running
tasklist | find "Plex Media Server" > nul
if errorlevel 1 (
rem The Plex Media Server is not running
rem Check if the Plex Media Server.exe process is running
tasklist | find "Plex Media Server.exe" > nul
if errorlevel 1 (
rem The Plex Media Server.exe process is not running
echo %date% %time% Plex Media Server is not running. Will relaunch it now. >> "%logdir%\plex_monitor.txt"
rem Relaunch the Plex Media Server
start "Plex Media Server" "C:\Program Files\Plex\Plex Media Server\Plex Media Server.exe"
echo %date% %time% Plex Media Server has been relaunched. >> "%logdir%\plex_monitor.txt"
)
)
rem Wait for 5 minutes before checking if the Plex Media Server is still running
timeout /t 300
rem Loop back to the beginning of the script
goto loop
What’s scarier, is that I then asked it to re-write the script in Python. and it did… I haven’t tested the Python version, but the batch file works fine
import os
import time
import subprocess
# Set the directory where the log file will be saved
logdir = r"C:\plex_monitor"
# Create the log directory if it does not exist
if not os.path.exists(logdir):
os.mkdir(logdir)
while True:
# Get the current date and time
date = time.strftime("%Y%m%d")
curr_time = time.strftime("%H%M%S")
# Check if the Plex Media Server is running
result = subprocess.run(["tasklist"], stdout=subprocess.PIPE, encoding="utf-8")
if "Plex Media Server" not in result.stdout:
# The Plex Media Server is not running
# Check if the Plex Media Server.exe process is running
result = subprocess.run(["tasklist"], stdout=subprocess.PIPE, encoding="utf-8")
if "Plex Media Server.exe" not in result.stdout:
# The Plex Media Server.exe process is not running
with open(os.path.join(logdir, "plex_monitor.txt"), "a") as f:
f.write(f"{date} {curr_time} Plex Media Server is not running. Will relaunch it now.\n")
# Relaunch the Plex Media Server
subprocess.run(["start", "Plex Media Server", r"C:\Program Files\Plex\Plex Media Server\Plex Media Server.exe"])
with open(os.path.join(logdir, "plex_monitor.txt"), "a") as f:
f.write(f"{date} {curr_time} Plex Media Server has been relaunched.\n")
# Wait for 5 minutes before checking if the Plex Media Server is still running
time.sleep(300)
Finally, and this is going to give me nightmares…
I asked it to re-qrite the script to support both Windows and Ubuntu Linux. The only information I gave it was to change the output directory of the log file…
import os
import time
import subprocess
# Set the directory where the log file will be saved
if os.name == "nt": # Windows
logdir = r"C:\plex_monitor"
else: # Linux
logdir = r"/var/log/plex_monitor"
# Create the log directory if it does not exist
if not os.path.exists(logdir):
os.mkdir(logdir)
while True:
# Get the current date and time
date = time.strftime("%Y%m%d")
curr_time = time.strftime("%H%M%S")
# Check if the Plex Media Server is running
if os.name == "nt": # Windows
result = subprocess.run(["tasklist"], stdout=subprocess.PIPE, encoding="utf-8")
if "Plex Media Server" not in result.stdout:
# The Plex Media Server is not running
# Check if the Plex Media Server.exe process is running
result = subprocess.run(["tasklist"], stdout=subprocess.PIPE, encoding="utf-8")
if "Plex Media Server.exe" not in result.stdout:
# The Plex Media Server.exe process is not running
with open(os.path.join(logdir, "plex_monitor.txt"), "a") as f:
f.write(f"{date} {curr_time} Plex Media Server is not running. Will relaunch it now.\n")
# Relaunch the Plex Media Server
subprocess.run(["start", "Plex Media Server", r"C:\Program Files\Plex\Plex Media Server\Plex Media Server.exe"])
with open(os.path.join(logdir, "plex_monitor.txt"), "a") as f:
f.write(f"{date} {curr_time} Plex Media Server has been relaunched.\n")
else: # Linux
result = subprocess.run(["ps", "-aux"], stdout=subprocess.PIPE, encoding="utf-8")
if "plexmediaserver" not in result.stdout:
# The Plex Media Server is not running
with open(os.path.join(logdir, "plex_monitor.txt"), "a") as f:
f.write(f"{date} {curr_time} Plex Media Server is not running. Will relaunch it now.\n")
# Relaunch the Plex Media Server
subprocess.run(["service", "plexmediaserver", "start"])
with open(os.path.join(logdir, "plex_monitor.txt"), "a") as f:
f.write(f"{date} {curr_time} Plex Media Server has been relaunched.\n")
# Wait for 5 minutes before checking if the Plex Media Server is still running
time.sleep(300)