Last active
May 3, 2023 13:20
-
-
Save letiemble/5143971 to your computer and use it in GitHub Desktop.
Init.d script for rtorrent.
The script creates a screen session and launch rtorrent as the given user. The user is taken from the script name; the script must follow the namnig convention "rtorrent-<user>".
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/sh | |
### BEGIN INIT INFO | |
# Provides: rtorrent-XXX | |
# Required-Start: $local_fs $remote_fs $portmap | |
# Required-Stop: $local_fs $remote_fs $portmap | |
# Should-Start: $network | |
# Should-Stop: $network | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Daemonized version of rtorrent. | |
# Description: Starts the rtorrent daemon. | |
### END INIT INFO | |
# Author: Laurent Etiemble | |
####################### | |
# System user to run as | |
user="`basename $0 | awk -F"-" '{ print $2 }'`" | |
# The full path to the filename where you store your rtorrent configuration | |
config="`su -c 'echo $HOME' $user`/.rtorrent.rc" | |
# Set of options to run with | |
options="" | |
# Default directory for screen, needs to be an absolute path | |
base="`su -c 'echo $HOME' $user`" | |
# Name of screen session | |
srnname="rtorrent-$user" | |
# File to log to (makes for easier debugging if something goes wrong) | |
logfile="/var/log/rtorrent-$user.log" | |
####################### | |
PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin | |
DESC="rtorrent as $user" | |
NAME=rtorrent | |
SCRIPTNAME="/etc/init.d/`basename $0`" | |
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=`getsession "$config"` | |
if ! [ -d "$session" ] ; then | |
echo "cannot find readable session directory $session from config $config. check permissions" | tee -a "$logfile" >&2 | |
exit 3 | |
fi | |
} | |
rtorrent_start() { | |
[ -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 | |
su -c "screen -S "$srnname" -X screen rtorrent ${options} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2 | |
} | |
rtorrent_stop() { | |
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"` | |
kill -s INT ${pid} | |
} | |
getsession() { | |
session=`cat "$1" | grep "^[[:space:]]*session[[:space:]]*=" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//" ` | |
su -c "readlink -f $session" $user | |
} | |
checkcnfg | |
case "$1" in | |
start) | |
echo -n "Starting $DESC: $NAME" | |
rtorrent_start | |
echo "." | |
;; | |
stop) | |
echo -n "Stopping $DESC: $NAME" | |
rtorrent_stop | |
echo "." | |
;; | |
restart|force-reload) | |
echo -n "Restarting $DESC: $NAME" | |
rtorrent_stop | |
sleep 1 | |
rtorrent_start | |
echo "." | |
;; | |
*) | |
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
If your user is
www-data
and you run asrtorrent-www-data
, then https://gist.github.com/letiemble/5143971#file-rtorrent-xxx-L19 will only catchwww
.Here's how to hotfix: