Jump to content

Init Script for Debian/Ubuntu & Derivatives


Superman710e

Recommended Posts

Hi All,

I have used the earlier ubuntu script provided early for the couple Debian & Ubuntu boxes I help administer and it worked well! http://forum.utorrent.com/viewtopic.php?pid=517398#p517398

On a whim I decided to try using the

/etc/init.d/skeleton

example script and adapting it for utserver, here's the results. It works very well! For those who wish to try here it is. Copy & paste it to

/etc/init.d/utserver 

and then

chmod a+x /etc/init.d/utserver

If you want it to start at boottime run

update-rc.d utserver defaults

This is setup for this directory structure:

/opt/utorrent/server/bin/utserver
/opt/utorrent/server/docs/
/etc/opt/utorrent/server/utserver.conf
/var/opt/utorrent/server/settings/webui.zip
/var/opt/utorrent/server/settings/
/var/opt/utorrent/server/log/

####Start below here####

#! /bin/sh
### BEGIN INIT INFO
# Provides: utserver
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start or stop the utserver.
# Description: uTorrent Server for Linux. A headless
# bittorrent client with a WebUI.
### END INIT INFO

# Author: John Doe <john.doe@jd.com>
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="uTorrent Server for Linux"
CHDIR=/opt/utorrent/server/bin
NAME=utserver
UT_CONFIG=/etc/opt/utorrent/server/$NAME.conf
UT_SETTINGS=/var/opt/utorrent/server/settings
UT_LOG=/var/opt/utorrent/server/log/$NAME.log
#DAEMON_ARGS="-daemon -configfile ${UT_CONFIG} -settingspath ${UT_SETTINGS} -logfile ${UT_LOG}"
DAEMON_ARGS="-configfile ${UT_CONFIG} -settingspath ${UT_SETTINGS} -logfile ${UT_LOG}"
DAEMON=$CHDIR/$NAME
USER=utorrent
GROUP=users
PIDFILE=/var/run/$NAME/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
STOP_TIMEOUT=5
INIT_VERBOSE=yes

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --make-pidfile --pidfile $PIDFILE --chuid $USER:$GROUP --chdir $CHDIR --background --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --make-pidfile --pidfile $PIDFILE --chuid $USER:$GROUP --chdir $CHDIR --background --exec $DAEMON -- \
$DAEMON_ARGS \
|| return 2
# Add code here, if necessary, that waits for the process to be ready
# to handle requests from services started subsequently which depend
# on this one. As a last resort, sleep for some time.
}

#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
#
# If the daemon can reload its configuration without
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
return 0
}

case "$1" in
start)
[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
stop)
[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
esac
;;
status)
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;;
#reload|force-reload)
#
# If do_reload() is not implemented then leave this commented out
# and leave 'force-reload' as an alias for 'restart'.
#
#log_daemon_msg "Reloading $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac

:

####End above here####

Hope someone finds it useful!

Jason

Link to comment
Share on other sites

Thank you so much, this worked perfectly!

One thing you might want to note in your instructions is setting the USER parameter in the script to a user that exists (I set mine to root, although I am not sure how secure this is).

True, yes, in my .deb package I have it automatically create the utorrent user and assign it to the users group. It also assigns the proper permissions to the created folder structures and binary. However, like you say for someone to just use this script, they should be aware of that.

Thus, if you want to use this script you may need to alter these 2 variables:

USER=utorrent
GROUP=users

and assign them to the user you want. Also assign similar permissions on whatever folder structure you use.

You can change these values to reflect your layout:


CHDIR=/opt/utorrent/server/bin
UT_CONFIG=/etc/opt/utorrent/server/$NAME.conf
UT_SETTINGS=/var/opt/utorrent/server/settings
UT_LOG=/var/opt/utorrent/server/log/$NAME.log
PIDFILE=/var/run/$NAME/$NAME.pid

Thanks,

Jason

Link to comment
Share on other sites

True, yes, in my .deb package I have it automatically create the utorrent user and assign it to the users group. It also assigns the proper permissions to the created folder structures and binary. However, like you say for someone to just use this script, they should be aware of that.

You have a deb package? Can I download that somewhere?

Link to comment
Share on other sites

Sure, they're here: http://goo.gl/2qfhd (Updated URL)

They're converted from the rpm packages I created for my OpenSUSE box, and then customized slightly and repackaged. A bit of a "quick & dirty" hack, but they work. (I just added in this init script, but have not tried an install with it, but it shouldn't cause any issues.)

Have fun! :D

Link to comment
Share on other sites

Got the below message after running rcutserver start when using your deb

start-stop-daemon: Unable to open pidfile '/var/run/utserver/utserver.pid' for writing: Permission denied (Permission denied)

Which version did you install? Did you change the user in the init script? If you changed the user, you will have to change the permissions on all the folders.

Here's the list that is being done by the package.

chown utorrent:users /etc/opt/utorrent/server/utserver.conf
chown utorrent:users /opt/utorrent/server/bin/utserver
chown -R utorrent:users /opt/utorrent/server/docs
chown -R utorrent:users /var/opt/utorrent/server
chown utorrent:users /var/run/utserver

If you changed the user & group you could copy and paste the above but change the user and group to what you are using.

ie: chown <user>:<group> <folder/file>

In case that doesn't work, as a workaround you could try the following, but with the user/group you set in the init script. (You will have to do this as root, or use sudo before each command depending if you're using Debian or *Ubuntu.)

/etc/init.d/utserver stop
rm -f /var/run/utserver/utserver.pid (just in case it's there)
mkdir -p /var/run/utserver
chown -R utorrent:users /var/run/utserver
/etc/init.d/utserver start

Hope that helps...

Regards,

Jason

PS: I recommend sticking with version 21886 as the subsequent versions broke the labeling/file moving function...

Link to comment
Share on other sites

Thank you Superman710e,

I'm also getting the pidfile error on ubuntu 10.10 when using your .deb (21886)

start-stop-daemon: unable to open pidfile '/var/run/utserver/utserver.pid' for writing (No such file or directory)

/var/run/utserver is properly created when you install the deb but somehow it get deleted on reboot.

I fixed it on my install by changing the pidfile path in /etc/init.d/utserver

PIDFILE=/var/run/$NAME.pid

Link to comment
Share on other sites

Thank you Superman710e,

I'm also getting the pidfile error on ubuntu 10.10 when using your .deb (21886)

start-stop-daemon: unable to open pidfile '/var/run/utserver/utserver.pid' for writing (No such file or directory)

/var/run/utserver is properly created when you install the deb but somehow it get deleted on reboot.

I fixed it on my install by changing the pidfile path in /etc/init.d/utserver

PIDFILE=/var/run/$NAME.pid

Okay, cool thanks, I guess it doesn't like the subdirectory, or maybe something else needs to change in the init script. I just took it from the default skeleton.

The odd thing is, this script works without issue on the Debian & Ubuntu boxes I have it running on, and with the PIDFILE set as it is in the first post...

Regards,

Jason

Link to comment
Share on other sites

Also needed to update-rc so it starts on boot.

sudo update-rc.d utserver defaults

Thanks, you are correct! That instruction is in the first post, but probably got lost in the verbosity! :D

For those who wish to try here it is. Copy & paste it to
/etc/init.d/utserver 

and then

chmod a+x /etc/init.d/utserver

If you want it to start at boottime run

update-rc.d utserver defaults

Also a good reminder from quadzero with regard to these commands. These commands need to be run as root, so whether you use su or sudo, use one to gain root privileges!

You could use one of the following at the start of your commands to switch root for your whole session:


su -
sudo -i

Just remember to

exit

when you are finished!

:cool:

Link to comment
Share on other sites

I added a dir check for ubuntu 10.10

*somewherein declarations

PIDDIR=/var/run/$NAME

.

.

.

.

.

.

*somewhere in do_start

# 1 if daemon was already running

# 2 if daemon could not be started

if [ ! -d $PIDDIR ]; then

mkdir $PIDDIR

chmod 0755 $PIDDIR

fi

.

.

.

.

.

------

Not related , my .conf file seems not never load though... I can write whatever I want in it , it only complains if it doesn't exist , for the rest nothing happens with it..

Link to comment
Share on other sites

  • 10 months later...

Sometimes utorrent actually takes a while to unbind the ports also startup may pass but utorrent may be unable to bind it's ports in which case the message should be failed.

I've made that an numerous structual changes to clean up the code a bit

also there's a new:

service utorrent log


#!/bin/sh
#
#
# Original source: http://forum.utorrent.com/viewtopic.php?id=88044
#
# uTorrent start stop service script
#
# copy to /etc/init.d
# run "update-rc.d utorrent defaults" to install
# run "update-rc.d utorrent remove" to remove
#
#
# version 2 improvments by:
# @author FanFan Huang (kaboom05+utorrentscript@gmail.com)
#
#
#where you extracted your utserver executable
UTORRENT_PATH=SOMEBASEDIR/utorrent-server-v3_0

LOGFILE=/var/log/utorrent.log #must be a writable directory

USER=utorrent #any user account you can create the utorrent user if you like

GROUP=users
NICE=15
SCRIPTNAME=/etc/init.d/utorrent #must match this file name

DESC="uTorrent Server for Linux"
CHDIR=$UTORRENT_PATH
NAME=utserver
UT_SETTINGS=$UTORRENT_PATH
UT_LOG=$LOGFILE

DAEMON_ARGS="-settingspath ${UT_SETTINGS} -logfile ${UT_LOG}"
DAEMON=$CHDIR/$NAME
PIDFILE=/var/run/utorrent.pid
STOP_TIMEOUT=5
INIT_VERBOSE=yes

FAILURE=false

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
FAILURE=false
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
# 3 if port bind failed

start-stop-daemon --start --nicelevel $NICE --quiet --make-pidfile --pidfile $PIDFILE --chuid $USER:$GROUP --chdir $CHDIR --background --exec $DAEMON --test > /dev/null
if [ "$?" = "1" ]; then
return 1
fi

start-stop-daemon --start --nicelevel $NICE --quiet --make-pidfile --pidfile $PIDFILE --chuid $USER:$GROUP --chdir $CHDIR --background --exec $DAEMON -- $DAEMON_ARGS
if [ "$?" != "0" ]; then
return 2
fi

#bind validation
while [ ! -e $LOGFILE ]; do
sleep 1 #Wait for file to be generated
done

while [ ! -n "$(cat $LOGFILE|grep 'IPv6 is installed')" ]; do
#wait until utorrent has finished bootup (IPv6 MESSAGE is the last message)
sleep 1
done

RESULT=$(cat $LOGFILE|grep 'bind failed')
if [ -n "$RESULT" ]; then
return 3
fi

return 0
}

#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
RETVAL="$?"
if [ "$RETVAL" = 2 ]; then
return 2
fi

start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
RETVAL="$?"
if [ "$RETVAL" = 2 ]; then
return 2
fi

#block process until server is completed shutting down fully
while [ -n "$(pidof "$NAME")" ]; do
sleep 1
done

# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
rm -f $LOGFILE #we don't want to keep our logfile
return "$RETVAL"
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
#
# If the daemon can reload its configuration without
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
return 0
}

msg_start() {
case "$1" in
0|1)
if [ "$VERBOSE" != no ]; then
log_end_msg 0
fi
;;
2)
if [ "$VERBOSE" != no ]; then
log_end_msg 1
fi
;;
3)
if [ "$VERBOSE" != no ]; then
log_daemon_msg "Port bind failure detected uTorrent may have limited functionality please change the bind port and restart uTorrent"
log_end_msg 1
fi
;;
esac
}

msg_stop() {
case "$1" in
0|1)
if [ "$VERBOSE" != no ]; then
log_end_msg 0
fi
;;
*)
if [ "$VERBOSE" != no ]; then
log_daemon_msg "Failed to stop service exit status $STATUS"
log_end_msg 1
fi
esac
}

case "$1" in
start)
if [ "$VERBOSE" != no ]; then
log_daemon_msg "Starting $DESC"
fi
do_start
msg_start "$?"
;;
stop)
if [ "$VERBOSE" != no ]; then
log_daemon_msg "Stopping $DESC"
fi
do_stop
msg_stop "$?"
;;
status)
if [ -e "$PIDFILE" ]; then
PID=" PID:($(cat $PIDFILE))"
else
PID=""
fi
status_of_proc "$DAEMON" "uTorrent$PID"

if [ "$?" != "0" ]; then
exit $?
fi
;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting $DESC"
do_stop
STATUS="$?"
if [ "$STATUS" -ne 0 ] && [ "$STATUS" -ne 1 ]; then
log_daemon_msg "Could not stop exit status $STATUS"
log_end_msg 1
exit 1
fi

do_start
STATUS="$?"
case "$STATUS" in
0)
log_end_msg 0
;;
*)
log_daemon_msg "Restart failed start exist status $STATUS"
log_end_msg 1
esac
;;
log)
if [ -e "$LOGFILE" ]; then
LOG=$(cat $LOGFILE)
echo "$LOG"
else
echo "uTorrent is not running no active log file"
fi
;;

*)

echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload|log}" >&2
exit 3
;;
esac

Link to comment
Share on other sites

  • 4 weeks later...

Here's an upstart script for you guys:


# It is highly recommended that you run this under a user account and not as root
# The LOGFILE must also be set to a directory writable by the user account

# Location of the log file (must be by the current user writable)
env LOGFILE=/home/utorrent/log/utorrent.log

# Path to the utorrent server
env UTORRENT_PATH=/home/utorrent/shared/programs/utorrent/utorrent-server-v3_0

# Name of the utorrent server binary (you usually don't need to change this)
env EXEC=utserver

# Nice value you can comment this out if don't want to run at a lower priority
nice 15

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [016]

# It is highly recommended that you don't run as the root user you will need
# to change this line to have your own user account for example:
#
# exec su <your user account> -c <utorrent command>

exec su utorrent -c "$UTORRENT_PATH/$EXEC -settingspath $UTORRENT_PATH -logfile $LOGFILE"

post-start script
#bind validation
while [ ! -e $LOGFILE ]; do
sleep 1 #Wait for file to be generated
done

while [ ! -n "$(cat $LOGFILE|grep 'IPv6 is installed')" ]; do
#wait until utorrent has finished bootup (IPv6 MESSAGE is the last message)
sleep 1
done

RESULT=$(cat $LOGFILE|grep 'bind failed')
if [ -n "$RESULT" ]; then
#bind failure stop the job
exit 3
fi
end script

post-stop script
#block process until server is completed shutting down fully
while [ -n "$(pidof "$EXEC")" ]; do
sleep 1
done

rm $LOGFILE
end script

Ubuntu uses upstart to satisfy startup dependencies. Upstart does away with the need to execute start-stop-daemon and all that other custom code. Upstart code is much more reliable in general and you can set things like respawn process if crashed unexpectedly.

Ubuntu recommends this method for init scripts going forward.

The script I posted waits for the File system and the internet interface to come up before it starts utserver.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...