Skip to content

Instantly share code, notes, and snippets.

@l-modolo
Created June 19, 2015 08:58
Show Gist options
  • Save l-modolo/e96a20ff75b022d62f77 to your computer and use it in GitHub Desktop.
Save l-modolo/e96a20ff75b022d62f77 to your computer and use it in GitHub Desktop.
Daemonise rtorrent with screen with the file /etc/init.d/rtorrent
#!/bin/bash
### BEGIN INIT INFO
# Provides: rtorrent
# Required-Start: $local_fs $remote_fs $network $syslog $netdaemons
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: rtorrent script using screen(1)
# Description: rtorrent script using screen(1) to keep torrents working without the user logging in
### END INIT INFO
# config
user="pi"
group=$(id -ng "$user")
config=("$(su -c 'echo $HOME' $user)/.rtorrent.rc")
options=("")
base=$(su -c 'echo $HOME' $user)
srnname="rtorrent"
logfile="/var/log/rtorrentInit.log"
# script
PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin
DESC="rtorrent"
NAME=rtorrent
DAEMON=$NAME
SCRIPTNAME=/etc/init.d/$NAME
case "$1" in
start)
echo "Starting $DESC: $NAME"
[ -d "${base}" ] && cd "${base}"
stty stop undef && stty start undef
su $user -c 'screen -S rtorrent -t rtorrent -d -m rtorrent' &> /dev/null
if [ $? -gt 0 ]; then
echo "Failed to start $DESC: $NAME"
else
echo "Started $DESC: $NAME"
fi
;;
stop)
echo "Stopping $DESC: $NAME"
killall -w -s 2 rtorrent &> /dev/null
screen -wipe
if [ $? -gt 0 ]; then
echo "Failed to stop $DESC: $NAME"
else
echo -n "Stopped $DESC: $NAME"
fi
;;
restart|force-reload)
$0 stop
sleep 1
$0 start
;;
*)
echo "usage: $0 {start|stop|restart|force-reload}"
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment