Skip to content

Instantly share code, notes, and snippets.

@davorb
Last active December 30, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davorb/7847751 to your computer and use it in GitHub Desktop.
Save davorb/7847751 to your computer and use it in GitHub Desktop.
#!/bin/sh
# cat /usr/local/etc/rc.d
# PROVIDE: rtorrent
# REQUIRE: DAEMON
#############
###<Notes>###
#############
# This script depends on screen.
# For the stop function to work, you must set an
# explicit session directory using ABSOLUTE paths (no, ~ is not absolute) in your rtorrent.rc.
# If you typically just start rtorrent with just "rtorrent" on the
# command line, all you need to change is the "user" option.
# Attach to the screen session as your user with
# "screen -dr rtorrent". Change "rtorrent" with srnname option.
# Licensed under the GPLv2 by lostnihilist: lostnihilist _at_ gmail _dot_ com
##############
###</Notes>###
##############
#######################
##Start Configuration##
#######################
# You can specify your configuration in a different file
# (so that it is saved with upgrades, saved in your home directory,
# or whateve reason you want to)
# by commenting out/deleting the configuration lines and placing them
# in a text file (say /home/user/.rtorrent.init.conf) exactly as you would
# have written them here (you can leave the comments if you desire
# and then uncommenting the following line correcting the path/filename
# for the one you used. note the space after the ".".
# . /etc/rtorrent.init.conf
# system user to run as
user="davidcollins"
# the system group to run as, not implemented, see d_start for beginning implementation
# group=`id -ng "$user"`
# the full path to the filename where you store your rtorrent configuration
config="`su $user -c 'echo $HOME'`/.rtorrent.rc"
# set of options to run with
options=""
# default directory for screen, needs to be an absolute path
base="`su $user -c 'echo $HOME'`"
# name of screen session
srnname="rtorrent"
# file to log to (makes for easier debugging if something goes wrong)
logfile="/var/log/rtorrentInit.log"
#######################
###END CONFIGURATION###
#######################
. /etc/rc.subr
PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin
name=rtorrent
rcvar=`set_rcvar`
eval "${rcvar}=\${${rcvar}:-'NO'}"
start_precmd=checkcnfg
start_cmd="${name}_start"
stop_cmd="${name}_stop"
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
err 3 "cannot find rtorrent binary in PATH $PATH"
fi
if ! [ -r "${config}" ] ; then
err 3 "cannot find readable config ${config}. check that it is there and permissions are appropriate"
fi
session=`getsession "$config"`
if ! [ -d "${session}" ] ; then
err 3 "cannot find readable session directory ${session} from config ${config}. check permissions"
fi
}
rtorrent_start()
{
echo "Starting $name."
[ -d "${base}" ] && cd "${base}"
stty stop undef && stty start undef
## start screen with rtorrent inside
su ${user} -c "screen -dm -S "${srnname}" ${name} ${options} 2>&1 1>/dev/null" | tee -a "$logfile" >&2
}
rtorrent_stop()
{
echo "Stopping $name."
session=`getsession "$config"`
if ! [ -s ${session}/rtorrent.lock ] ; then
return
fi
pid=`cat ${session}/rtorrent.lock | 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}
fi
}
getsession()
{
session=`cat "$1" | grep "^[[:space:]]*session[[:space:]]*=" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//" `
echo $session
}
load_rc_config $name
run_rc_command "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment