# upstart bittorrent sync two configs for btsync

I want to write an upstart script for BitTorrent Sync that starts btsync like "btsync --config day.conf" in the morning and restarts it with "btsync --config night.conf" at night. This way, I would be able to limit the upload rate in the "day.conf" file and remove any limits in the "night.conf" file. I want to use cron to then start btsync with the right config file.

I currently have in /etc/init/btsync.conf

setuid patrick
setgid patrick

start on runlevel [2345]
stop on runlevel [016]

expect fork

instance $CONF chdir /home/patrick/programs/btsync exec ./btsync --config$CONF


I can then start btsync with something like:

sudo initctl start btsync CONF=btsync-day.conf


which I could put into /etc/crontab and it would work. But I think that using the instance stanza is not the right thing to do, as I am not running instances really. Btsync can not be running multiple instances. Also to stop the job, I have to stop it using the right CONF= variable, but I would rather be able to just stop it without needing to specify the right instance.

Is there a better way to do this in upstart?

I think the best way to do it is to have a script that will manage the btsync application. The script will monitor the time and restart the btsync application accordingly.

The script should look like (not tested):

#!/bin/bash

SUNRISE="06:00"
SUNSET="19:00"

RUNDIR=basename "$0" PIDFILE="${RUNDIR}/.sync/sync.pid"
CONFWFILE="${RUNDIR}/.btsync_safe.confw" CONFDAY="${RUNDIR}/btday.conf"
CONFNIGHT="${RUNDIR}/btnight.conf" function start_bt { start-stop-daemon --chdir "$RUNDIR" --pidfile "$PIDFILE" --start --exec ./btsync -- -c$1
echo "$1" > "$CONFWFILE"
}

function stop_bt
{
start-stop-daemon --pidfile "$PIDFILE" --stop } function which_conf { curtime=date +%H:%M [$curtime > "$SUNRISE" &&$curtime < "$SUNSET" ] && echo "$CONFDAY" || echo "$CONFNIGHT" } trap "stop_bt" EXIT chdir$RUNDIR
for ((;;))
do
sleep 60
[ cat "$CONFWFILE" == which_conf ] || stop_bt kill -0 $PIDFILE || start_bt which_conf
done