Issues in start_pms script

Noticed a number of minor issues in the /usr/sbin/start_pms script. Here's the original script

#!/bin/sh

#change these parameters in /etc/default/plexmediaserver
export PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS=6
export PLEX_MEDIA_SERVER_HOME=/usr/lib/plexmediaserver
export PLEX_MEDIA_SERVER_MAX_STACK_SIZE=3000
export PLEX_MEDIA_SERVER_TMPDIR=/tmp
export PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR="${HOME}/Library/Application Support"

test -f /etc/default/plexmediaserver && . /etc/default/plexmediaserver

if [ ! -d “$PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR” ]
then
mkdir -p "PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR" if [ ! ? -eq 0 ]
then
echo “WARNING COULDN’T CREATE $PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR, MAKE SURE I HAVE PERMISSON TO DO THAT!”
exit 1
fi
fi

export LD_LIBRARY_PATH="{PLEX_MEDIA_SERVER_HOME}" export TMPDIR="{PLEX_MEDIA_SERVER_TMPDIR}"

echo $PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS $PLEX_MEDIA_SERVER_MAX_STACK_SIZE $PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR

ulimit -s $PLEX_MAX_STACK_SIZE

(cd /usr/lib/plexmediaserver; ./Plex\ Media\ Server)

First off, the guide here strongly implies that you can just export variables and that will change how plex works. Not only does this guide need an update (and to perhaps include debian/ubuntu), but the script above will completely overwrite all of these variables. Here is a better alternative that actually utilizes the environment variables if the user has set them. This is posix-compliant and should work with any /bin/sh

# Setup default parameters
# Either with environmental variables, or defaults if variables are unset
export PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS=${PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS:-6}
export PLEX_MEDIA_SERVER_HOME=${PLEX_MEDIA_SERVER_HOME:-/usr/lib/plexmediaserver}
export PLEX_MEDIA_SERVER_MAX_STACK_SIZE=${PLEX_MEDIA_SERVER_MAX_STACK_SIZE:-3000}
export PLEX_MEDIA_SERVER_TMPDIR=${PLEX_MEDIA_SERVER_TMPDIR:-/tmp}
export PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR=${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR:-"${HOME}/Library/Application Support"}

Load any always-set defaults

test -f /etc/default/plexmediaserver && . /etc/default/plexmediaserver

Note that this only works if ./etc/default/plexmediaserver doesn't overwrite the environment values. The convention for /etc/default/* files is to be fully-commented out. Plex currently violates this convention, e.g. here's the default file: 

# default script for Plex Media Server

the number of plugins that can run at the same time

PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS=6

ulimit -s $PLEX_MEDIA_SERVER_MAX_STACK_SIZE

PLEX_MEDIA_SERVER_MAX_STACK_SIZE=3000

where the mediaserver should store the transcodes

PLEX_MEDIA_SERVER_TMPDIR=/tmp

uncomment to set it to something else

PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR="${HOME}/Library/Application\ Support"

the user that PMS should run as, defaults to ‘plex’

note that if you change this you might need to move

the Application Support directory to not lose your

media library

PLEX_MEDIA_SERVER_USER=plex

This is wrong - the entire file should be commented out by default, and then the user can change it if desired. 

 

These two changes together allow stuff like this, wehre the user sets a variable and start_pms uses it. 

$ PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS=1 /usr/sbin/start_pms

Finally, the last line of the start_pms script says this:

(cd /usr/lib/plexmediaserver; ./Plex\ Media\ Server)

It should say this (if plex wanted to be fancy they could use exec to get rid of the now-useless current shell, but it's not a big deal)

(cd $PLEX_MEDIA_SERVER_HOME; ./Plex\ Media\ Server)

PS - For what it's worth, I'd also replace this

echo $PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS $PLEX_MEDIA_SERVER_MAX_STACK_SIZE $PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR

 with something like this:

env | sort | grep PLEX

which generates much nicer startup output: 

PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR=/config
PLEX_MEDIA_SERVER_HOME=/opt/plexmediaserver
PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS=3
PLEX_MEDIA_SERVER_TMPDIR=/config/tmp
PLEX_MEDIA_SERVER_USER=media

but I'm not sure if env, grep, and sort are widely available. Perhaps someone could weigh in if they know? Or perhaps it's just better to make echo emulate this manually, instead of outputting a pseudo-cryptic message like "3 3000 /some/big/directory" on startup. 

Might wanna create a PR on GitHub for this git:

https://github.com/RustyDust/plex-media-server-distro-scripts

/T

Might wanna create a PR on GitHub for this git:

https://github.com/RustyDust/plex-media-server-distro-scripts

/T

Is this repo actually used by the plex team? It was last updated in 2012, and I've never heard of RustyDust :-)

it is

/T

take a peak here:

https://github.com/ukdtom/plex-media-server-distro-scripts

But get your Q....puzzled myself  ;)

Early 2021 clean-up: no feature suggestion