Need help writing a "wrapper" script for the webgui for an init.d service

67 views
Skip to first unread message

Tiny

unread,
Apr 23, 2011, 11:25:26 AM4/23/11
to Alt-F
Hi,

I have installed debian into a chroot, on my alt-f 0.1b7 flashed nas.
I have also installed pyload, and I have an init.d script that works,
It starts, stops, and gives the status of the process, from the chroot
cli.

I'm not familiar with scripting and I'm struggling to get the wrapper
to work.

Sometimes I get it to work with the start command, other times only
the stop works.
I can't get the status to work properly at all, (hence the reason the
other start and stop don't work properly,
because it does'nt know the state of the process).

I've been googling and headscratching for days now.

Hopefully someone can help me to get this to work.... I've used dhub's
script for his debian_sabnzbd as a template for the wrapper.

This is my script that goes in CHROOT/etc/init.d called pyload

#!/bin/sh
# Start/stop pyload.
#
### BEGIN INIT INFO
# Provides: pyload
# Short-Description: Regular background program processing daemon
# Description: pyLoad is a fast, lightweight and full
featured download manager for many One-Click-Hoster,
# container formats like DLC, video sites
or just plain http/ftp links.
### END INIT INFO
#
### CONFIGURATION
PYLOAD_DIR="/opt/pyload" # folder which contains pyLoadCore.py
PYTHON="/usr/bin/python" # python location
LOG="/var/log/pyload.log" #l log
NAME="pyload"
PIDFILE="/root/.pyload/pyload.pid" # PID location
#################

start() {
echo "Starting Service: pyload"
start-stop-daemon --start --oknodo --pidfile ${PIDFILE} -b -d $
{PYLOAD_DIR} --startas ${PYTHON} pyLoadCore.py -- >>${LOG} 2>>${LOG}
}

stop() {
echo "Stopping Service: pyload"
start-stop-daemon --stop --oknodo --pidfile ${PIDFILE} --retry=TERM/
30/KILL/5
rm ${PIDFILE}
}

This is the wrapper script

#! /bin/sh

DESC="Initialize pyload under the debian chroot environment"
TYPE=net
NAME=debian_pyload

. $(dirname $0)/common

# First find the debian chroot environment
CHROOT="`getchrootdir`"

start_debian_pyload() {
chroot "$CHROOT" /etc/init.d/pyload start
RC="$?"
return $RC
}

stop_debian_pyload() {
pkill -9 pyload
chroot "$CHROOT" /etc/init.d/pyload stop
RC="$?"
return $RC
}

status_debian_pyload() {
chroot "$CHROOT" /etc/init.d/pyload status > /dev/null
RC="$?"
return $RC
}

case "$1" in
start)
#echo -n "Starting $NAME: "
start_debian_pyload
rc="$?"
if [ "$rc" == "0" ]; then
echo "OK."
exit 0
else
echo "Debian startup failed."
exit 1
fi
;;
stop)
#echo -n "Stopping $NAME: "
stop_debian_pyload
rc="$?"
if [ "$rc" == "0" ]; then
echo "OK."
exit 0
else
echo "Debian stop failed."
exit 1
fi
;;
status)
echo -n "$NAME "
status_debian_pyload
rc="$?"
if [ "$rc" == "0" ]; then
echo "running"
exit 0
else
echo "stopped"
exit 1
fi
;;
restart) restart $NAME ;;
*) usage $0 "start|stop|status|restart" ;;
esac


status() {

echo -n "Status of pyload: "
if [ -e ${PIDFILE} ]; then
echo " Fancontrol daemon is running"
else
echo " Fancontrol daemon is not running"
fi
}

case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
esac

exit 0

Thanks

Joao Cardoso

unread,
Apr 24, 2011, 6:36:02 PM4/24/11
to Alt-F


On Apr 23, 4:25 pm, Tiny <trickymick...@gmail.com> wrote:
> Hi,
>
> I have installed debian into a chroot, on my alt-f 0.1b7 flashed nas.
> I have also installed pyload, and I have an init.d script that works,
> It starts, stops, and gives the status of the process, from the chroot
> cli.
>
> I'm not familiar with scripting and I'm struggling to get the wrapper
> to work.
>
> Sometimes I get it to work with the start command, other times only
> the stop works.
> I can't get the status to work properly at all, (hence the reason the
> other start and stop don't work properly,
> because it does'nt know the state of the process).
>
> I've been googling and headscratching for days now.
>
> Hopefully someone can help me to get this to work.... I've used dhub's
> script for his debian_sabnzbd as a template for the wrapper.
>
> This is my script that goes in CHROOT/etc/init.d called pyload

As you say that it works fine, I will not analise it.

> This is the wrapper script
>
> #! /bin/sh
>
> DESC="Initialize pyload under the debian chroot environment"
> TYPE=net
> NAME=debian_pyload
>
> . $(dirname $0)/common

This command includes a file named "/etc/init.d/common" that has some
functions already defined, namely start(), stop() and status().
Your script also has functions with that name, so problems can arise.
I don't think you need anything else below this point. Just delete
till the end of the file ;)

Don't call this function status(), cal it something else, like
mstatus()
> status() {
>         echo -n "Status of pyload: "
>         if [ -e ${PIDFILE} ]; then
>         echo " Fancontrol daemon is running"
>         else
>         echo " Fancontrol daemon is not running"
>         fi
>
> }

Why a new "case" ? You already test the argument "$1" and take actions
above.
This is redundant and problematic, as the start function defined in /
etc/init.d/common will be executed, and will try to launch a process
(none, as you don't supply an argument -- if you look to other scripts
from Alt-F, you will notice that the function start is invoked with
$NAME as argument, where NAME is a variable defined at the top and has
the name of the process you want to execute)

> case "$1" in

dont
>  start)

same problem as status() above

>  start
>  ;;
>  stop)

same problem as status() above

>  stop
>  ;;
>  status)

same problem as status() above

>  status
>  ;;
> esac
>
> exit 0
>
> Thanks

But... just for curiosity, what does this script intends to control?
what is 'pyload'?

Tinybilbo

unread,
May 1, 2011, 3:48:40 PM5/1/11
to Alt-F
Hi, Joao,
> But... just for curiosity, what does this script intends to control?
> what is 'pyload'?
pyLoad is a lightweight download manager for many One-Click-Hosters,
video sites, or plain http/ftp links.

> if you look to other scripts...
> from Alt-F, you will notice that the function start is invoked with
> $NAME as argument, where NAME is a variable defined at the top and has
> the name of the process you want to execute)

Thanks for looking at the scripts,
Somehow they got mixed together, which did'nt help.
I've looked at the your other scripts and used the name function, and
this is what I've come up with...
Unfortunately the "start" from the webgui just does'nt work, however
it does from the command line, which is weird.
Both the "status" and "stop" work now.
I changed some of the functions names to avoid confusion.

/mnt/sda2/debian/debian/etc/init.d/pyload

##################################################################################################################################
#!/bin/sh

PYLOAD_DIR="/opt/pyload" # folder which contains pyLoadCore.py
PYTHON="/usr/bin/python"
LOG="/var/log/pyload.log"
PIDFILE="/root/.pyload/pyload.pid"

go() {
echo "Starting Service: pyload"
start-stop-daemon --start --oknodo --pidfile ${PIDFILE} -b -d $
{PYLOAD_DIR} --startas ${PYTHON} pyLoadCore.py -- >>${LOG} 2>>${LOG}
}

end() {
echo "Stopping Service: pyload"
start-stop-daemon --stop --oknodo --pidfile ${PIDFILE} --retry=TERM/
30/KILL/5
rm ${PIDFILE}
}


test() {

echo -n "Status of pyload: "
if [ -e ${PIDFILE} ]; then
echo " Pyload daemon is running"
return 0
else
echo " Pyload daemon is not running"
return 1
fi
}

case "$1" in
go)
go
;;
end)
end
;;
test)
test
;;
esac
###################################################################################################################################

and the wrapper script
/etc/init.d/S90pyload

####################################################################################################################################
DESC="Daemon to run pyload"
NAME=pyload
TYPE=sys

. $(dirname $0)/common

case "$1" in
start) $NAME go ;;
stop) $NAME end ;;
status) $NAME test ;;

*) usage $0 "start|stop|status" ;;
esac
####################################################################################################################################

Any thoughts as to why the "start" does'nt work...? (is it because, it
needs to be run from a chroot?)

Thanks

Joao Cardoso

unread,
May 2, 2011, 8:03:36 PM5/2/11
to Alt-F
Almost certainly.

And for the S90pyload to work from the command line, have you changed
the PATH?
Because

      start)   $NAME go ;;

where NAME=pyload, implies that the 'pyload' script must be in root's
PATH. Or you might have a stray copy of pyload out of Debian?

Did you take a look at "dhub" Debian initd and wrap scripts? I think
he did something similar to your requirements,

>
> Thanks
Reply all
Reply to author
Forward
0 new messages