Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
init.d script for btsync (based on another script built to run dropbox)
#!/bin/sh
### 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
# Replace with linux users you want to run BTSync clients for
BTSYNC_USERS="mendel"
DAEMON=/usr/bin/btsync
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"
start-stop-daemon -b -o -c $btsuser -S -u $btsuser -x $DAEMON -- --config $config
else
echo "Couldn't start BTSync for $btsuser (no $config found)"
fi
done
}
stop() {
for btsuser in $BTSYNC_USERS; do
dbpid=`pgrep -fu $btsuser $DAEMON`
if [ ! -z "$dbpid" ]; then
echo "Stopping btsync for $btsuser"
start-stop-daemon -o -c $btsuser -K -u $btsuser -x $DAEMON
fi
done
}
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
;;
stop)
stop
;;
restart|reload|force-reload)
stop
start
;;
status)
status
;;
*)
echo "Usage: /etc/init.d/btsync {start|stop|reload|force-reload|restart|status}"
exit 1
esac
exit 0
- Create a directory named .sync in your home directory
- Create a file inside .sync named config.json with the dumped and modified configuration
- Replace the configuration entry "storage_path" with the path to the .sync directory
- Move btsync to /usr/bin or replace DAEMON with the absolute path to btsync
- Copy the script to /etc/init.d and chmod +x it
- Replace BTSYNC_USERS with a list of usernames that you want btsync to work with, space delimited
- Use rcconf or another tool to make the service btsync start at system initialization
- Execute service btsync start
WHEN SETTING UP BTSYNC FOR MULTIPLE USERS, REMEMBER TO USE DIFFERENT PORT NUMBERS FOR webui.listen
- MULTIPLE PROCESSES CAN'T LISTEN TO THE SAME PORT!
Example:
{
"device_name": "<DEVICE NAME>",
"listening_port": 0,
"storage_path": "/home/mendel/.sync",
"check_for_updates": true,
"use_upnp": true,
"download_limit": 0,
"upload_limit": 0,
"webui": {
"listen": "0.0.0.0:8888",
"login" : "admin",
"password" : "password"
},
"shared_folders": []
}

dafyk commented May 17, 2013

If you name this script btsync, function status() will always reports btsync as running. Problem is on line 32
change from:
dbpid=pgrep -u $btsuser btsync
to:
dbpid=pgrep -fu $btsuser $DAEMON

Owner

MendelGusmao commented Jun 20, 2013

Thank you, @dafyk

Owner

MendelGusmao commented Jun 21, 2013

Thank you, @letiemble

Very nice! These instructions have worked on all of the Debian-based distros I've applied them to- Debian, Ubuntu, Mint, OpenMediaVault. I opted to run update-rc.d btsync defaults to make it start on boot. Piece of cake with extra icing, thank you.

vvtommy commented Aug 1, 2013

Thank you, :>

thoemy commented Aug 26, 2013

For reference a systemd service file. Copy it to /etc/systemd/system/btsync@.service.

[Unit]
Description=BitTorrent Sync Service for %i

[Service]
User=%i
ExecStart=/usr/local/bin/btsync --nodaemon --config %h/.sync/config.json

[Install]
WantedBy=multi-user.target

%i will be replaced by the part after the @ and %h by the home directory of the configured user.

Enable with systemctl enable btsync@MYUSER.service for user MYUSER.

jaycuse commented Sep 24, 2013

Thank you :)
Just a note for Debian based distros. if you use update-rc.d btsync defaults and get a message about dependency based boot sequence. The command you want to use is insserv btsync.

https://wiki.debian.org/LSBInitScripts/DependencyBasedBoot

Asysob commented Nov 19, 2013

Thank you for the script. Makes running BitTorrent Sync on my Raspberry Pis a lot easier :-)

kirbo commented May 8, 2014

I'm using:
BTSYNC_USERS=$(stat -c "%U" /home/*/.sync/config.json)
..instead of:
BTSYNC_USERS="user1 user2 user3",
..so I don't have to update init.d script everytime a new user wants to use BitTorrent Sync.

If I were even lazier, I'd be modifying "user skeleton" http://linuxg.net/the-unix-and-linux-skeleton-directory-etcskel/ so that it would include .sync/config.json.example and .sync/README.md files to help new users to enable and config their BitTorrent Sync.

any chance to get this updated to the latest approach for BTSync on servers? Specifically, that it is now using btsync.conf at the main folder for BTSync.

Thanks!

When omitting the -b switch in line 24 (where btsync is started) the errors aren't swallowed and it still works because btsync forks itself to background.

👍

I agree with @eggapauli and I would also add --log /var/log/btsync.log at the end of the start command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment