Run Bittorrent Sync as a serice on Ubuntu

A init script for running btsync as a system service on behalf of a given user.
created by on 2014-05-01

The init script

Create the init script /etc/init.d/btsync:

# !/usr/bin/env bash

### BEGIN INIT INFO
# Provides: btsync
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Multi-user daemonized version of btsync.
# Description: Starts the btsync daemon for all registered users.
### END INIT INFO

DAEMON=/usr/bin/btsync
BTSYNC_USERS="andreaskoch"
LOGFILE=/var/log/btsync.log

start() {
    for btsuser in $BTSYNC_USERS; do
    HOMEDIR=`getent passwd $btsuser | cut -d: -f6`
    config=$HOMEDIR/.sync/config.json
    if [ -f $config ]; then
        echo "Starting BTSync for $btsuser"
        su - $btsuser -c "$DAEMON --config $config"
    else
        echo "Couldn't start BTSync for $btsuser (no $config found)"
    fi
    done
}

stop() {
    killall $DAEMON
}

status() {
    for btsuser in $BTSYNC_USERS; do
        dbpid=`pgrep -fu $btsuser $DAEMON`
        if [ -z "$dbpid" ]; then
            echo "btsync for USER $btsuser: not running."
        else
            echo "btsync for USER $btsuser: running (pid $dbpid)"
        fi
    done
}

case "$1" in
    start)
    start
    ;;

    restart)
    stop
    sleep 2
    start
    ;;

    stop)
    stop
    ;;

    status)
    status
    ;;

    *)
    echo "usage : $0 start|restart|stop|status"
    ;;
esac

exit 0

Make the init script executable:

sudo chmod +x /etc/ini.d/btsync

Btsync Configuration

Create a btsync config file (~/.sync/config.json) for the user that want to use bittorrent sync:

mkdir ~/.sync
touch ~/.sync/config.json
{
		"device_name": "Your-Device-Name",
		"listening_port": 0,
		"storage_path": "/home/your-user/.sync",
		"check_for_updates": true,
		"use_upnp": true,
		"download_limit": 0,
		"upload_limit": 0,
		"webui": {
				"listen": "0.0.0.0:8888",
				"login" : "some-user",
				"password" : "some-password"
		},
		"shared_folders": []
}

Schedule the service for autostart

sudo update-rc.d btsync defaults

Start the service

sudo service btsync start

Related Links

Tags:
Fork allmark on GitHub