Skip to content

Instantly share code, notes, and snippets.

@dawncold
Last active January 10, 2022 23:15
Show Gist options
  • Save dawncold/cfaa411cc61902977c9ba41bd5c55e9c to your computer and use it in GitHub Desktop.
Save dawncold/cfaa411cc61902977c9ba41bd5c55e9c to your computer and use it in GitHub Desktop.
Plex media server script for FreeBSD
#! /bin/sh
. /etc/rc.subr
name=plexmediaserver
rcvar=plexmediaserver_enable
load_rc_config $name
: ${plexmediaserver_enable:=NO}
command=/usr/sbin/daemon
procname="/root/plexdir/Plex\ Media\ Server"
command_args="-f ${procname}"
pidfile=/var/run/plex/plex.pid
start_precmd=plex_precmd
stop_precmd=plex_stop_precmd
stop_postcmd=plex_stop_postcmd
plex_precmd()
{
#Set identification variables for FREENAS; with fall back to freebsd
if [ -f "/etc/version" ]; then
export PLEX_MEDIA_SERVER_INFO_VENDOR="$(cat /etc/version|cut -d- -f1)"
export PLEX_MEDIA_SERVER_INFO_DEVICE=NAS
export PLEX_MEDIA_SERVER_INFO_MODEL="$(uname -m)"
export PLEX_MEDIA_SERVER_INFO_PLATFORM_VERSION="$(cat /etc/version|cut -d- -f2-)"
else
export PLEX_MEDIA_SERVER_INFO_VENDOR=FreeBSD
export PLEX_MEDIA_SERVER_INFO_DEVICE=PC
export PLEX_MEDIA_SERVER_INFO_MODEL="$(uname -m)"
export PLEX_MEDIA_SERVER_INFO_PLATFORM_VERSION="$(uname -r)"
fi
SCRIPTPATH="/root/plexdir"
export LD_LIBARARY_PATH="${SCRIPTPATH}/lib"
export PLEX_MEDIA_SERVER_HOME="${SCRIPTPATH}"
export PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS=6
export PLEX_MEDIA_SERVER_PIDFILE=${pidfile}
export PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR="/root/Library/Application Support"
export LC_ALL="en_US.UTF-8"
export LANG="en_US.UTF-8"
export PYTHONHOME="${SCRIPTPATH}/Resources/Python"
export PATH="${SCRIPTPATH}/Resources/Python/bin:${PATH}"
ulimit -s 3000
mkdir -p /var/run/plex
}
plex_stop_precmd()
{
if [ -r ${pidfile} ]; then
export _PLEXPID=$(check_pidfile ${pidfile} ${procname})
fi
}
plex_stop_postcmd()
{
_PLEX_CHILDREN=$(pgrep -g ${_PLEXPID})
echo "Cleaning up leftover child processes."
kill $sig_stop ${_PLEX_CHILDREN}
wait_for_pids ${_PLEX_CHILDREN}
}
run_rc_command "$1"
@dawncold
Copy link
Author

dawncold commented Mar 15, 2020

check_pidfile doesn't work for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment