Script to Auto Update Plex on Synology nas [rev6]

Hi all,
this is my script that runs every night to auto update PMS Plex Pass on my Synology.
maybe someone want it:

EDIT

i have published a GitHub

Thanks to all!

M.

#!/bin/bash
mkdir -p /tmp/plex/ > /dev/null 2>&1
token=$(cat /volume1/Plex/Library/Application\ Support/Plex\ Media\ Server/Preferences.xml | grep -oP 'PlexOnlineToken="\K[^"]+')
url=$(echo "https://plex.tv/api/downloads/5.json?channel=plexpass&X-Plex-Token=$token")
jq=$(curl -s ${url})
newversion=$(echo $jq | jq -r .nas.Synology.version)
echo New Ver: $newversion
curversion=$(synopkg version "Plex Media Server")
echo Cur Ver: $curversion
if [ "$newversion" != "$curversion" ]
then
echo New Vers Available
/usr/syno/bin/synonotify PKGHasUpgrade '{"[%HOSTNAME%]": $(hostname), "[%OSNAME%]": "Synology", "[%PKG_HAS_UPDATE%]": "Plex", "[%COMPANY_NAME%]": "Synology"}'
CPU=$(uname -m)
url=$(echo "${jq}" | jq -r '.nas.Synology.releases[] | select(.build=="linux-'"${CPU}"'") | .url')
/bin/wget $url -P /tmp/plex/
/usr/syno/bin/synopkg install /tmp/plex/*.spk
sleep 30
/usr/syno/bin/synopkg start "Plex Media Server"
rm -rf /tmp/plex/*
else
echo No New Ver
fi
exit

crontab:
cat /etc/crontab
0 3 * * * root /volume1/Script/plexupdate.sh 2>&1 &

rev1 fix
rev2 notification
rev3 cpu based url (x86 or x32)
rev4 extract token in better way
rev5 compatible with all CPU
rev6 typo

IMPORTANT:
Change the Trust Level to Any publisher in Package Center > Settings

14 Likes

Nice script.

You can also make it easy for the lesser skilled at the shell by placing it in Task Scheduler.

I like this! I wonder if an email or notification can be added so you know when it updates.

Do you prefer telegram notification?
Do you have telegram bot?

I have implementer my own script with telegram notification (I prefer instead of receiving email)

Thanks for this! I have been looking to do something like this for a while.

Ref Email notifications that were asked about, Synology Task Scheduler allows for enabling notifications via email either on completion of script. Could get a bit much getting an email every day though? I have set this script to run once a week with notifications on which is suitable for me.

I Know but via script I can send notification only when update successfully (and not on every scheduled task run).

added notification on success with

/usr/syno/bin/synonotify PKGHasUpgrade ‘{"[%HOSTNAME%]": $(hostname), “[%OSNAME%]”: “Synology”, “[%PKG_HAS_UPDATE%]”: “Plex”, “[%COMPANY_NAME%]”: “synology”}’

1 Like

Fixed, added Plex certificate to Synology package center, https://support.plex.tv/articles/205165858-how-to-add-plex-s-package-signing-public-key-to-synology-nas-package-center/


Any help, I get this error

./plexupdate.sh 
New Ver: 1.18.0.1944-f2cae8d6b
Cur Ver: 1.18.0.1906-0c0e93faa
New Vers Avaiable
--2019-10-22 11:16:07--  https://downloads.plex.tv/plex-media-server-new/1.18.0.1944-f2cae8d6b/synology/PlexMediaServer-1.18.0.1944-f2cae8d6b-x86_64.spk
Resolving downloads.plex.tv... 104.18.156.41, 104.18.157.41, 2606:4700::6812:9d29, ...
Connecting to downloads.plex.tv|104.18.156.41|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 102174720 (97M) [binary/octet-stream]
Saving to: '/volume2/tmp/plex/PlexMediaServer-1.18.0.1944-f2cae8d6b-x86_64.spk'

PlexMediaServer-1.18.0.1944-f2cae8d6b-x86_ 100%[=====================================================================================>]  97.44M  48.2MB/s    in 2.0s    

2019-10-22 11:16:09 (48.2 MB/s) - '/volume2/tmp/plex/PlexMediaServer-1.18.0.1944-f2cae8d6b-x86_64.spk' saved [102174720/102174720]

Failed to install package /volume2/tmp/plex/PlexMediaServer-1.18.0.1944-f2cae8d6b-x86_64.spk, error = [289]
package Plex Media Server start successfully

okay error not about my script :blush:

rev3 add cpu controll for x64 or x32 bit download url

cat /volume2/Plex/Library/Application\ Support/Plex\ Media\ Server/Preferences.xml | grep PlexOnlineToken | awk -F'"' '{ print $26 }'
is just echoing “2” for me and not my token.

maybe this als alternative

cat /volume2/Plex/Library/Application\ Support/Plex\ Media\ Server/Preferences.xml | grep PlexOnlineToken | awk -F'PlexOnlineToken=' '{ print $2 }' | awk -F '"' '{ print $2 }'

fix on rev 4

What email address does this notification send to?

Thank you for this! Just a small fix: but maybe you fix your typo’s like: Avaiable

it use the Notification setting on Synology

done!!

I was linked to this by a friend as well as being shown on my Google Now feed, thank you @Martino!

It looks like its running just fine on my Synology DS1019+, but I set the task to disabled for now until the next PLEX release so I can manually test it and confirm its working, but if anyone is interested in deploying it, here is what I did:

---Synology Console---
Install NANO via Package Center (I use SynoCommunity)
Add Plex Certificate to Synology Package Center (https://support.plex.tv/articles/205165858-how-to-add-plex-s-package-signing-public-key-to-synology-nas-package-center/)
Enable Notifications on Synology (Control Panel ... Notification)
Enable SSH (Control Panel ... Terminal & SNMP ... Enable SSH Service)

---SSH into Synology---
#Comment: Login with Administrator (or your Administrator account you created)
sudo -i
#Comment: Enter root password (usually Administrator password)
cd /volume1
mkdir Scripts
cd Scripts
nano plexupdate.sh

#Comment: Paste text into nano screen, no changes should need to be made

#!/bin/bash
mkdir /volume1/plextemp/ > /dev/null 2>&1
token=$(cat /volume1/Plex/Library/Application\ Support/Plex\ Media\ Server/Preferences.xml | grep -oP 'PlexOnlineToken="\K[^"]+')
url=$(echo "https://plex.tv/api/downloads/5.json?channel=plexpass&X-Plex-Token=$token")
jq=$(curl -s ${url})
newversion=$(echo $jq | jq -r .nas.Synology.version)
echo New Ver: $newversion
curversion=$(synopkg version "Plex Media Server")
echo Cur Ver: $curversion
if [ "$newversion" != "$curversion" ]
then
echo New Vers Available
/usr/syno/bin/synonotify PKGHasUpgrade '{"[%HOSTNAME%]": $(hostname), "[%OSNAME%]": "Synology", "[%PKG_HAS_UPDATE%]": "Plex", "[%COMPANY_NAME%]": "Synology"}'
cpu=$(uname -m)
if [ "$cpu" = "x86_64" ]; then
url=$(echo $jq | jq -r ".nas.Synology.releases[1] | .url")
else
 url=$(echo $jq | jq -r ".nas.Synology.releases[0] | .url")
fi
/bin/wget $url -P /volume1/tmp/plex/
/usr/syno/bin/synopkg install /volume1/tmp/plex/*.spk
sleep 30
/usr/syno/bin/synopkg start "Plex Media Server"
rm -rf /volume1/tmp/plex/*
else
echo No New Ver
fi
exit

#Comment: Save file, CTRL + O and ENTER
#Comment: Close file, CTRL + X

bash plexupdate.sh

#Comment: Should show No New Ver

---Setup Update Scheduler---
#Comment: Go back to Synology Console
Open Control Panel
Open Task Scheduler
Click Create ... Scheduled Task ... User-defined script
Enter Task as Update Plex
Click Schedule Tab
Change Schedule to fit needs
Click Task Settings Tab
Enter User-defined script as bash /volume1/Scripts/plexupdate.sh
Click OK

#Comment: I unchecked the new script UNTIL I verify it works (next Plex version release)
3 Likes

Thanks man, I love this!
This was exactly what I wanted for a while now. Keep up the good work.

1 Like

Thanks for doing this, I too have been wanting something like this for a long time.

But I too am getting the same [289] install error using the Task Scheduler and root permissions. Mine is currently outdated and gets the same error when trying to install the latest version.

I’m not real smart on this stuff, any ideas?

try this