Jump to content

Emby update script for Ubuntu


Recommended Posts

Posted (edited)

Hi all,

 

I built a script that can be used to automatically upgrade Emby server when a new package is made available via apt-get. See steps below for setting up the script and a cronjob to automatically run it at 2:30am nightly.

 

1. Create a directory for the script

mkdir -p ~/scripts/updateEmby/

2. Create the script file (using nano)

nano ~/scripts/updateEmby/updateEmby.sh

3. Paste in the following code and save the file (ctrl-o keystroke to save)

#!/bin/bash

logfile="$(dirname $0)/updateEmby.log"

# Relaunch as root if user didn't use "sudo"
[ `whoami` = root ] || exec sudo $0 "$@"

# Get the current service status
function service_status() {
  if service emby-server status > /dev/null 2>&1; then
    result="Running"
  else
    result="Stopped"
  fi
  echo "$result"
}

# Start the service
function service_start() {
  for i in {1..3}; do
    if [ $i -gt 1 ]; then
      sleep 2
    fi
    if [ "$(service_status)" == "Stopped" ]; then
      service emby-server start > /dev/null 2>&1
    else
      break
    fi
  done
}

# Stop the service
function service_stop() {
  for i in {1..3}; do
    if [ $i -gt 1 ]; then
      sleep 2
    fi
    if [ "$(service_status)" == "Running" ]; then
      service emby-server stop > /dev/null 2>&1
    else
      break
    fi
  done
}


# Update repository package lists
echo "$(date +%Y-%m-%dT%H:%M:%S-%3N) Updating repository package lists" >> $logfile
apt-get update > /dev/null 2>&1

# Get the installed and candidatee package information
output=$(apt-cache policy emby-server 2>&1)
installedVers=$(echo "$output" | grep "Installed:" | awk '{ print $2 }')
candidateVers=$(echo "$output" | grep "Candidate:" | awk '{ print $2 }')

# If Emby Server isn't installed, exit
if [ "$installedVers" == "(none)" ]; then
  echo "$(date +%Y-%m-%dT%H:%M:%S-%3N) Error!! Emby Server is not installed" >> $logfile
# If Emby Server is already up-to-date, exit
elif [ "$installedVers" == "$candidateVers" ]; then
  echo "$(date +%Y-%m-%dT%H:%M:%S-%3N) Emby Server is already up-to-date" >> $logfile
# Upgrade Emby Server
else
  echo "$(date +%Y-%m-%dT%H:%M:%S-%3N) Upgrading Emby Server to version $candidateVers" >> $logfile
  service_stop
  apt-get -y install --only-upgrade emby-server > /dev/null 2>&1
  service_start
  echo "$(date +%Y-%m-%dT%H:%M:%S-%3N) Done." >> $logfile
fi

# Done
exit

4. Make the script executable

chmod +x ~/scripts/updateEmby/updateEmby.sh

5. Create the log file

touch ~/scripts/updateEmby/updateEmby.log

6. Edit the root crontab file (Note: The script must be executed w/ elevated permissions to perform the package installation)

sudo crontab -e

7. Paste in the following line and save the file (Note: replace /home/sysadmin w/ your user's home directory path)

30 2 * * * /home/sysadmin/scripts/updateEmby/updateEmby.sh

Enjoy!

 

- tret

Edited by tret
Posted

Well done.

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...