Skip to content

Commit

Permalink
Converted btsync and rtorrent init scripts to upstart.
Browse files Browse the repository at this point in the history
  • Loading branch information
xombiemp committed Jan 28, 2015
1 parent 58b41e8 commit d758a58
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 262 deletions.
71 changes: 0 additions & 71 deletions btsync

This file was deleted.

25 changes: 25 additions & 0 deletions btsync.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# btsync startup script
# requires upstart v1.4 or newer

description "BitTorrent Sync"

setuid YOURUSER
setgid YOURUSER
env CONFIG=/home/YOURUSER/.config/btsync/config.json

start on ( local-filesystems
and net-device-up IFACE=eth0)
stop on runlevel [!2345]

respawn

pre-start script
# check for config.json
if [ ! -f ${CONFIG} ]; then
# won't try to start without config
echo "ERROR: ${CONFIG} not found!"
exit 1
fi
end script

exec btsync --nodaemon --config ${CONFIG}
191 changes: 0 additions & 191 deletions rtorrent

This file was deleted.

70 changes: 70 additions & 0 deletions rtorrent.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# rtorrent startup script
# requires upstart v1.4 or newer

description "rTorrent - BitTorrent client"

setuid YOURUSER
setgid YOURUSER
env RTORRENTRC=/home/YOURUSER/.config/rtorrent/rtorrent.rc

start on ( local-filesystems
and net-device-up IFACE=eth0
and stopped rc)
stop on runlevel [!2345]

respawn

# "setsid screen -Dm" only forks once
expect fork

pre-start script
# check for configuration and session directory

# check for rtorrent.rc
if [ -f ${RTORRENTRC} ]; then
# check config for session directory
SESSION_DIR=$(sed -n 's/^[[:space:]]*session[[:space:]]*=[[:space:]]*\(.*\)$/\1/p' ${RTORRENTRC})
if [ -z ${SESSION_DIR} ]; then
echo "ERROR: session directory not configured!"
exit 1
else
# make sure directory exists
mkdir -p ${SESSION_DIR}
fi

# check config for session.lock.set
USE_LOCK=$(sed -n 's/^[[:space:]]*session\.use_lock.set[[:space:]]*=[[:space:]]*\(.*\)$/\1/p' ${RTORRENTRC})
if [ "x$USE_LOCK" != "xyes" ]; then
echo "WARNING: for proper service control add this to rtorrent.rc: session.use_lock.set = yes"
fi
else
# won't try to start without config
echo "ERROR: ${RTORRENTRC} not found!"
exit 1
fi
end script

# using setsid to avoid screen complaining about /var/run/screen permissions
# invoking interactive shell after clean rtorrent shutdown to avoid immediate
# termination of screen (stop command would end up in respawn-loop otherwise)
exec setsid screen -Dm -S rtorrent sh -c 'rtorrent -n -o import=${RTORRENTRC} && exec sh'

pre-stop script
# stop rtorrent instance gracefully before upstart stop command kills the screen session

# get session directory
# as we're already running, there's no need to do all the sanity checks again
SESSION_DIR=$(sed -n 's/^[[:space:]]*session[[:space:]]*=[[:space:]]*\(.*\)$/\1/p' ${RTORRENTRC})

# now, if we have a pid file, we can perform a clean shutdown
if [ -f ${SESSION_DIR}/rtorrent.lock ]; then
RT_PID=$(sed -n 's/^.*+\([0-9]\+\)$/\1/p' ${SESSION_DIR}/rtorrent.lock)
kill -2 $RT_PID
# wait until rtorrent exits
while kill -0 $RT_PID >/dev/null 2>&1; do
sleep 1
done
else
echo "WARNING: No pid for rtorrent found. Did you set \"session.use_lock.set = yes\" in rtorrent.rc?"
fi
end script

0 comments on commit d758a58

Please sign in to comment.