Page 1 of 1
init.d script without sudo?
Posted: August 26th, 2008, 7:43 am
by mollydog
Hi
Im looking for an init.d script for a linux system without sudo. I use Mandriva which has an old fashioned root user and no sudo.
Thanks for any help
C
Re: init.d script without sudo?
Posted: August 26th, 2008, 8:04 am
by shypike
Just remove the sudo commands, you can run SABnzbd as root.
Re: init.d script without sudo?
Posted: August 26th, 2008, 8:33 am
by mollydog
Hi, thanks for your reply. Is it safe to run as root with a web interface?
Re: init.d script without sudo?
Posted: August 26th, 2008, 11:36 am
by mollydog
Worked it out..
Here's what I did, hope it helps somebody else
Code: Select all
#! /bin/sh
case "$1" in
start)
echo "Starting SABnzbd."
su -c "python /usr/local/src/SABnzbd/SABnzbd.py -d -f /home/sabuser/.sabnzbd/sabnzbd.ini" - sabuser
;;
stop)
echo "Shutting down SABnzbd."
wget -q --delete-after "http://HOSTADDRESS:PORT/sabnzbd/api?mode=shutdown"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
You need to replace the sabuser and hostname and port with your own as with the Install As A Unix Daemon wiki page.
Thanks to the devs for a great app

Re: init.d script without sudo?
Posted: August 27th, 2008, 4:44 am
by mollydog
Taking this a step further, Mandriva uses chkconfig in a similar way to Ubuntu and updaterc.d to manage the rc.d run level scripts. chkconfig needs some headers adding to the init.d file to make it into the correct syntax so it can be started and stopped automatically at boot and shutdown and managed through the graphical service manager in the mandriva control centre.
Ive included the complete file to enable you to do this, obviously you will need to change the SABUSER to your user and HOSTNAME and PORT to your own. It is adapted from the example script in the Mandriva wiki, using a variable for the service name so could be improved upon if you are a real stickler but it does the job!
Code: Select all
#! /bin/sh
#
### BEGIN INIT INFO
# Provides: sabnzbd
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 5
# Short-Description: launches sabnzbd
# Description: Start n Stop SABnzbd n'stuff
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
DAEMON_NAME=sabnzbd
LOCK_FILE=/var/lock/subsys/$DAEMON_NAME
RETVAL=0
# default option, they can be overriden in /etc/sysconfig/$DAEMON_NAME
# of course, you can place what you want.
# this file should be commented, with proper pointer to the doc, and you should use
# more than one line of option, if possible.
[ -f /etc/sysconfig/$DAEMON_NAME ] && . /etc/sysconfig/$DAEMON_NAME
start() {
# if you cannot start the daemon since something is missing ( like a path that
# cannot be set by default
# , place the test here
# if [ -z "$SOME_VAR" ]; then
# echo "You need to set $SOME_VAR in /etc/sysconfig/$DAEMON_NAME"
# RETVAL=1
# return
# fi
[ -f $LOCK_FILE ] && return
echo -n "Starting $DAEMON_NAME: "
su -c "python /usr/local/src/SABnzbd/SABnzbd.py -d -f /home/SABUSER/.sabnzbd/sabnzbd.ini" - SABUSER
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCK_FILE
}
stop() {
echo -n "Shutting down $DAEMON_NAME: "
wget -q --delete-after "http://HOSTNAME:PORT/sabnzbd/api?mode=shutdown"
sleep 3
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 5
start
;;
condrestart)
if [ -f $LOCK_FILE ]; then
stop
sleep 5
start
fi
;;
*)
echo "Usage: $0 {start|stop|restart|condrestart}"
RETVAL=1
esac
exit $RETVAL
Save this file as sabnzbd in /etc/init.d, you will need to be root, and make it executable.
Once saved you can add it using chkconfig with
You should then find it listed in MCC and be able to set it to start and stop automatically at boot and shutdown.
It uses a lock file in /var/lock/subsys to prevent multiple occurances. I found I had to add a few sleep's in to give the web server time to respond before it deletes the lock file. You may find you can decrease these but if you are unable to start sabnzbd after stopping it then its likely the lock file still exists, You can safely delete it if the sabnzbd is really stopped...
..will return your search for 'python' if it is and anything else using python (screenlets maybe?) but not a line like - python /usr/local/src/SABnzbd/SABnzbd.py -d -f /home/SABUSER/.sabnzbd/sabnzbd.ini
Hope it helps someone, Im sure it can be adapted to other non debian distro's too.
Re: init.d script without sudo?
Posted: August 27th, 2008, 6:32 am
by jcfp
mollydog wrote:
It uses a lock file in /var/lock/subsys to prevent multiple occurances. I found I had to add a few sleep's in to give the web server time to respond before it deletes the lock file.
Nice script, just a small remark: in stop(), you are reading the return value of the "sleep 3" command rather than the actual shutdown command. As a result, the return value will always be 0, and shutdown will be reported as successful even when it isn't (for example in case the web interface is password protected) or sabnzbd wasn't even running in the first place. Something along the lines of ...
Code: Select all
stop() {
echo -n "Shutting down $DAEMON_NAME: "
wget -q --delete-after "http://HOSTNAME:PORT/sabnzbd/api?mode=shutdown"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && {
sleep 3;
rm -f $LOCK_FILE;
}
}
... would correct that but also bring to light a new issue: current sabnzbd doesn't return anything after receiving an api shutdown command via http, thus wget thinks it has failed, etc... see
ticket #88. You'd probably end up always getting non-zero return values from wget even when shutdown succeeds which is worse than the current situation since it will leave the lock file in place. A possible temporary workaround would be to hardcode the RETVAL in the stop() function to be zero until ticket 88 is fixed.
Re: init.d script without sudo?
Posted: August 27th, 2008, 7:16 am
by mollydog
I see your point. I knew it was all going too well
Its probably better to have it correct and temorarily forced than incorrect so have made the changes as you suggested.
Thankyou!