--- sabnzbd.initd 2014-06-08 21:24:28.576078387 +0100 +++ sabnzbd 2014-06-08 21:23:42.471484110 +0100 @@ -2,7 +2,7 @@ # Copyright 1999-2013 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 -RUNDIR=/var/run/sabnzbd +RUNDIR='/var/run/sabnzbd' depend() { need net @@ -11,11 +11,11 @@ get_var() { echo $(sed -n \ '/^\[misc]/,/^'$1'/ s/^'$1' = \([[:alnum:].]\+\)[\r|\n|\r\n]*$/\1/p' \ - ${SABNZBD_CONFIGFILE}) + "${SABNZBD_CONFIGFILE}") } get_port() { - if [ "$(get_var 'enable_https')" == "1" ]; then + if [ "$(get_var 'enable_https')" -eq 1 ]; then echo $(get_var 'https_port') else echo $(get_var 'port') @@ -23,79 +23,78 @@ } get_addr() { - local host=$(get_var 'host') - local port=$(get_port) - local protocol - - [ "${host}" == "0.0.0.0" ] && host=localhost - if [ "$(get_var 'enable_https')" == "1" ]; then - protocol="https" - else - protocol="http" - fi + local host="$(get_var 'host')" + local protocol='http' + + [[ "${host}" == '0.0.0.0' ]] && host='localhost' - echo ${protocol}://${host}:${port} + [[ "$(get_var 'enable_https')" -eq 1 ]] && protocol='https' + + echo "${protocol}://${host}:$(get_port)" } get_pidfile() { - local port=$(get_port) - echo "${RUNDIR}/sabnzbd-${port}.pid" + echo "${RUNDIR}/sabnzbd-$(get_port).pid" +} + +start_pre() { + local pidfile="${get_pidfile}" + + if [ "${RC_CMD}" == 'restart' ]; then + while [ -e "$pidfile" ]; do + sleep 1 + done + fi + + return 0 } start() { ebegin "Starting SABnzbd" - checkpath -q -d -o ${SABNZBD_USER}:${SABNZBD_GROUP} -m 0770 "${RUNDIR}" + checkpath -q -d -o "${SABNZBD_USER}":"${SABNZBD_GROUP}" -m 0770 "${RUNDIR}" start-stop-daemon \ --quiet \ --start \ - --user ${SABNZBD_USER} \ - --group ${SABNZBD_GROUP} \ + --user "${SABNZBD_USER}" \ + --group "${SABNZBD_GROUP}" \ --name sabnzbd \ --background \ - --pidfile $(get_pidfile) \ + --pidfile "$(get_pidfile)" \ --exec /usr/bin/sabnzbd \ -- \ - --config-file ${SABNZBD_CONFIGFILE} \ - --logging ${SABNZBD_LOGGING} \ + --config-file "${SABNZBD_CONFIGFILE}" \ + --logging "${SABNZBD_LOGGING}" \ --daemon \ - --pid ${RUNDIR} + --pid "${RUNDIR}" - eend $? -} - -start_pre() { - if [ "$RC_CMD" == "restart" ]; then - local pidfile=$(get_pidfile) - while [ -e ${pidfile} ]; do - sleep 1 - done - fi - - return 0 + eend "$?" } stop() { - local api_key=$(get_var 'api_key') - local addr=$(get_addr) - local pidfile=$(get_pidfile) + local api_key="$(get_var 'api_key')" + local addr="$(get_addr)" local rc=1 - ebegin "Stopping SABnzbd @ ${addr}" + ebegin "Stopping SABnzbd" + + # This can only work if we have enabled the API + if [ -n "${api_key}" ] && [ "$(get_var 'disable_api_key')" -ne 1 ]; then + einfo "Attempting web-based shutdown @ ${addr}" + + # SABnzbd will return "ok" if shutdown is successful + [[ $(/usr/bin/curl -k -s "${addr}/sabnzbd/api?mode=shutdown&apikey=${api_key}") == 'ok' ]] && rc=0 + fi - # SABnzbd will return "ok" if shutdown is successful - rc=$(/usr/bin/curl -k -s "${addr}/sabnzbd/api?mode=shutdown&apikey=${api_key}") - if [ "${rc}" == "ok" ]; then - rc=0 - else - einfo "Falling back to SIGTERM, this may not work if you restarted via the web interface" + if [ "${rc}" -eq 1 ]; then + einfo "Falling back to SIGTERM. NOTE: May not work if you restarted via the web interface." start-stop-daemon \ --stop \ - --pidfile ${pidfile} \ + --pidfile "$(get_pidfile)" \ --retry SIGTERM/1/SIGKILL/5 - rc=$? + rc="$?" fi - eend ${rc} + eend "${rc}" }