How to start utorrent server at boot on Debian and OpenSuse

This post explains how to start a command/program at boot time, on Linux, especially Debian. This specifically deals with the example of how to start utorrent server -utserver at boot time.

Note: These scripts have been updated and refined. For a simple one-liner install on any Debian/Ubuntu VPS, check this page

Update: The script has been tested and works fine, and works even with OpenSuse 12.2. However note that the installation of utorrent itself requires a few configurations, described below.

Btw, you should never run utorrent as root. The example shows utorrent installed at /root/utorrent, but you’d probably want to change the location.
First, write a script to start and stop the utserver program at will. Mine is as follows, and I’ve named it as ‘utstart’

#!/bin/bash
### BEGIN INIT INFO
# Provides:          utserver
# Required-Start:    
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Start/stop utserver
### END INIT INFO

case "" in
  start)
        echo Starting utorrent server
        cd /root/utorrent
        ./utserver &
        ;;
  stop)
        echo Stopping utorrent server
        killall utserver
        ;;
    *)
        echo Usage: start  stop
        exit 1
        ;;
    esac
exit 0

The initial comment-like text are what are known as LSB tags (something added in Debian 6). If these arent added, you get the following error when you start the server:

insserv: warning: script 'utstart' missing LSB tags and overrides[/code]
Note the points about linux runlevels:

0 – Shut down (or halt) the system 1 – Single-user mode; usually aliased as s or S 6 – Reboot the system The script needs to stop during the above runlevels. 2 – Multiuser mode without networking 3 – Multiuser mode with networking 5 – Multiuser mode with networking and the X Window System And of course it needs to start during the runlevels mentioned above.

Once you’ve written a script, copy it to /etc/init.d directory, and make it executable.

cp utstart /etc/init.d/utstart
chmod 755 /etc/init.d/utstart

Now,

To add it to the boot sequence:

On Debian6 or newer:

insserv utstart -dv[/code]
-d option means to setup the startup system so that utstart is started up with Defaults as defined in itself.

-v option stands for Verbose.

On older than Debian6:

update-rc.d utstart defaults

If stead of starting it up automatically at boot, you would rather like to start it up manually, you do:

/etc/init.d/utstart start

When you want to stop it,

/etc/init.d/utstart stop

You can remove it from the boot sequence with:

On Debian6 or newer:

insserv utstart -rv[/code]

On older than Debian6:

update-rc.d -f utstart remove

 Here, -r stands for remove; and -v for verbose.

Though maybe not as elegant, the easiest way to start a script is probably to include it in /etc/rc.local:

cat  /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

cd /root/utorrent
./utserver &
exit 0

Always remember to check whether your script is free of syntax errors. :smirk :wink

I have made a couple of modifications to make this script install and uninstall itself, without any need for manual copying or running innserv:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          utserver
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Start/stop utserver
### END INIT INFO
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
me=`basename `

case "" in
  start)
        echo Starting utorrent server
        cd /root/utorrent
        ./utserver &
        ;;
  stop)
        echo Stopping utorrent server
        killall utserver
        ;;
  install)
        echo Install

        echo Scriptname: $me
        echo Path: $DIR
        echo Installing utorrent server as a boot-time program..
        cp $DIR/$me /etc/init.d/
        insserv $me -dv
        echo utorrent server installed to boot-time..
        exit 0
        ;;
  uninstall)
        echo Uninstall
        echo Uninstalling utorrent server from boot init..
        insserv $me -rv
        echo Not deleting /etc/init.d/$me. You may delete it yourself. Leaving it there wont do any harm.
        echo utorrent server removed from boot init.
        ;;
  *)
        echo Usage: start  stop
        exit 1
        ;;
  esac
exit 0

I have now modifed it further, to add bash autocompletion (you need autocomplete package installed):

#!/bin/bash
### BEGIN INIT INFO
# Provides:          utserver
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     true
# Short-Description: Start/stop utserver
### END INIT INFO

function install_autocomplete () {
echo '#!/bin/bash
_utstart()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="start stop install uninstall"

    if [[ ${cur} == * && ${COMP_CWORD} -eq 1 ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
}
complete -F _utstart -o filenames utstart' > /etc/bash_completion.d/utstart
source /etc/bash_completion.d/utstart
chmod +x /etc/bash_completion.d/utstart
echo "Autocomplete entries for utstart added to bash. You may need to reboot or manually source the entry like this for this session:
      . /etc/bash_completion.d/utstart
"
}

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
me=`basename `
UTORRENT_LOCATION="/root/utorrent"
case "" in
  start)
        echo Starting utorrent server
        cd $UTORRENT_LOCATION
        ./utserver &
        ;;
  stop)
        echo Stopping utorrent server
        killall utserver
        ;;
  install)
        echo Install

        echo Scriptname: $me
        echo Path: $DIR
        echo Installing utorrent server as a boot-time program..
        cp $DIR/$me /etc/init.d/
        insserv $me -dv
        echo utorrent server installed to boot-time..
        install_autocomplete
        exit 0
        ;;
  uninstall)
        echo Uninstall
        echo Uninstalling utorrent server from boot init..
        insserv $me -rv
        echo Not deleting /etc/init.d/$me. You may delete it yourself. Leaving it there wont do any harm.
        echo utorrent server removed from boot init.
        ;;
  *)
        echo Usage: start  stop  install  uninstall
        exit 1
        ;;
  esac
exit 0

Additional Notes for installing utorrent server on OpenSuse 12.2 64 bit:

The following error message is shown on starting utorrent binary in OpenSuse 12.2:

 #./utserver
./utserver: error while loading shared libraries: libssl.so.0.9.8: cannot open shared object file: No such file or directory

Start yast, and search for packages in:

Search (P)hrase: libssl.so

[x]Provides (Alt-S)

And installed the following packages that turned up in search:

libopenssl0_9_8
libopenssl0_9_8-32bit

If it does not work, install all of these. Now, utorrent will start, but still give a message:

#./utserver
./utserver: /usr/lib/libcrypto.so.0.9.8: no version information available (required by ./utserver)
./utserver: /usr/lib/libssl.so.0.9.8: no version information available (required by ./utserver)
server started - using locale C

 

Update:
The same error “./utserver: error while loading shared libraries: libssl.so.0.9.8: cannot open shared object file: No such file or directory” recently appeared on a Debian dedicated server. Trying to install libssl apt packages from the wheezy repo didn’t help.

Solution: Download and manually install the .deb package from http://packages.debian.org/squeeze/libssl0.9.8

For a 64 bit Debian: http://packages.debian.org/squeeze/amd64/libssl0.9.8/download

wget http://ftp.us.debian.org/debian/pool/main/o/openssl/libssl0.9.8_0.9.8o-4squeeze14_amd64.deb
dpkg -i libssl0.9.8_0.9.8o-4squeeze14_amd64.deb

For 32 bit Debian:

wget http://droidzone.in/debian/libssl0.9.8_0.9.8o-4squeeze14_i386.deb
wget http://droidzone.in/debian/libssl0.9.8-dbg_0.9.8o-4squeeze14_i386.deb
dpkg -i *deb

Now try starting utorrent again. It works.