init.d script without sudo?

Get help with all aspects of SABnzbd
Forum rules
Help us help you:
  • Are you using the latest stable version of SABnzbd? Downloads page.
  • Tell us what system you run SABnzbd on.
  • Adhere to the forum rules.
  • Do you experience problems during downloading?
    Check your connection in Status and Interface settings window.
    Use Test Server in Config > Servers.
    We will probably ask you to do a test using only basic settings.
  • Do you experience problems during repair or unpacking?
    Enable +Debug logging in the Status and Interface settings window and share the relevant parts of the log here using [ code ] sections.
Post Reply
mollydog
Newbie
Newbie
Posts: 35
Joined: August 26th, 2008, 7:37 am

init.d script without sudo?

Post 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
User avatar
shypike
Administrator
Administrator
Posts: 19774
Joined: January 18th, 2008, 12:49 pm

Re: init.d script without sudo?

Post by shypike »

Just remove the sudo commands, you can run SABnzbd as root.
mollydog
Newbie
Newbie
Posts: 35
Joined: August 26th, 2008, 7:37 am

Re: init.d script without sudo?

Post by mollydog »

Hi, thanks for your reply. Is it safe to run as root with a web interface?
mollydog
Newbie
Newbie
Posts: 35
Joined: August 26th, 2008, 7:37 am

Re: init.d script without sudo?

Post 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  :)
mollydog
Newbie
Newbie
Posts: 35
Joined: August 26th, 2008, 7:37 am

Re: init.d script without sudo?

Post 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.

Code: Select all

chmod a+x sabnzbd
Once saved you can add it using chkconfig with

Code: Select all

chkconfig --add sabnzbd
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...

Code: Select all

ps aux | grep python
..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.
User avatar
jcfp
Release Testers
Release Testers
Posts: 989
Joined: February 7th, 2008, 12:45 pm

Re: init.d script without sudo?

Post 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.
mollydog
Newbie
Newbie
Posts: 35
Joined: August 26th, 2008, 7:37 am

Re: init.d script without sudo?

Post by mollydog »

I see your point. I knew it was all going too well  :D

Its probably better to have it correct and temorarily forced than incorrect so have made the changes as you suggested.

Thankyou!
Post Reply