I’m sure many of you know of Plex Media Server (PMS) and how awesome it can be for letting your kids watch your movies on the go. It likely needs no introduction, but if you’d like to learn more, please click Plex Media Server to be taken to their site.
In my home lab running ESXi through my VMUG Advantage EVALexperience (shameless plug, I know), I have an ubuntu VM I built specifically for PMS with 2 vCPUs & 4GB of RAM.
I then set up the mounts for my Synology NAS where my movie folders are so they’re mounted at boot, installed PMS, configured libraries, did some customizations, and BOOM! Kids’ movies on my phone on the go!
Sounds awesome! Why are you writing a post?
After a week or so, the web interface showed my server was out of date.
I had to manually download the file and basically reinstall PMS. It’s not that bad, it saves all settings, just tedious to do. “Why not automate it?” I thought to myself. Now, as you know, my background is Windows, as evident by my PowerShell & PowerCLI scripts, among other things. I’m a scripter/coder by nature, so jumping over to a *nix platform was kind of exiting.
I did some googling and found a script on github that was basically what I was looking for, but 1) was very long; and 2) required my username & password in the script. I’m sorry, but I don’t put my user/pass in a script, even if it’s as trivial as logging into Plex’s website to download the latest version.
I put on my scripting hat and went to work!
What does it do, and how does it do it?
I wanted something that I could create a cron job for, a script that would download the latest version and install for me. I set out on my quest and found it was actually fairly easy. I use the download link provided by PMS, but had to learn how to use awk to get my version numbers. The hardest part for me was comparing version numbers. It wasn’t like PowerShell where you can simply tag numbers as version with a -gt to evaluate true or false, but I did find a very simple way.
In short, the script downloads the latest version in the channel (stable, beta, etc) & distro (windows, linux, mac, etc) you’re currently configured for, then compares that version to the currently installed version, and only updates if the downloaded version is newer. If it’s older, it deletes the file.
If you copy that link “Please install manually”, you’ll need that url: https://plex.tv/downloads/latest/1?channel=16&build=linux-ubuntu-x86_64&distro=ubuntu&X-Plex-Token=removed
I removed my token for obvious reasons. Save the URL with the token, you’ll need it in the script.
The script is very simple. It uses wget to download the latest PMS as plex.deb. From there it gets the version of that package, gets the version that’s installed, compares the two, installs if the downloaded version is newer, or deletes the plex.deb file if the downloaded version is not newer.
**UPDATE** March 22, 2016
The script works fine as a daily cron job, but unfortunately, I had zero visibility into what it was doing. I decided to add some logging to it so I can check up and see what it’s doing. I think daily is a bit much, so I may change it to something like Sunday & Wednesday. One thing I want to point out is you MUST have the if (($? < 1)) statement immediately after the dpkg command, otherwise it will change the value of $?. Anyway, the script below has the updated logging output.
The output from the log will look like this:
# Tue Mar 22 19:08:00 CDT 2016
# downloading plex.deb
# comparing versions
# currently installed version is 0.9.16.3.1840
# downloaded version is 0.9.16.3.1840
# 0.9.16.3.1840 is not greater than 0.9.16.3.1840
# deleting downloaded package
So here’s the script:
**UPDATE** April 18, 2016
I’ve moved the script over to github for easier change tracking: https://github.com/ThepHuck/ThepHuck/tree/master/PlexUpdater
I’ve also added functionality to check the api of the local server to see if anything is being played before running the update. Head on over there and check it out!