# Extract specified value from override file if it exists (Null if not) GetOverride() { Retval="" # Don't know if we have pushd so do it long hand CurrDir="$(pwd)" # Find the metadata dir if customized if [ -e /etc/systemd/system/plexmediaserver.service.d ]; then # Get there cd /etc/systemd/system/plexmediaserver.service.d # Glob up all 'conf files' found ConfFile="$(find override.conf local.conf *.conf 2>/dev/null | head -1)" # If there is one, search it if [ "$ConfFile" != "" ]; then Retval="$(grep "$1" $ConfFile | head -1 | sed -e "s/.*${1}=//" | tr -d \" | tr -d \')" fi fi # Go back to where we were cd "$CurrDir" # What did we find echo "$Retval" } # Universal output function Output() { if [ $Scripted -gt 0 ]; then echo \[$(date "+%Y-%m-%d %H.%M.%S")\] "$@" else echo "$@" fi # $LOG_TOOL \[$(date "+%Y-%m-%d %H.%M.%S")\] "$@" } # Determine which host we are running on and set variables HostConfig() { # On all hosts except Mac PIDOF="pidof" STATFMT="-c" STATBYTES="%s" STATPERMS="%a" # On all hosts except QNAP DFFLAGS="-m" # Synology (DSM 7) if [ -d /var/packages/PlexMediaServer ] && \ [ -d "/var/packages/PlexMediaServer/shares/PlexMediaServer/AppData/Plex Media Server" ]; then # Where is the software PKGDIR="/var/packages/PlexMediaServer/target" PLEX_SQLITE="$PKGDIR/Plex SQLite" LOG_TOOL="logger" # Where is the data AppSuppDir="/var/packages/PlexMediaServer/shares/PlexMediaServer/AppData" DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" LOGFILE="$DBDIR/DBRepair.log" # We are done HostType="Synology (DSM 7)" # We do have start/stop as root HaveStartStop=1 StartCommand="/usr/syno/bin/synopkg start PlexMediaServer" StopCommand="/usr/syno/bin/synopkg stop PlexMediaServer" return 0 # Synology (DSM 6) elif [ -d "/var/packages/Plex Media Server" ] && \ [ -f "/usr/syno/sbin/synoshare" ]; then # Where is the software PKGDIR="/var/packages/Plex Media Server/target" PLEX_SQLITE="$PKGDIR/Plex SQLite" LOG_TOOL="logger" # Get shared folder path AppSuppDir="$(synoshare --get Plex | grep Path | awk -F\[ '{print $2}' | awk -F\] '{print $1}')" # Where is the data AppSuppDir="$AppSuppDir/Library/Application Support" if [ -d "$AppSuppDir/Plex Media Server" ]; then DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" LOGFILE="$DBDIR/DBRepair.log" HostType="Synology (DSM 6)" # We do have start/stop as root HaveStartStop=1 StartCommand="/usr/syno/bin/synopkg start PlexMediaServer" StopCommand="/usr/syno/bin/synopkg stop PlexMediaServer" return 0 fi # QNAP (QTS & QuTS) elif [ -f /etc/config/qpkg.conf ]; then # Where is the software PKGDIR="$(getcfg -f /etc/config/qpkg.conf PlexMediaServer Install_path)" PLEX_SQLITE="$PKGDIR/Plex SQLite" LOG_TOOL="/sbin/log_tool -t 0 -a" # Where is the data AppSuppDir="$PKGDIR/Library" DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" LOGFILE="$DBDIR/DBRepair.log" # Start/Stop if [ -e /etc/init.d/plex.sh ]; then HaveStartStop=1 StartCommand="/etc/init.d/plex.sh start" StopCommand="/etc/init.d/plex.sh stop" fi # Use custom DFFLAGS (force POSIX mode) DFFLAGS="-Pm" HostType="QNAP" return 0 # SNAP host (check before standard) elif [ -d "/var/snap/plexmediaserver/common/Library/Application Support/Plex Media Server" ]; then # Where things are PLEX_SQLITE="/snap/plexmediaserver/current/Plex SQLite" AppSuppDir="/var/snap/plexmediaserver/common/Library/Application Support" PID_FILE="$AppSuppDir/plexmediaserver.pid" DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" LOGFILE="$DBDIR/DBRepair.log" LOG_TOOL="logger" HaveStartStop=1 StartCommand="snap start plexmediaserver" StopCommand="snap stop plexmediaserver" HostType="SNAP" return 0 # Standard configuration Linux host elif [ -f /etc/os-release ] && \ [ -d /usr/lib/plexmediaserver ] && \ [ -d /var/lib/plexmediaserver ]; then # Where is the software PKGDIR="/usr/lib/plexmediaserver" PLEX_SQLITE="$PKGDIR/Plex SQLite" LOG_TOOL="logger" # Where is the data AppSuppDir="/var/lib/plexmediaserver/Library/Application Support" # DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" # PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" # Find the metadata dir if customized if [ -e /etc/systemd/system/plexmediaserver.service.d ]; then # Get custom AppSuppDir if specified NewSuppDir="$(GetOverride PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR)" if [ -d "$NewSuppDir" ]; then AppSuppDir="$NewSuppDir" else Output "Given application support directory override specified does not exist: '$NewSuppDir'. Ignoring." fi fi DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" LOGFILE="$DBDIR/DBRepair.log" HostType="$(grep ^PRETTY_NAME= /etc/os-release | sed -e 's/PRETTY_NAME=//' | sed -e 's/"//g')" HaveStartStop=1 StartCommand="systemctl start plexmediaserver" StopCommand="systemctl stop plexmediaserver" return 0 # Netgear ReadyNAS elif [ -e /etc/os-release ] && [ "$(cat /etc/os-release | grep ReadyNASOS)" != "" ]; then # Find PMS if [ "$(echo /apps/plexmediaserver*)" != "/apps/plexmediaserver*" ]; then PKGDIR="$(echo /apps/plexmediaserver*)" # Where is the code PLEX_SQLITE="$PKGDIR/Binaries/Plex SQLite" AppSuppDir="$PKGDIR/MediaLibrary" PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" LOGFILE="$DBDIR/DBRepair.log" LOG_TOOL="logger" HaveStartStop=1 StartCommand="systemctl start fvapp-plexmediaserver" StopCommand="systemctl stop fvapp-plexmediaserver" HostType="Netgear ReadyNAS" return 0 fi # ASUSTOR elif [ -f /etc/nas.conf ] && grep ASUSTOR /etc/nas.conf >/dev/null && \ [ -d "/volume1/Plex/Library/Plex Media Server" ]; then # Where are things PLEX_SQLITE="/volume1/.@plugins/AppCentral/plexmediaserver/Plex SQLite" AppSuppDir="/volume1/Plex/Library" PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" LOGFILE="$DBDIR/DBRepair.log" LOG_TOOL="logger" HostType="ASUSTOR" return 0 # Apple Mac elif [ -d "/Applications/Plex Media Server.app" ] && \ [ -d "$HOME/Library/Application Support/Plex Media Server" ]; then # Where is the software PLEX_SQLITE="/Applications/Plex Media Server.app/Contents/MacOS/Plex SQLite" AppSuppDir="$HOME/Library/Application Support" DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" PID_FILE="$DBDIR/dbtmp/plexmediaserver.pid" LOGFILE="$DBDIR/DBRepair.log" LOG_TOOL="logger" # MacOS uses pgrep and uses different stat options PIDOF="pgrep" STATFMT="-f" STATBYTES="%z" STATPERMS="%A" # Root not required on MacOS. PMS runs as username. RootRequired=0 # make the TMP directory in advance to store plexmediaserver.pid mkdir -p "$DBDIR/dbtmp" # Remove stale PID file if it exists [ -f "$PID_FILE" ] && rm "$PID_FILE" # If PMS is running create plexmediaserver.pid PIDVALUE=$($PIDOF "Plex Media Server") [ $PIDVALUE ] && echo $PIDVALUE > "$PID_FILE" HostType="Mac" return 0 # Western Digital (OS5) elif [ -f /etc/system.conf ] && [ -d /mnt/HD/HD_a2/Nas_Prog/plexmediaserver ] && \ grep "Western Digital Corp" /etc/system.conf >/dev/null; then # Where things are PLEX_SQLITE="/mnt/HD/HD_a2/Nas_Prog/plexmediaserver/binaries/Plex SQLite" AppSuppDir="$(echo /mnt/HD/HD*/Nas_Prog/plex_conf)" PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" LOGFILE="$DBDIR/DBRepair.log" LOG_TOOL="logger" HostType="Western Digital" return 0 # - Docker cgroup v1 & v2 # - Podman (libpod) # - Kubernetes (and TrueNAS platforms) elif [ "$(grep docker /proc/1/cgroup | wc -l)" -gt 0 ] || [ "$(grep 0::/ /proc/1/cgroup)" = "0::/" ] || [ "$(grep libpod /proc/1/cgroup | wc -l)" -gt 0 ] || [ "$(grep kube /proc/1/cgroup | wc -l)" -gt 0 ]; then # HOTIO Plex image structure is non-standard (contains symlink which breaks detection) if [ -n "$(grep -irslm 1 hotio /etc/s6-overlay/s6-rc.d)" ]; then PLEX_SQLITE=$(find /app/usr/lib/plexmediaserver /usr/lib/plexmediaserver -maxdepth 0 -type d -print -quit 2>/dev/null); PLEX_SQLITE="$PLEX_SQLITE/Plex SQLite" AppSuppDir="/config" PID_FILE="$AppSuppDir/plexmediaserver.pid" DBDIR="$AppSuppDir/Plug-in Support/Databases" LOGFILE="$DBDIR/DBRepair.log" LOG_TOOL="logger" if [ -d "/run/service/plex" ] || [ -d "/run/service/service-plex" ]; then SERVICE_PATH=$([ -d "/run/service/plex" ] && echo "/run/service/plex" || [ -d "/run/service/service-plex" ] && echo "/run/service/service-plex") HaveStartStop=1 StartCommand="s6-svc -u $SERVICE_PATH" StopCommand="s6-svc -d $SERVICE_PATH" fi HostType="HOTIO" return 0 # Docker (All main image variants except binhex and hotio) elif [ -d "/config/Library/Application Support" ]; then PLEX_SQLITE="/usr/lib/plexmediaserver/Plex SQLite" AppSuppDir="/config/Library/Application Support" PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" LOGFILE="$DBDIR/DBRepair.log" LOG_TOOL="logger" # Miscellaneous start/stop methods if [ -d "/var/run/service/svc-plex" ]; then HaveStartStop=1 StartCommand="s6-svc -u /var/run/service/svc-plex" StopCommand="s6-svc -d /var/run/service/svc-plex" fi if [ -d "/var/run/s6/services/plex" ]; then HaveStartStop=1 StartCommand="s6-svc -u /var/run/s6/services/plex" StopCommand="s6-svc -d /var/run/s6/services/plex" fi HostType="Docker" return 0 # BINHEX Plex image elif [ -e /etc/os-release ] && grep "IMAGE_ID=archlinux" /etc/os-release 1>/dev/null && \ [ -e /home/nobody/start.sh ] && grep PLEX_MEDIA /home/nobody/start.sh 1> /dev/null ; then PLEX_SQLITE="/usr/lib/plexmediaserver/Plex SQLite" AppSuppDir="/config" PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" LOGFILE="$DBDIR/DBRepair.log" LOG_TOOL="logger" HostType="BINHEX" return 0 fi # Last chance to identify this host elif [ -e /etc/os-release ]; then # Arch Linux (must check for native Arch after binhex) if [ "$(grep -E '=arch|="arch"' /etc/os-release)" != "" ] && \ [ -d /usr/lib/plexmediaserver ] && \ [ -d /var/lib/plex ]; then # Where is the software PKGDIR="/usr/lib/plexmediaserver" PLEX_SQLITE="$PKGDIR/Plex SQLite" LOG_TOOL="logger" # Where is the data AppSuppDir="/var/lib/plex" # Find the metadata dir if customized if [ -e /etc/systemd/system/plexmediaserver.service.d ]; then # Get custom AppSuppDir if specified NewSuppDir="$(GetOverride PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR)" if [ "$NewSuppDir" != "" ]; then if [ -d "$NewSuppDir" ]; then AppSuppDir="$NewSuppDir" else Output "Given application support directory override specified does not exist: '$NewSuppDir'. Ignoring." fi fi fi DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" LOGFILE="$DBDIR/DBRepair.log" LOG_TOOL="logger" HostType="$(grep PRETTY_NAME /etc/os-release | sed -e 's/^.*="//' | tr -d \" )" HaveStartStop=1 StartCommand="systemctl start plexmediaserver" StopCommand="systemctl stop plexmediaserver" return 0 fi fi # Unknown / currently unsupported host return 1 }