Created
November 17, 2012 10:31
-
-
Save larvanitis/4094730 to your computer and use it in GitHub Desktop.
RTORRENT::Daemon setup / init script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
### BEGIN INIT INFO | |
# Provides: rtorrent | |
# Required-Start: $network | |
# Required-Stop: $network | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Start rtorrent as a daemon | |
### END INIT INFO | |
user="p2p" | |
config="/etc/rtorrent.conf" # the full path to the filename where you store your rtorrent configuration | |
options="-n -o import=${config}" # set of options to run with | |
base="`su -c 'echo $HOME' $user`" | |
srnname="rtorrent" # name of screen session | |
logfile="/var/log/rtorrentd.log" # file to log to (makes for easier debugging if something goes wrong) | |
session="" | |
lock_file="" | |
PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin | |
DESC="Bittorrent client" | |
NAME=rtorrent | |
DAEMON=$NAME | |
SCRIPTNAME="/etc/init.d/$NAME"d | |
checkcnfg() { | |
exists=0 | |
for i in `echo "$PATH" | tr ':' '\n'` ; do | |
if [ -f $i/$NAME ] ; then | |
exists=1 | |
break | |
fi | |
done | |
if [ $exists -eq 0 ] ; then | |
echo "Cannot find rtorrent binary in PATH $PATH" | tee -a "$logfile" >&2 | |
exit 3 | |
fi | |
if ! [ -r "${config}" ] ; then | |
echo "Cannot find readable config ${config}. Check that it is there and permissions are appropriate" | tee -a "$logfile" >&2 | |
exit 3 | |
fi | |
session=`cat ${config} | grep "^[[:space:]]*session[[:space:]]*=" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//" ` | |
if ! [ -d "${session}" ] ; then | |
echo "Cannot find readable session directory ${session} from config ${config}. Check permissions" | tee -a "$logfile" >&2 | |
exit 3 | |
fi | |
lock_file="${session}/rtorrent.lock" | |
} | |
cleanup_lockfile() { | |
if [ -s ${lock_file} ] ; then # lock exists | |
pid=`cat ${lock_file} | awk -F: '{print($2)}' | sed "s/[^0-9]//g"` | |
ps -A | grep -sq ${pid}.*rtorrent && return 1 # rtorrent is running | |
rm ${lock_file} # rtorrent is not running | |
fi | |
return 0 | |
} | |
d_start() { | |
! cleanup_lockfile && log_daemon_msg "Is $NAME already running?" && return 1 | |
[ -d "${base}" ] && cd "${base}" | |
stty stop undef && stty start undef | |
su -c "screen -ls | grep -sq "\.${srnname}[[:space:]]" " ${user} || \ | |
su -c "screen -dm -S ${srnname} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2 | |
# this works for the screen command, but starting rtorrent below adopts screen session gid | |
# even if it is not the screen session we started (e.g. running under an undesirable gid | |
su -c "screen -S $srnname -X screen rtorrent ${options}" ${user} | tee -a "$logfile" >&2 | |
} | |
d_stop() { | |
! [ -s ${lock_file} ] && echo " $NAME wasn't running" && return | |
pid=`cat ${lock_file} | awk -F: '{print($2)}' | sed "s/[^0-9]//g"` | |
# make sure the pid doesn't belong to another process | |
if ps -A | grep -sq ${pid}.*rtorrent ; then | |
kill -s INT ${pid} | |
echo -n " " | |
# wait up to 10 seconds and then kill it forcefully | |
count=0 | |
while kill -0 ${pid} 2>/dev/null; do | |
echo -n "." && sleep 1 && (( count++ )) | |
[ $count -eq 10 ] && echo -n "timeout - force killing $NAME!" && kill -9 ${pid} && break | |
done | |
echo | |
fi | |
} | |
checkcnfg | |
. /lib/lsb/init-functions | |
case "$1" in | |
start) | |
log_daemon_msg "Starting $DESC $NAME" | |
d_start | |
log_end_msg "$?" | |
;; | |
stop) | |
log_daemon_msg "Stopping $DESC $NAME" | |
d_stop | |
log_end_msg 0 | |
;; | |
restart|force-reload) | |
$0 stop | |
$0 start | |
;; | |
*) | |
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment