Script to Auto Update Plex on Synology nas [rev6]

Works like a charm. thank you very much.

I seem to get the following error message with this script:

Blockquote
Failed to install package /volume1/tmp/plex/PlexMediaServer-1.18.5.2309-f5213a238-x86.spk, error = [258]

This is the complete output log.

output.log (145.7 KB)

As far as I can tell it is downloading the wrong installer since I’m not on an x86 cpu but arm7 (DS 218j). Any idea how to fix this?

@MPH_91 Try this script and let me know if it works for you:

#!/bin/bash

# restrict script execution to root
if [[ ${EUID} -ne 0 ]]; then
  echo "Must be run as root. Exiting script..."
  exit 1
fi

# configurable download path
TMP_DL_PATH='/tmp'

#######################################
# Reports action status and removes
# downloaded installer file.
# Globals:
#   TMP_DL_PATH
# Arguments:
#   Exit status, an integer
#   Action, a string
# Outputs:
#   Writes action and status to stdout
#######################################
check_status() {

  if [[ $1 -ne 0 ]]; then
    echo "$2 failed. Exiting script..."
    rm -f "${TMP_DL_PATH}"/"${pms_pkg}" 2>/dev/null
    exit 1
  else
    echo "$2 succeeded."
  fi

}

# get pms path, token, and package info
pms_path=$(find / -path '*/Plex Media Server/Preferences.xml' -print -quit 2>/dev/null)
pms_token=$(sed -n 's/.*PlexOnlineToken="\([^"]*\).*/\1/p' "${pms_path}")
pms_info=$(curl -s "https://plex.tv/api/downloads/5.json?channel=plexpass&X-Plex-Token=${pms_token}")

# check if pms_token is defined
if [[ -z "${pms_token}" ]]; then
  echo "Unable to locate PMS Token. Exiting script..."
  exit 1
fi

# get current, new, and greater versions
cur_version=$(synopkg version "Plex Media Server")
new_version=$(jq -r '.nas.Synology.version' <<< "${pms_info}")
gtr_version=$(echo -e "${cur_version}\n${new_version}" | sort -V | tail -1)

# compare package versions
if [[ "${gtr_version}" == "${cur_version}" ]]; then
  echo "Plex Media Server is up-to-date."
else

  # notify new version available
  echo "Plex Media Server version ${new_version} is available..."
  synonotify PKGHasUpgrade '{"[%HOSTNAME%]": $(hostname), "[%OSNAME%]": "Synology", "[%PKG_HAS_UPDATE%]": "Plex", "[%COMPANY_NAME%]": "Synology"}'

  # get model and cpu architecture info
  model=$(cat /proc/sys/kernel/syno_hw_version)
  cpu=$(uname -m)

  # check if cpu architecture is armv7
  if [[ "${cpu}" == "armv7"* ]]; then
    # filter rules based on model definitions outlined here:
    # curl -s 'https://plex.tv/api/downloads/5.json?channel=plexpass' | jq '.nas.Synology.releases[] | select(.label | contains("ARMv7"))'
    if [[ ${model//[^0-9]/} =~ 1[34]$ && \
          "${model}" != "DS414j"* ]] || \
       [[ "${model}" =~ ^(DS115j|RS815|DS216se) ]] ; then
      arch="armv7hf"
    elif [[ ${model//[^0-9]/} =~ 1[5-8]$ ]]; then
      arch="armv7hf_neon"
    fi
  # otherwise assign value of cpu to arch
  else
    arch="${cpu}"
  fi

  # get download url
  pms_dl_url=$(jq -r '.nas.Synology.releases[] | select(.build=="linux-'"${arch}"'") | .url' <<< "${pms_info}")
  pms_pkg="${pms_dl_url##*/}"

  # check if pms_dl_url is defined
  if [[ -z "${pms_dl_url}" ]]; then
    echo "Download URL not defined because CPU type is ${arch}. Exiting script..."
    exit 1
  fi

  # download file to TMP_DL_PATH
  echo "Downloading ${pms_pkg}..."
  mkdir -p "${TMP_DL_PATH}"
  wget -P "${TMP_DL_PATH}" "${pms_dl_url}"
  check_status $? "Download"

  # install package
  echo "Installing ${pms_pkg}..."
  synopkg install "${TMP_DL_PATH}/${pms_pkg}"
  check_status $? "Install"

  # start plexmediaserver
  echo "Starting Plex Media Server..."
  synopkg start "Plex Media Server"
  check_status $? "Startup"

  # remove package
  rm -f "${TMP_DL_PATH}/${pms_pkg}" 2>/dev/null

fi

First of all thanks for your help!

Unfortunately when I execute the script it outputs this error message:

Plex Media Server version 1.18.7.2438-f342a5a43 is available…
Downloading …
http://: Invalid host name.
Download failed. Exiting script…

@MPH_91 Can you paste the output of the following commands?

jq --version
jq -r '.nas.Synology.releases[] | select(.build=="linux-'"$(uname -m)"'") | .url' <<< "${pms_info}"

I suspect that your version of jq does not support select.

I executed your command and it only ouputs this

jq-1.5

Was I supposed to execute just the 2 lines or should I add it somewhere into your previous code?

My bad @MPH_91. Can you run the script with bash -x and paste the output?

So basically ssh into the NAS and then execute this:

bash -x

jq --version

jq -r ‘.nas.Synology.releases | select(.build=="linux-’"(uname -m)"'") | .url' <<< "{pms_info}"

Correct?

Actually, I need you to execute the entire script like so:

bash -x [script_name]

Then paste the output so I can debug any issues when run on your NAS.

Hey guys, thanks for the amazing work with this. I’m having a issue with it however.
output:

Usage: grep [OPTION]… PATTERN [FILE]…
Try ‘grep --help’ for more information.
plexupdate.sh: line 13: -oP: command not found
plexupdate.sh: line 13: X-Plex-Token=: command not found
New version:
Current version: 1.18.8.2461-03ad9abb4
New version available!
wget: missing URL
Usage: wget [OPTION]… [URL]…

Try `wget --help’ for more options.
Failed to install package /tmp/plex/*.spk, error = [150]
package Plex Media Server start successfully

Not exactly sure where i went wrong.

Thanks in advance

@craft211 Can you try running the script I pasted in this reply?

Thanks for testing testing @MPH_91. I updated my post with the full script and added changes.

Script updated to grab the PlexOnlineToken value from local PMS Preferences.xml file, add the X-Plex-Token parameter to the API call for PMS package version, and fixed the logic for comparing the installed PMS version to the latest available version.

1 Like

Shouldn’t

mkdir /volume1/plextemp/ > /dev/null 2>&1

be

mkdir /volume1/tmp/plex/ > /dev/null 2>&1

@euphnutz

I tried your script and get:
Failed to install package /tmp/PlexMediaServer-1.18.9.2571-e106a8a91-x86_64.spk, error = [289]

Any suggestion?

Edit: Fixed: Changing the trust level in package center to any publisher fixed that error.

1 Like

Hi, all. Like many of you, I also wanted Plex Media Server to auto-update, and I stumbled across this thread. I took a look at all the existing plex auto-update scripts people have written, and decided to write my own:

My goals were to:

  • make the echoed messages super clear
  • make the version checking logic as smart as possible
  • make sure the script fails if there are any errors
  • ensure temp files are cleaned up properly
  • write bash code as idiomatically as possible
  • attempt to find the “Plex Media Server” directory that contains Preferences.xml efficiently
  • attempt to support all NAS architectures

If you have any comments or suggestions, please file an issue or PR over at github (I probably won’t respond to bug reports or change requests made here).

Thanks!

7 Likes

Hello,

I chose to use your script in the end as the code was easier for me to follow and I appreciated the additional features.

Your instructions were also easy to follow and it runs like a dream!

Thank you very much!

Hi. Tnx fo script. It’s cool! But, I have some error…
Plex Update - File was downloaded.

Plex Update - Installing package.

Failed to install package /tmp/PlexTempDownload/PlexMediaServer-1.19.2.2702-776106bc6-x86_64.spk, error = [273]

Plex Update - There was an error installing the package.

What I must to do to fix it?

Tnx

How it must see in script?

Glad I found this thread! I’m using the updated script from @ckbcowboy and it couldn’t have gone smoother.