# rtorrent daemon

## roog

Hi All,

rtorrent is a nice and fast text-based ncurses BitTorrent client and is ideal for use with dtach (or screen). I wanted to run rtorrent as a daemon on my server allowing it to:

- start automatically at boot

- connect to using ssh

- watch for torrents in a specified directory

- move completed torrents to a different directory

To do this, make sure you have the latest rtorrent (0.7.8 at the time of this writing, currently keyworded ~arch in portage) and dtach installed. Copy-paste the following code into a file called /etc/init.d/rtorrent:

```

#!/sbin/runscript

# Copyright 2007 Rogier Koppejan

# Distributed under the terms of the GNU General Public License v2

depend() {

        use net

}

start() {

        ebegin "Starting rtorrent"

        start-stop-daemon --start --chuid $USER \

            --env TERM="xterm" \

            --env HOME="/home/$USER" \

            --exec $DTACH -- -n $SOCKET $RTORRENT

        eend $?

}

stop() {

        ebegin "Stopping rtorrent"

        start-stop-daemon --stop --signal 2 --name rtorrent

        eend $?

}

```

The configuration options go into /etc/conf.d/rtorrent, adjust the options to your personal needs:

```

# /etc/conf.d/rtorrent: config file for /etc/init.d/rtorrent

# Options to pass to the rtorrent daemon.

# The username/uid to start rtorrent with

USER="username"

# Path to the dtach binary (needs to be absolute path)

DTACH="/usr/bin/dtach"

# The socket for the rtorrent

SOCKET="/tmp/rtorrent.dtach"

# Path to the rtorrent binary (needs to be absolute path)

RTORRENT="/usr/bin/rtorrent"

```

Now create and configure your ~/.rtorrent.rc. A sample can be found here, here's mine:

```

# Maximum and minimum number of peers to connect to per torrent.

min_peers = 20

max_peers = 200

# Maximum number of simultanious uploads per torrent.

max_uploads = 20

# Global upload and download rate in KiB. "0" for unlimited.

download_rate = 0

upload_rate = 40

# Session directory where rtorrent will save torrent info.

session = ~/.rtorrent.session

# Default directory to save the downloaded torrents.

directory = ~/Download/torrents/active/

# Watch a directory for new torrents and stop those that 

# have been deleted.

schedule = watch_directory,5,5,load_start=~/Download/torrents/active/*.torrent

schedule = untied_directory,5,5,remove_untied=

# When the torrent finishes, remove torrent and move downloaded 

# files to ~/Download/torrents/finished/.

on_finished = rm_torrent,"execute=rm,$d.get_tied_to_file="

on_finished = move_complete,"execute=mv,-u,$d.get_base_path=,~/Download/torrents/finished/ ;d.set_directory=~/Download/torrents/finished/"

# Close torrents when diskspace is low.

schedule = low_diskspace,5,60,close_low_diskspace=100M

# Stop torrents when reaching upload ratio in percent,

# when also reaching total upload in bytes, or when

# reaching final upload ratio in percent.

schedule = ratio,60,60,"stop_on_ratio=100"

# Port range to use for listening.

port_range = 6891-6900

# Scheduling upload rates.

schedule = throttle_day,10:00:00,24:00:00,upload_rate=30

schedule = throttle_night,20:00:00,24:00:00,upload_rate=40

```

Then add rtorrent to de default runlevel, exec as root:

```
$ rc-update add rtorrent default
```

You can attach to rtorrent and view its status executing:

```
dtach -a /path/to/socket
```

or

```
dtach -A /path/to/socket rtorrent
```

Type Ctrl+\ to detach rtorrent again. Finally, I've created the following alias in my ~/.bashrc to make my life easier:

```
alias rtorrent='dtach -A /path/to/socket /usr/bin/rtorrent'
```

This command will connect to rtorrent or restart it in case it died for some reason (which doesn't ever happen as far as I can tell, but just in case).

That's it! You're all set. Torrents can now be uploaded from anywhere to your watch directory (local or remote using scp, ftp, ...) and you can monitor rtorrents status at any time (also local or remote). 

Happy downloading!

PS) The daemon script could be enhanced by using pidfiles. However, when I try this with -m --pidfile /path/to/pidfile, the pid of dtach instead of rtorrent is stored. If anyone knows how to solve this, please tell me!

[Edit1]: Added the line --env TERM="xterm" to the init script.

[Edit2]: Included rtorrent.rc info.Last edited by roog on Thu Nov 08, 2007 8:40 am; edited 7 times in total

----------

## nixnut

Moved from Gentoo Chat to Documentation, Tips & Tricks.

----------

## Oak

Excellent, thank you. Unfortunately I can't help you improving it though. I just wanted to thank you.

 :Smile: 

----------

## roog

I've made a small update to the init-script. During init, TERM is set to linux, so dtach is started with the TERM=linux environment. When you reattach to the rtorrent dtach session after the boot process, TERM is usually set to xterm, and this will result in weird keybinding behaviour. For instance the ALT key seems to be activated continuously and the up/down/left/right-keys can no longer be used for navigating in rtorrent. Look here and here for more info.

In short, the line

```
 --env TERM="xterm" \
```

was added to the startup script. Now everything seems to be fine again.

----------

## Oak

 *Quote:*   

> For instance the ALT key seems to be activated continuously and the up/down/left/right-keys can no longer be used for navigating in rtorrent.

 

Thanks for the update!

----------

## 59729

Hmmm I will have a look at this when I get the time, would be cool if it could repair, extract and move the files when they are downloaded to.

----------

## toaster.waffle

I'm interested in seeing a daemon using detached screen sessions and with support for multiple instances of rtorrent.  Right now I'm just using /etc/conf.d/local.start but would like to see a solution from someone who knows more about how to do things properly than I do.

Good work nonetheless  :Smile: 

P.S. (for the curious...)

```

# Wipe dead screens.

su - limiteduser -c "exec screen -wipe" &> /dev/null

# Start first instance of rtorrent in screen with different settings than those in ~/.rtorrent.rc

su - limiteduser -c "exec screen -dm rtorrent -n -p 45320-45320 -s ~/Music/.rtorrent-session/ -d ~/Music/ -o download_rate=0,upload_rate=20"

# Start rtorrent with settings from ~/.rtorrent.rc

su - limiteduser -c "exec screen -dmS rtorrent rtorrent"

```

----------

## anxt

Just recently I learned the bash shell (and i think ksh) has a disown builtin.  So you can remove a job from list and close shell, no dtach or screen.  Probably not relevant but interesting noetheless.  I learn a little unix every day.

----------

## RaceTM

Is anyone else having an issue with torrents having their hash check fail and thus stop the download?  If I restart the daemon it resumes fine, however I haven't been able to successfully get the rtorrent daemon to restart using crontab.  It's very frustrating...I tried everything from /etc/init.d/rtorrent restart, to doing a killall rtorrent every hour and /etc/init.d/rtorrent start a minute later.  Nothing seems to work.  Can anyone make a suggestion?

----------

## anxt

perhaps you should look at dmesg for CRC errors?  rtorrent has run so well for me, so much i lose a session and realize i shared (gentoo) isos at like 50 to 1 ratio  :Smile:   you shouldn't get very many hash failures unless you are being fed deliberately bad data.  if it checks good on download, it should remain that way.

(or the data on your disk is failing)

----------

## lazx888

So you download the torrent to an active directory, and when finished, remove the torrent and move the files to a completed directory - no seeding?

Or does schedule = ratio,60,60,"stop_on_ratio=100" determine when the torrent is "finished"??

Great work (if seeding still occurs on completion of download)

----------

## pdw_hu

How about including encryption and DHT in your guide, they won't hurt even if it's not being used.

```
dht = auto

dht_port = 666 the port of the beast ;)

encryption = allow_incoming,try_outgoing,enable_retry
```

----------

## danomac

 *lazx888 wrote:*   

> So you download the torrent to an active directory, and when finished, remove the torrent and move the files to a completed directory - no seeding?
> 
> Or does schedule = ratio,60,60,"stop_on_ratio=100" determine when the torrent is "finished"??
> 
> Great work (if seeding still occurs on completion of download)

 

The stop_on_ratio command tells rtorrent to stop seeding torrents. The above means to stop seeding at 100%, i.e. upload the same as you've downloaded.

----------

## kuku

 *danomac wrote:*   

>  *lazx888 wrote:*   So you download the torrent to an active directory, and when finished, remove the torrent and move the files to a completed directory - no seeding?
> 
> Or does schedule = ratio,60,60,"stop_on_ratio=100" determine when the torrent is "finished"??
> 
> Great work (if seeding still occurs on completion of download) 
> ...

 

yes - on complete download there is no seeding - any idea how to change it ?

I want to delete the *.torrent file when let's say ratio is 2.0 and when it does not occur then after 1 week

----------

## timbo

Hey great work with the how-to...

This app is just what I wanted bar one aspect, I want it to not download or upload anything in the evening, our ISP has free downloads (doesn't get added to your cap) durring the wee hours and thats when I want to do the downloading, can this be done with rtorrent, I can't see how.  

I like being able to dump a .torrent file into a directory and it's magically downloaded, is there another app that can do this?

Regards

Tim

 :Cool: 

----------

## vputz

Well, you can get close to that, from the faq:

 *Quote:*   

> Scheduling download rate ¶
> 
>  *Quote:*   schedule = throttle_1,01:00:00,24:00:00,download_rate=0
> 
> schedule = throttle_2,05:00:00,24:00:00,download_rate=25 
> ...

 

----------

## DocterD

Just for my own curiosity. Why do you not use BTG instead of rTorrent?

----------

## Oak

 *DocterD wrote:*   

> Just for my own curiosity. Why do you not use BTG instead of rTorrent?

 

The reason I stopped using BTG and reverted back to rTorrent, was that there weren't any way to specify which files to download from a torrent.

Some of the torrents that I download contains files that I don't want, which makes BTG unusable.

The minute that this feature is added, I'll switch back to BTG...

----------

## DocterD

Do you mean over the Webinterface? It has been added to the 0.9.8 Release.

----------

## Oak

Alright! Time to unmerge rtorrent...

Is version 0.9.8 available in some of the overlays?

[EDIT]

I created my own from the 0.9.7 ebuild, which complains about libboost_iostream. Version 0.9.7 complains about memset and memcpy from the stdlib...

A working 0.9.8 ebuild would be really really nice!

The 0.9.8 problem occurs in the linking process and seems to be a known problem. It says "recompile with -fPIC", which probably is meant for boost, but I can't figure out which use flag that enables this, if any.

[/EDIT]

----------

## DocterD

Yeah i got the same Problem with compiling rb_libtorrent-0.13.1

----------

## Oak

 *DocterD wrote:*   

> Yeah i got the same Problem with compiling rb_libtorrent-0.13.1

 

What about version 0.13? For me it compiles just fine, but unfortunately, btg still doesn't

[EDIT]

I finally got btg-0.9.7 to compile after modifying a couple of source files and disabling the gtk2 use flag. Since I've never created a patch before, I simply created an archive with the new files and a small shell script to insert them. If someone finds it useful, you're very welcome to create a patch from it. I'm using rb_libtorrent-v0.13 and gcc-4.3.1 by the way

http://www.oakstream.org/files/btg-0.9.7-header_fix.tar.gz

[/EDIT]

----------

## Mounir Lamouri

I've written a btg-0.9.8 ebuild (see bug link at the end of the message) and Dennis told me there was a compilation error.

After reading this doc : http://www.gentoo.org/proj/en/base/amd64/howtos/index.xml?part=1&chap=3

and as I see you and Dennis have a 64 bits system, I think it's a 64 bits related issue with -fPIC.

Actually, it looks more like a boost issue than a btg one but I don't see any other similar bug...

So, I suppose it can be managed only on btg but I really don't know how. Does someone have an experience here ?

Note : may be this topic should be cut by a moderator...?

or should this discussion continued here : https://bugs.gentoo.org/show_bug.cgi?id=231105

----------

## Oak

Actually, the "-fPIC problem" isn't really a problem anymore, at least for me. As stated in my last post, the only problem left is the missing headers in btg which are easy to fix.

I'll try the 0.9.8 version later today and report back here as soon as possible.

----------

## Mounir Lamouri

Dennis Oberhoff told me 0.9.7 was compiling correctly on is system so I supposed -fPIC error appeared with 0.9.8. May be with boost 1.35 ?

Did you had fPIC error with 0.9.7 ebuild ?

----------

## Zucca

If you have cron installed you can always add someting like this to crontab:

```
@reboot screen -dmt "torrent transfers" rtorrent
```

It should work. :) -t switch just adds the window title, which can be left out.

----------

## Oak

Since this thread is about rtorrent and not btg, I think we should continue our btg discussion here: https://forums.gentoo.org/viewtopic-p-5153287.html#5153287

----------

## Zucca

Hm. Isn't there a GUI frontends for rtorrent? Just a thought that is it possible to connect with rtorrent GUI frontend to a rtorrent that is running inside a dtach or screen?

----------

## Mounir Lamouri

Google : "rtorrent gui" will provide some interesting results.

But you should look at btg because its server/client design is totally appropriate.

----------

## Zucca

I like _very_ much the rtorrent design. And all the possible cuntomizations in it.

I've used it on a 900Mhz Pentium III / 340Mb RAM machine where KDE was running. It has amazing low memory usage and most of the time it wont bother your cpu too much. ;)

Now you may see why I'm not keen to change from it... But there is no reason to stay with rtorrent if some other client provides better resource usage (can't really get much better) or user interface.

One thing I found kinda tricky in rtorrent is that you cannot easily change the download destination directory for one individual torrent download that is already started (I just save *.torrent -files to a directory where rtorrent finds then and starts downloading automatically).

----------

## viperlin

EDIT:

ignore this  - just slap me for causing you an email, i had a conflict of aliases and scripts (ok i didnt source .bashrc after modifications)

----------

