Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- ## ------------------------------------------------------------------------------------------
- ## Emby Server : "Take Your Home Media Anywhere With Emby, Your personal media on any device"
- ## Website : https://emby.media/
- ## Source : https://github.com/MediaBrowser/Emby
- ## License : https://github.com/MediaBrowser/Emby/blob/master/LICENSE.md
- ## Copyright (C) 2016 | solabc16
- ##
- ## This program is free software; you can redistribute it and/or
- ## modify it under the terms of the GNU General Public License
- ## as published by the Free Software Foundation; either version 2
- ## of the License, or (at your option) any later version.
- ##
- ## This program is distributed in the hope that it will be useful,
- ## but WITHOUT ANY WARRANTY; without even the implied warranty of
- ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ## GNU General Public License for more details.
- ##
- ## You should have received a copy of the GNU General Public License
- ## along with this program; if not, write to the Free Software
- ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- ## ------------------------------------------------------------------------------------------
- ##
- ## VARIABLES: Core, must be declared at head of file.
- ##
- script_version="3.0.8500.0-1"
- pkg_name="EmbyServer"
- script_directory=$(dirname $0)
- script_name=$(basename $0)
- ##
- ## Workaround to enable support for DSM 5.x users.
- ##
- if [ "$(grep "major" "/etc.defaults/VERSION" | cut -d"\"" -f2)" == "5" ]; then
- if [ -z "${BASH}" ]; then
- target_shell="/var/packages/${pkg_name}/scripts/utilities/sbin/bash"
- if [ -x "${target_shell}" ]; then
- "${target_shell}" "$0" "$@"
- exit
- fi
- fi
- fi
- ##
- ## VARIABLES: DEPLOYMENT SPECIFIC
- ##
- PKG_DISPNAME="Emby Server"
- EMBY_USER_LOGIN="embysvr"
- PKG_SERVICING_LOG="/var/log/synopkg_embyserver_servicing.log"
- ##
- ## VARIABLES: GENERAL
- ##
- ES=-1
- INSTALL_DIR="/var/packages/${SYNOPKG_PKGNAME}/target"
- LOG_ROOT="${INSTALL_DIR}/var/logs"
- SSS_LOG="/dev/null"
- TMP_LOG=""
- LOGAVAIL="false"
- EMBY_PID=${INSTALL_DIR}/var/embysvr.pid
- sss_mode=""
- ##
- ## VARIABLES: EMBY
- ##
- EMBY_EXEC="${INSTALL_DIR}/server/MediaBrowser.Server.Mono.exe"
- ##
- ## VARIABLES: MONO
- ##
- MONO_HOME="${INSTALL_DIR}/mono"
- MONO_BINS="${MONO_HOME}/bin"
- MONO_LIBS="${MONO_HOME}/lib"
- MONO_EXEC="${MONO_BINS}/mono"
- ##
- ## VARIABLES: FFMPEG
- ##
- FFMPEG_HOME="${INSTALL_DIR}/ffmpeg"
- FFMPEG_BINS="${FFMPEG_HOME}/bin"
- FFMPEG_LIBS="${FFMPEG_HOME}/lib"
- ##
- ## VARIABLES: IMAGEMAGICK
- ##
- MAGICK_HOME="${INSTALL_DIR}/imagemagick"
- MAGICK_BINS="${MAGICK_HOME}/bin"
- MAGICK_LIBS="${MAGICK_HOME}/lib"
- ##
- ## VARIABLES: SQLITE
- ##
- SQLITE_HOME="${INSTALL_DIR}/sqlite"
- SQLITE_BINS="${SQLITE_HOME}/bin"
- SQLITE_LIBS="${SQLITE_HOME}/lib"
- ##
- ## Functions : Logging functions, helper functions not intended to be called directly.
- ##
- create_timestamp(){
- echo "$(date -u +'%Y-%m-%d %H:%M:%S %Z')"
- }
- log_entry(){
- local ENTRY="[$(create_timestamp)]"
- ENTRY+=" $@"
- if [ "${LOGAVAIL}" == "true" ]; then
- echo "$ENTRY" >> ${TMP_LOG}
- fi
- }
- ##
- ## Functions : Logging functions, log file helper functions
- ##
- log_init(){
- local RC=-1
- if [ -d "${LOG_ROOT}" ]; then
- SSS_LOG="${LOG_ROOT}/start-stop-status_$(date -u +'%Y%m%d').log"
- if [ ! -f ${SSS_LOG} ]; then
- touch ${SSS_LOG}
- echo "* This log is rotated daily at 00:00 UTC, files are located at [${LOG_ROOT}]." >> ${SSS_LOG}
- echo "* The package servicing log can be found at [${PKG_SERVICING_LOG}]." >> ${SSS_LOG}
- echo "* Launch the package URL and navigate to Help -> Logs for ${PKG_DISPNAME} logs." >> ${SSS_LOG}
- echo "" >> ${SSS_LOG}
- echo "File Version : ${script_version}" >> ${SSS_LOG}
- echo "" >> ${SSS_LOG}
- echo "******************** LOG BEGINS ********************" >> ${SSS_LOG}
- echo "" >> ${SSS_LOG}
- fi
- TMP_LOG="/tmp/tmp.$(< /dev/urandom tr -dc A-Za-z0-9 | head -c10)"
- LOGAVAIL="true"
- RC=0
- else
- RC=1
- fi
- return ${RC}
- }
- log_commit(){
- if [ "${LOGAVAIL}" == "true" ]; then
- cat ${TMP_LOG} >> ${SSS_LOG}
- rm ${TMP_LOG}
- fi
- }
- ##
- ## Functions : Logging functions, use these to write entries to the log.
- ##
- log_message(){
- log_entry "$1"
- }
- ##
- ## Functions: Synology system helper functions.
- ##
- syno_ensure_pkgtl_status_is_stopped(){
- local ES=-1
- ##
- if [ "${SYNOPKG_DSM_VERSION_MAJOR}" == "6" ]; then
- if [ "${sss_mode}" == "status" ]; then
- status pkgctl-${SYNOPKG_PKGNAME} | grep -q "stop/waiting" &> /dev/null
- ES=$?
- if [ "${ES}" -ne 0 ]; then
- stop pkgctl-${SYNOPKG_PKGNAME}
- log_message ">> Ensuring package status is correct in DSM."
- fi
- fi
- fi
- }
- ##
- ## FUNCTIONS: start/stop/status/log method implementations.
- ##
- start_daemon(){
- local RC=-1
- local ES=-1
- local COMMAND=""
- local STDOUT="${LOG_ROOT}/embysvr.stdout"
- local STDERR="${LOG_ROOT}/embysvr.stderr"
- ##
- daemon_status
- ES=$?
- if [ "${ES}" -ne 0 ]; then
- log_message ">> Starting ${PKG_DISPNAME}."
- echo "" | tee -a ${STDOUT} ${STDERR} > /dev/null
- echo "------------------------------ $(create_timestamp) ------------------------------" | tee -a ${STDOUT} ${STDERR} > /dev/null
- echo "" | tee -a ${STDOUT} ${STDERR} > /dev/null
- chown ${EMBY_USER_LOGIN}:users ${STDOUT} ${STDERR}
- if [[ -d "/dev/dri" ]]; then
- log_message ">> Looking for DRM devices: FOUND"
- chmod -R go+rw /dev/dri/*
- else
- log_message ">> Looking for DRM devices: NONE"
- fi
- COMMAND+="env "
- COMMAND+="PATH=${MONO_BINS}:${FFMPEG_BINS}:${MAGICK_BINS}:${SQLITE_BINS}:${PATH} "
- COMMAND+="LD_LIBRARY_PATH=${FFMPEG_LIBS} "
- COMMAND+="${MONO_EXEC} "
- COMMAND+="${EMBY_EXEC} "
- COMMAND+="-package synology "
- COMMAND+="-programdata ${INSTALL_DIR}/var "
- COMMAND+="-ffmpeg ${FFMPEG_BINS}/ffmpeg "
- COMMAND+="-ffprobe ${FFMPEG_BINS}/ffprobe "
- COMMAND+="1>> ${STDOUT} "
- COMMAND+="2>> ${STDERR} & "
- COMMAND+="echo \$! > ${EMBY_PID};"
- log_message ">> Launch command [${COMMAND}]."
- su - ${EMBY_USER_LOGIN} -s /bin/sh -c "${COMMAND}"
- ES=$?
- if [ "${ES}" -eq 0 ]; then
- wait_for_status 0 20
- ES=$?
- if [ "${ES}" -eq 0 ]; then
- sleep 8
- log_message ">> ${PKG_DISPNAME} started successfully."
- RC=0
- else
- log_message ">> ${PKG_DISPNAME} failed to start."
- log_message ">> CHECK: [${STDOUT}]."
- log_message ">> CHECK: [${STDERR}]."
- RC=1
- fi
- else
- log_message ">> Launch invocation failed with status [${ES}]."
- RC=1
- fi
- else
- if [ "${ES}" -eq 0 ]; then
- log_message ">> Start request ignored, package is running!"
- RC=0
- else
- log_message ">> Package is not startable, status is [${ES}]."
- RC=1
- fi
- fi
- ##
- return ${RC}
- }
- stop_daemon(){
- local RC=-1
- local ES=-1
- ##
- daemon_status
- ES=$?
- if [ "${ES}" -eq 0 ]; then
- log_message ">> Stopping ${PKG_DISPNAME}."
- if [ -f "${EMBY_PID}" ]; then
- kill $(cat "${EMBY_PID}")
- wait_for_status 3 20
- ES=$?
- if [ "${ES}" -ne 0 ]; then
- log_message ">> ${PKG_DISPNAME} is still running, forcing shutdown."
- kill -9 $(cat "${EMBY_PID}")
- RC=1
- else
- log_message ">> ${PKG_DISPNAME} shutdown successfully."
- RC=0
- fi
- else
- log_message ">> PID file [${EMBY_PID}] does not exist."
- RC=1
- fi
- else
- log_message ">> Stop request ignored, package is stopped!"
- RC=0
- fi
- return ${RC}
- }
- daemon_status(){
- ##
- ## 3rd-Party Package Developer Guide : Status Codes
- ##
- ## 0 : package is running.
- ## 1 : program of package is dead and /var/run pid file exists.
- ## 2 : program of package is dead and /var/lock lock file exists
- ## 3 : package is not running
- ## 4 : package status is unknown
- ## 150 : package is broken and should be reinstalled. Please note, broken status (150) is only supported by DSM 4.2 and later.
- ##
- local RC=-1
- if [ ! -f "${EMBY_PID}" ]; then
- log_message ">> PID file [${EMBY_PID}] does not exist."
- syno_ensure_pkgtl_status_is_stopped
- RC=3
- else
- if [ -s "${EMBY_PID}" ]; then
- if [ -d /proc/$(cat "${EMBY_PID}") ]; then
- log_message ">> Process [$(cat ${EMBY_PID})] exists."
- RC=0
- else
- log_message ">> Process [$(cat ${EMBY_PID})] does not exist."
- rm ${EMBY_PID}
- log_message ">> Removed PID file [${EMBY_PID}]."
- syno_ensure_pkgtl_status_is_stopped
- RC=1
- fi
- else
- log_message ">> Removing PID file [${EMBY_PID}] as it is zero bytes."
- rm ${EMBY_PID}
- syno_ensure_pkgtl_status_is_stopped
- RC=1
- fi
- fi
- if [ "${RC}" -eq 0 ]; then
- log_message ">> ${PKG_DISPNAME} is running."
- else
- log_message ">> ${PKG_DISPNAME} is not running."
- fi
- return ${RC}
- }
- wait_for_status(){
- local RC=-1
- local ES=-1
- local COUNTER=$2
- ##
- log_message ">> Waiting for daemon status to become [$1]."
- while [ ${COUNTER} -gt 0 ]; do
- log_message ">> Checking status [${COUNTER}/$2]."
- daemon_status
- ES=$?
- if [ ${ES} == $1 ]; then
- COUNTER=-1
- RC=0
- else
- COUNTER=$(($COUNTER-1))
- sleep 1
- fi
- done
- return ${RC}
- }
- ##
- ## FUNCTIONS: Script Entry Point Implementation
- ##
- main(){
- local RC=-1
- local ES=-1
- ##
- log_init
- ES=$?
- if [ "${ES}" -eq 0 ]; then
- log_message "Script called with action [$1]..."
- case $1 in
- start)
- sss_mode="start"
- start_daemon
- ES=$?
- RC=${ES}
- ;;
- stop)
- sss_mode="stop"
- stop_daemon
- ES=$?
- RC=${ES}
- ;;
- status)
- sss_mode="status"
- daemon_status
- ES=$?
- RC=${ES}
- ;;
- log)
- log_message ">> Returning log file location [${SSS_LOG}]."
- echo "${SSS_LOG}"
- RC=0
- ;;
- *)
- log_message ">> Requested function is not supported."
- RC=1
- ;;
- esac
- fi
- return ${RC}
- }
- ##
- ## SCRIPT: Entry Point Begin
- ##
- main "$@"
- ES=$?
- log_message ">> Exit status is [${ES}]."
- log_commit
- exit ${ES}
- ##
- ## SCRIPT: Entry Point End
- ##
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement