Start rTorrent Automatically at Boot on Debian/Ubuntu

I tend to use ruTorrent as a front-end for rTorrent and so I like to have rTorrent start silently and automatically on boot. Most of the guides on this subject seem to suggest using an init script placed in /etc/init.d, but this is more complicated than it needs to be in my opinion. You can use start-stop-daemon instead which is much simpler and yields the same result. Here’s how.

First install dtach:

1
sudo apt-get install dtach

Then edit your /etc/rc.local file:

1
sudo nano /etc/rc.local

By default this file looks something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0

Add the following before exit 0, making sure to replace karl with the user as which you would like to run rTorrent, and /usr/local/bin/rtorrent with the location of rTorrent on your system (/usr/bin/rtorrent if you installed from the repo, /usr/local/bin/rtorrent if you compiled rTorrent from source with the default install path):

1
start-stop-daemon --start --chuid karl --name rtorrent --exec /usr/bin/dtach -- -n /tmp/rtorrent.dtach /usr/local/bin/rtorrent

Your /etc/rc.local should now look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

start-stop-daemon --start --chuid karl --name rtorrent --exec /usr/bin/dtach -- -n /tmp/rtorrent.dtach /usr/local/bin/rtorrent

exit 0

Press ctrl+o to save the file and ctrl+x to quit nano.

That’s it! To test you can either reboot, or else manually run the rc.local script:

1
sudo /etc/rc.local

If you don’t use an rTorrent front end, or want to view rTorrent in a terminal for whatever reason, you can reattach it to your terminal with the following command:

1
dtach -a /tmp/rtorrent.dtach

You can either press ctrl+z or simply close the terminal to quit dtach (rTorrent will keep running in the background).

5 comments

  • Kristijan

    Thanks it is working great !

  • Gerhard

    Thanks for the howto! It works fine apart from the problem that when you reattaches the arrow keys don’t work (http://libtorrent.rakshasa.no/ticket/192).
    The solution for this is setting env TERM=linux.
    I tried inserting this in the start-stop-daemon command but I can’t get it working. Any advice?
    Also if you shutdown or reboot next time you start it has to hashcheck all the torrents, to prevent this add ‘kill -2 $(pgrep rtorrent)’ to the /etc/rc0.d and /etc/rc6.d .

  • inverse

    And How to: restart, stop and start rtorrent whit dtach??

  • RamZesS

    Hello ppl
    I got It working in my raspbery pi (debian wheezy)
    To be able not only to start but also to stop gracefully,
    I created start/stop scripts according to instruction here
    http://crunchbang.org/forums/viewtopic.php?id=14453

    I created a user rtorrent and put configuration (.rtorrent.rc) into his home

    then I added to my /etc/rc.local.startup

    1
    2
    3
    4
    5
    6
    7
    8
    echo -n "start rtorrent"
    usr=rtorrent
    bittorrent=/usr/bin/rtorrent
    process=rtorrent
    #v=--verbose
    TERM=linux
    start-stop-daemon --start ${v} --oknodo --pidfile /var/run/${process}.pid --chuid ${usr} --umask 0002 --name ${process} --chdir $(eval echo ~${usr}) --exec /usr/bin/dtach -- -n /tmp/${usr}.dtach ${bittorrent}
    echo "."

    and to my /etc/rc.local.shutdown

    1
    2
    3
    4
    5
    6
    7
    echo -n "stop rtorrent"
    usr=rtorrent
    process=rtorrent
    bittorrent=/usr/bin/rtorrent
    #v=--verbose
    start-stop-daemon ${v} --stop --oknodo --chuid ${usr} --signal INT --name ${process}
    echo "."

    Here is a small explaination:
    -chdir $(eval echo ~${usr}) - run rtorren in the home dir of the rtorrent user
    -signal INT ask rtorrent nicely to quit

  • RamZesS

    .. more comments
    after you attached to session with

    1
    dtach -a /tmp/rtorrent.dtach

    press ^\ (Ctrl-\) to leave the sassion again. rtorrent is continue working, you can re-attach to it again later
    BTW; you can redifine it with dtach option -e .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>