SABnzbd 0.7.16 and Fedora 20 updated init scripts
Posted: December 1st, 2013, 10:41 am
With fedora 20 launching in 9 days, I've gone though the SABnzbd fedora init scripts and updated them.
Thanks to diedjee for the original scripts. This will correct the nc -z bug introduced after they changed the deployed netcat version in fedora 19 and updates the shutdown method to use the new SABnzbd api.
/etc/sysconfig/SABnzbd
Note the removal of the password and username arguments. They are no longer required with the new API in SABnzbd 0.7.16.
/etc/init.d/SABnzbd
Comments, concerns or additions, just paste them below.
Thanks to diedjee for the original scripts. This will correct the nc -z bug introduced after they changed the deployed netcat version in fedora 19 and updates the shutdown method to use the new SABnzbd api.
/etc/sysconfig/SABnzbd
Code: Select all
# SABnzbd service configuration
#run SABnzbd as
sabuser=user
#modify if SABnzbd config file is somewhere else for some reason
config="/home/${sabuser}/.sabnzbd/sabnzbd.ini"
#API key (can be found in the above config file)
apikey=$(cat "$config" | grep -E "^api_key =" | awk '{print $3}')
#gui address, eg: ${protocol}://${host}:${port}/sabznbd/
protocol=http
host=127.0.0.1
port=8080
#use nice, ionice, taskset to start SABnzbd
nicecmd=
# example: nicecmd="nice -n 19 ionice -c3"
/etc/init.d/SABnzbd
Code: Select all
#!/bin/sh
# chkconfig: - 20 80
# description: SABnzbd
exec="/usr/share/SABnzbd/SABnzbd.py"
prog="SABnzbd"
if [ -f /etc/sysconfig/$prog ]; then
. /etc/sysconfig/$prog
fi
if [ ! -n "$sabuser" -o ! -n "$apikey" -o ! -n "$protocol" -o ! -n "$host" -o ! -n "$port" ]; then
echo "Please configure /etc/sysconfig/$prog first."
[ ! -n "$sabuser" ] && echo -n " sabuser"
[ ! -n "$apikey" ] && echo -n " apikey"
[ ! -n "$protocol" ] && echo -n " protocol"
[ ! -n "$host" ] && echo -n " host"
[ ! -n "$port" ] && echo -n " port"
echo " variable(s) undefined"
exit 1
fi
if [ ! -f "$config" ]; then
echo "Can't find $config."
echo "Please run SABnzbd as $sabuser manually to configure it first."
exit 1
fi
# Source function library.
. /etc/rc.d/init.d/functions
start() {
[ -x $exec ] || exit 5
[ -f $config ] || exit 6
echo -n $"Starting $prog: "
# if not running, start it up here, usually something like "daemon $exec"
daemon --user=$sabuser $nicecmd $exec -d -f $config
retval=$?
echo
return $retval
}
stop() {
echo -n $"Stopping $prog: "
shutdownurl="${protocol}://${host}:${port}/sabnzbd/api?mode=shutdown&apikey=${apikey}"
/usr/bin/wget -q --delete-after --no-check-certificate "$shutdownurl"
echo "fetched $shutdownurl"
for sleepval in 1s 5s 10s 20s;do
sleep $sleepval
rh_status_q
ret=$?
if [ $ret -ne 0 ];then
echo_success
echo
return 0
fi
done
echo_failure
return 1
}
restart() {
stop
start
}
reload() {
restart
}
force_reload() {
restart
}
#Check whether the server is listening on $host:$port
#An exit value of 0 means: LISTENING
#Another value means: NOT LISTENING
rh_status() {
# run checks to determine if the service is running or use generic status
nbports=$(netstat -lnt | awk '$6 == "LISTEN" && $4 ~ ":'$port'"' | wc -l)
if [ $nbports -ne 0 ];then
pid=`ps -fu $sabuser | grep -v grep | grep SABnzbd.py | awk '{print $2}'`;
echo "$prog (pid $pid) is running...";
ret=0;
else
echo "$prog is stopped"
ret=1;
fi
echo "rh_status return=$ret"
return $ret;
}
#Same definition as for rh_status but query version (no output)
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
echo "STOP SWITCH"
rh_status
rh_status_q || echo "TEST 1"
rh_status_q && echo "TEST 2"
rh_status_q || exit 0
$1
;;
restart)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
$1
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
exit $?