Bittorrent Sync Debian Start Script

Published: by Creative Commons Licence

Time to read: 5 minutes

Bittorrent Sync is a Dropbox alternative for the private cloud. Files can be synced between clients in the local network or to your own server.

The following start script starts or stops the btsync daemon in Linux.

#! /bin/sh
### BEGIN INIT INFO
# Provides:          Bittorrent Sync 
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs 
# Default-Start:     3 4 5
# Default-Stop:      0 1 6
# Short-Description: Startskript for btsync daemon.
# Description:       Starts the btsync daemon. 
### END INIT INFO

# Author: Tim Pommerening <admin@tyranus.de>

# Do NOT "set -e"

DESC="Bittorrent Sync"
NAME=btsync
DAEMON=/opt/btsync/${NAME}
CONFIG=/opt/btsync/${NAME}.config
PIDFILE=/var/run/btsync/btsync.pid
SCRIPTNAME=/etc/init.d/btsync

# Exit if the package is not installed
[ -x "$DAEMON" ] || (printf "$DAEMON not found."; exit 0)
# Exit if config file is missing
[ -f "$CONFIG" ] || (printf "$CONFIG not found."; 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 - proofed
    #   1 if daemon was already running - proofed
    #   2 if daemon could not be started - TODO: NOT PROOFED YET
    if [ -f "${PIDFILE}" ]; then
          printf "pid file exists. Is btsync already running?\n\n"
          return "1"
    fi

    su btsync -c "${DAEMON} --config ${CONFIG} >/dev/null" 
    RVAL=$?
    if [ "x${RVAL}" = "x0" ]; then
      PID=`head -1 ${PIDFILE} 2>/dev/null`
      printf "btsync started (pid=${PID}).\n\n"
    else
      printf "Unable to start btsync (return code=${RVAL}).\n\n"
    fi
    return "${RVAL}"
    # 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
        PID=`head -1 ${PIDFILE} 2>/dev/null`
        RVAL=$?
        if [ "x${RVAL}" = "x1" ]; then
          printf "Could not find pid file. btsync is not running?\n\n"
          return "${RVAL}"
        fi

        kill -15 ${PID}
        RVAL=$?
        if [ "x${RVAL}" = "x0" ]; then
      su btsync -c "rm ${PIDFILE}"
          printf "Stopped btsync (pid=${PID}) and removed pid file.\n\n"
        else
          printf "Error stopping btsync (pid=${PID})."
        fi
    return "$RVAL"
}

#
# 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.
    #
    # TODO: Is btsync reloadable with HUP?
    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
    ;;
  #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|restart}" >&2
    exit 3
    ;;
esac

: