PageRenderTime 21ms CodeModel.GetById 15ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/rc1.d/K02sabnzbdplus

http://github.com/brinkman83/bashrc
Shell | 130 lines | 92 code | 16 blank | 22 comment | 25 complexity | 4f09b67c6398fc6c33665b553f6df638 MD5 | raw file
  1#!/bin/sh
  2#
  3# Copyright (C) 2008-2010 by JCF Ploemen <linux@jp.pp.ru>
  4# released under GPL, version 2 or later
  5
  6################################################
  7#                                              #
  8#  TO CONFIGURE EDIT /etc/default/sabnzbdplus  #
  9#                                              #
 10################################################
 11
 12### BEGIN INIT INFO
 13# Provides:          sabnzbdplus
 14# Required-Start:    $local_fs $network $remote_fs
 15# Required-Stop:     $local_fs $network $remote_fs
 16# Should-Start:      NetworkManager
 17# Should-Stop:       NetworkManager
 18# Default-Start:     2 3 4 5
 19# Default-Stop:      0 1 6
 20# Short-Description: SABnzbd+ binary newsgrabber
 21### END INIT INFO
 22
 23DAEMON=/usr/bin/sabnzbdplus
 24SETTINGS=/etc/default/sabnzbdplus
 25
 26([ -x $DAEMON ] && [ -r $SETTINGS ]) || exit 0
 27
 28DESC="SABnzbd+ binary newsgrabber"
 29DEFOPTS="--daemon"
 30PYTHONEXEC="^$(sed -n '1s/^#\!\([a-z0-9\.\/]\+\)\(.*\)/\1(\2)?/p' $DAEMON)"
 31PIDFILE=/var/run/sabnzbdplus.pid
 32SETTINGS_LOADED=FALSE
 33
 34# these are only accepted from the settings file
 35unset USER CONFIG HOST PORT EXTRAOPTS
 36
 37. /lib/lsb/init-functions
 38
 39check_retval() {
 40	if [ $? -eq 0 ]; then
 41		log_end_msg 0
 42		return 0
 43	else
 44		log_end_msg 1
 45		exit 1
 46	fi
 47}
 48
 49is_running() {
 50	# returns 0 when running, 1 otherwise
 51	PID="$(pgrep -f -x -u $USER "$PYTHONEXEC $DAEMON $DEFOPTS.*")"
 52	RET=$?
 53	[ $RET -gt 1 ] && exit 1 || return $RET
 54}
 55
 56load_settings() {
 57	if [ $SETTINGS_LOADED != "TRUE" ]; then
 58		. $SETTINGS
 59
 60		[ -z "$USER" ] && {
 61			log_warning_msg "$DESC: not configured, aborting. See $SETTINGS";
 62			return 1; }
 63
 64		OPTIONS="$DEFOPTS"
 65		[ -n "$CONFIG" ] && OPTIONS="$OPTIONS --config-file $CONFIG"
 66		[ -n "$HOST" ] && SERVER="$HOST" || SERVER=
 67		[ -n "$PORT" ] && SERVER="$SERVER:$PORT"
 68		[ -n "$SERVER" ] && OPTIONS="$OPTIONS --server $SERVER"
 69		[ -n "$EXTRAOPTS" ] && OPTIONS="$OPTIONS $EXTRAOPTS"
 70		SETTINGS_LOADED=TRUE
 71	fi
 72	return 0
 73}
 74
 75start_sab() {
 76	load_settings || exit 0
 77	if ! is_running; then
 78		log_daemon_msg "Starting $DESC"
 79		start-stop-daemon --quiet --chuid $USER --start --exec $DAEMON -- $OPTIONS
 80		check_retval
 81		# create a pidfile; we don't use it but some monitoring app likes to have one
 82		[ -w $(dirname $PIDFILE) ] && \
 83			pgrep -f -x -n -u $USER "$PYTHONEXEC $DAEMON $OPTIONS" > $PIDFILE
 84	else
 85		log_success_msg "$DESC: already running (pid $PID)"
 86	fi
 87}
 88
 89stop_sab() {
 90	load_settings || exit 0
 91	if is_running; then
 92		TMPFILE=$(mktemp /tmp/sabnzbdplus.XXXXXXXXXX || exit 1)
 93		trap '[ -f $TMPFILE ] && rm -f $TMPFILE' EXIT
 94		echo "$PID" > $TMPFILE
 95		log_daemon_msg "Stopping $DESC"
 96		start-stop-daemon --stop --user $USER --pidfile $TMPFILE --retry 30
 97		check_retval
 98	else
 99		log_success_msg "$DESC: not running"
100	fi
101	[ -f $PIDFILE ] && rm -f $PIDFILE
102}
103
104case "$1" in
105	start)
106		start_sab
107	;;
108	stop)
109		stop_sab
110	;;
111	force-reload|restart)
112		stop_sab
113		start_sab
114	;;
115	status)
116		load_settings || exit 4
117		if is_running; then
118			log_success_msg "$DESC: running (pid $PID)"
119		else
120			log_success_msg "$DESC: not running"
121			[ -f $PIDFILE ] && exit 1 || exit 3
122		fi
123	;;
124	*)
125		log_failure_msg "Usage: $0 {start|stop|restart|force-reload|status}"
126		exit 3
127	;;
128esac
129
130exit 0