Running rtorrent as a daemon

A long time ago, I suggested rtorrent was something like an “rtorrent daemon.” That’s still true, of course — I don’t know of any torrent client that runs lighter and has the same range of controls as rtorrent.

I never really tried to set it up as a true daemon though, until I saw this thread on the Arch Linux forums, and its corresponding wiki page. It might not sound like it’s worth the effort, but it’s kind of nice to run the entire system in the background, unattended and preconfigured, and just drop torrents and run.

Mimicking the same concept in Crux was fairly easy; I needed to add a user called rtorrent, set up the proper subdirectories and then configure rtorrent’s rc file as I liked. The script shown in the thread and on the wiki needed scraped down a little bit, to where it’s really just a simple series of start and stop commands. (Oh, and the daemon needs either screen or dtach, depending on which you prefer.)

I don’t know if I’d go through the work of setting this up every time I wanted to install rtorrent, but I can see where this would be useful on a remote machine, or on a machine that sits by itself and does nothing but download. Oh wait, that was my original point, wasn’t it? :mrgreen:

9 thoughts on “Running rtorrent as a daemon

  1. fuxter

    i was surprised to see that installation of rtorrent for ubuntu does not come with /etc/init.d/rtorrent by default!
    i just got used to http://oleg.wl500g.info/ oleg’s custom firmware (wich is linux of course) for my router. when i installed rtorrent for the router i came with /etc/init.d/rtorrent and /etc/rtorrent.conf. all i had to do is create a new user “p2p” and configure directories and some settings to my likes.
    so for my ubuntu i just mimicked the same behavior.
    oh, and there’s special alias “rt” that sues to p2p user and screens -r the rtorrent session. and that’s the best part. but it come a bit annoying when you connect to remote rtorrent session being in screen yourself. always knocks me off my local screen when i wanted to detach remote one. ^a+a saves the day though.

    Reply
  2. gfg

    Why don’t you use transmission-daemon? it has a nice web gui, and you can use it over ssh with transmission-client

    Reply
  3. karthik

    I’ve been running rtorrent in a detatched screen session for ages now- but I often forget to quit rtorrent before powering off the PC. On the next restart, rtorrent forces a hash check that slows down all activity and locks up the PC.

    Thanks to this guide, I’m one step closer to fire and forget. Thanks!

    Reply
  4. lindylex

    Use this for running rtorrent as a daemon with Debian.

    #!/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
    
    #!/bin/sh
    
    #############
    
    ######
    
    #############
    
    # 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
    
    ##############
    
    ######
    
    ##############
    
    #######################
    
    ##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
    
    #Do not put a space on either side of the equal signs e.g.
    
    # user = user 
    
    # will not work
    
    # system user to run as
    
    user="usernamerunningrtorrent"
    
    # 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="/home/usernamerunningrtorrent/.rtorrent.rc"
    
    # set of options to run with
    
    options=""
    
    # default directory for screen, needs to be an absolute path
    
    #base="/home/${user}"
    
    base="/home/usernamerunningrtorrent"
    
    # 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###
    
    #######################
    
    PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin
    
    DESC="rtorrent"
    
    NAME=rtorrent
    
    DAEMON=$NAME
    
    SCRIPTNAME=/etc/init.d/$NAME
    
    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
    
    }
    
    d_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
    
      # 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 -ls | grep -sq "\.${srnname}[[:space:]]" " ${user} || su -c "sg \"$group\" -c \"screen -fn -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
    
    }
    
    d_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"`
    
    	if ps -A | grep -sq ${pid}.*rtorrent ; then # make sure the pid doesn't belong to another process
    
    		kill -s INT ${pid}
    
    	fi
    
    }
    
    getsession() { 
    
    	session=`awk '/^[[:space:]]*session[[:space:]]*=[[:space:]]*/{print($3)}' "$config"`
    
    	echo $session
    
    }
    
    checkcnfg
    
    case "$1" in
    
      start)
    
    	echo -n "Starting $DESC: $NAME"
    
    	d_start
    
    	echo "."
    
    	;;
    
      stop)
    
    	echo -n "Stopping $DESC: $NAME"
    
    	d_stop
    
    	echo "."
    
    	;;
    
      restart|force-reload)
    
    	echo -n "Restarting $DESC: $NAME"
    
    	d_stop
    
    	sleep 1
    
    	d_start
    
    	echo "."
    
    	;;
    
      *)
    
    	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
    
    	exit 1
    
    	;;
    
    esac
    
    exit 0
    Reply
    1. K.Mandla Post author

      I put sourcecode tags on your script, in the hopes that WordPress.com doesn’t mangle the code. Cheers and thanks. šŸ˜‰

      Reply
  5. lindylex

    A little more instruction for the beginners.

    1. Go to the commandline
    2. aptitude update
    3. aptitude install tofrodos nano
    4. Log in as root or use su and continue with 4. Alternative minus using the su in front of each command.
    su [ Enter root password ]
    4. Alternative using su
    su nano /etc/init.d/rtorentdaemon

    This will open the text editor nano if installed to create and edit this file /etc/init.d/rtorentdaemon

    Place the contents from above into the file and edit these lines to match the user you plan to run rtorrent as.

    user=ā€usernamerunningrtorrentā€

    config=ā€/home/usernamerunningrtorrent/.rtorrent.rcā€

    base=ā€/home/usernamerunningrtorrentā€

    An example of this file “config=ā€/home/usernamerunningrtorrent/.rtorrent.rcā€” can be retrieved here.

    http://libtorrent.rakshasa.no/browser/trunk/rtorrent/doc/rtorrent.rc?rev=latest

    Copy it from the website into a Linux editor and save. Don’t download directly since it was create with Windows it will bomb with lines break issues. You can use the dos2unix command to convert it if you download the file directly like this.

    dos2unix /home/usernamerunningrtorrent/.rtorrent.rc

    Reply
  6. partimers

    Is it possible to have a daemon script to start rtorrent for multiple users? I am planning to migrate my tflux-b4rt seedbox (shared with other 6 friends of mine) to rutorrent. It is a Debian box, and I would like to have rtorrent running on daemon mode, starting one session per user.

    Reply

Leave a reply to lindylex Cancel reply