Check if Plex is running, restart if not?

Plex allows you to aggregate all your personal media and access it anywhere you go. Enjoy your own media on all your devices with the Plex Media Server.
User avatar
OneCD
Guru
Posts: 12155
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: Check if Plex is running, restart if not?

Post by OneCD »

chrisonnas wrote: Sun Nov 21, 2021 5:04 pm For some reason it works from the # shell prompt and not from crontab. Any suggestions?
Can you please post your crontab entry here?

ImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImage
chrisonnas
Getting the hang of things
Posts: 50
Joined: Sat Jan 05, 2019 5:27 pm

Re: Check if Plex is running, restart if not?

Post by chrisonnas »

Yep, here it is - set to run every 10 mins rather than 5:

Code: Select all

*/10 * * * * /sbin/curl --silent --insecure --fail --max-time 10 http://127.0.0.1:32400; [[ $? -ne 22 ]] && /etc/init.d/plex.sh restart >/dev/null 2>&1
This command runs as expected in a terminal window, but restarts plex every 10 mins with cron...
User avatar
OneCD
Guru
Posts: 12155
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: Check if Plex is running, restart if not?

Post by OneCD »

chrisonnas wrote: Tue Nov 23, 2021 4:01 pm This command runs as expected in a terminal window, but restarts plex every 10 mins with cron...
Can you please confirm the exit code after running curl on an operational Plex instance?

Code: Select all

/sbin/curl --silent --insecure --fail --max-time 10 http://127.0.0.1:32400; echo $?

ImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImage
chrisonnas
Getting the hang of things
Posts: 50
Joined: Sat Jan 05, 2019 5:27 pm

Re: Check if Plex is running, restart if not?

Post by chrisonnas »

Yep, sure - I just ran it (wihout "--silent") and got an error code [7] as PMS isnt running properly (confirmed with plexamp app etc):

Code: Select all

curl: (7) Failed to connect to 127.0.0.1 port 32400: Connection refused
7
Running the following succesfully restarted PMS:

Code: Select all

/sbin/curl --silent --insecure --fail --max-time 10 http://127.0.0.1:32400; [[ $? -ne 22 ]] && /etc/init.d/plex.sh restart >/dev/null
Now PMS is running, I get the [22] code returned:

Code: Select all

# /sbin/curl --silent  --insecure --fail --max-time 10 http://127.0.0.1:32400; echo $?
22
User avatar
OneCD
Guru
Posts: 12155
Joined: Sun Aug 21, 2016 10:48 am
Location: "... there, behind that sofa!"

Re: Check if Plex is running, restart if not?

Post by OneCD »

Do you get the same return code when using the —silent parameter?

Maybe you’ll need to increase the check frequency, like the OP of this topic did?

ImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImageImage
chrisonnas
Getting the hang of things
Posts: 50
Joined: Sat Jan 05, 2019 5:27 pm

Re: Check if Plex is running, restart if not?

Post by chrisonnas »

OneCD wrote: Wed Nov 24, 2021 1:47 pm Do you get the same return code when using the —silent parameter?
Yes, the return codes are working at the terminal, checked with "echo $?", but assume that cron is seeing something different. I was wondering if cron is blocked from using curl or its environment generates a different code. After PMS died yesterday and I saw that return code [7] was given, I have changed the logic in the crontab to read

Code: Select all

[[ $? = 7 ]]
Hope that cron gets this - been running via cron every 10 minutes overnight and it doesnt restart PMS unecessarily. I'll check the QTS notification centre for Plex restart messages, once I see them then I know it works.
OneCD wrote: Wed Nov 24, 2021 1:47 pm Maybe you’ll need to increase the check frequency, like the OP of this topic did?
If I did that with the [[ $? -ne 22 ]] then it would be restarting every 5 minutes!
holger_kuehn
Easy as a breeze
Posts: 413
Joined: Sun Oct 20, 2013 11:45 pm
Location: Premnitz, Germany

Re: Check if Plex is running, restart if not?

Post by holger_kuehn »

you could write the return code to a file and check this while it's running via cron, just to see if execution via cron returns something unexpected
NAS (production): TS-1635AX FW: QTS 5.1.4.2596 build 20231128
NAS (backup): TS-1635AX FW: QTS 5.1.4.2596 build 20231128
QTS (SSD): [RAID-1] 2 x 2TB Samsung Evo 860 M.2-Sata
Data (QTier): [RAID-6] 4 x 4TB Samsung 870 QVO Sata
Data (HDD): [RAID-6] 7 x 18TB Exos
RAM: 8 GB (QNAP shipped)
UPS: CyberPower CP900EPFCLCD
BACKUP: 10x4TB WD Red using a USB 3.0 Dock
Usage: SMB with rclone (encrypted)

NAS: TS-873U-RP FW: QTS 5.1.4.2596 build 20231128
Data (SSD): [RAID-10] 4 x 1TB Samsung Evo 860 Sata
RAM: 8 GB (QNAP shipped)
UPS: CyberPower PR2200ELCDRT2U
BACKUP: 4TB Synology DS214 FW: DSM 7.0.41890
Usage: SMB, Backup Domain Controller
chrisonnas
Getting the hang of things
Posts: 50
Joined: Sat Jan 05, 2019 5:27 pm

Re: Check if Plex is running, restart if not?

Post by chrisonnas »

holger_kuehn wrote: Wed Nov 24, 2021 10:39 pm you could write the return code to a file and check this while it's running via cron, just to see if execution via cron returns something unexpected
odd. the file says 22!
I've left my crontab entry looking for a code [7] and lets see if that picks up PMS breaking. It was happening nearly daily, so hopefully I will find out soon.
Post Reply

Return to “Plex Media Server”