Start rtorrent using init scripts

Searching around the net I came across 2 articles which helped in writing the init script presented below.
There are 3 versions of the script – one for gentoo based distros one for debian based distros and one for Fedora, which is quite similar to debian one.
Requirements for running the scripts

  • rtorrent
  • screen
  • adequate knowledge to operate rtorrent and screen

Steps to get started

  • Copy the contents of the script for your distro to a file
  • Save it with the name indicated in the script
  • Make the file executable with following command

chmod +x /path/to/file

  • Add it to your default boot level as indicated in the comments
  • Change the TUSER value from username to your username
  • Optionally you can change rtorrent to some other bittorrent client depending on your preference. NOTE:- It should be a CLI bittorrent client

The gentoo version of the script

#!/sbin/runscript

# To add the script automatically at the startup execute the following command in the terminal
# eselect rc add rtorrent default

RTUSER=username
TORRENT=/usr/bin/rtorrent

opts=”start stop”

depend() {
#this daemon needs internet to function
need net.eth0
use logger
}

start() {
#display to user that what is being started
ebegin “Starting rtorrent”
#start the process and record record it’s pid
start-stop-daemon –start –background –pidfile /var/run/rtorrent.pid –make-pidfile –exec su -c “/usr/bin/screen -dmUS torrent $TORRENT” $RTUSER
# To interact with rtorrent you will need to reattach the screen
einfo “To use your rtorrent session, reattach screen using following command:”
einfo “screen -r torrent”
#output failure or success
if [[ $? -eq 0 ]]; then
if [[ $? -eq 0 ]]; then
eend “Process started successfully”
else
eend “Process failed to start”
}

stop () {
#display that we are stopping the process
ebegin “Stopping rtorrent”
#stop the process using pid from start()
start-stop-daemon –stop –pidfile /var/run/rtorrent.pid –name rtorrent
#output success or failure
if [[ $? -eq 0 ]]; then
eend “Process stopped successfully”
else
eend “Process failed to stop”
}

Debian version

#!/bin/bash

# To start the script automatically at bootup type the following command
# update-rc.d torrent defaults 99

RTUSER=username
TORRENT=/usr/bin/rtorrent

case $1 in
start)
#display to user that what is being started
echo “Starting rtorrent”
#start the process and record record it’s pid
start-stop-daemon –start –background –pidfile /var/run/rtorrent.pid –make-pidfile –exec su -c “/usr/bin/screen -dmUS torrent $TORRENT” $RTUSER
#output failure or success
#info on how to interact with the torrent
echo “To interact with the torrent client, you will need to reattach the screen session with following command”
echo “screen -r torrent”
if [[ $? -eq 0 ]]; then
echo “The process started successfully”
else
echo “The process failed to start”
;;

stop)
#display that we are stopping the process
echo “Stopping rtorrent”
#stop the process using pid from start()
start-stop-daemon –stop –pidfile /var/run/rtorrent.pid –name rtorrent
#output success or failure
if [[ $? -eq 0 ]]; then
echo “The process stopped successfully”
else
echo “The process failed to stop”
;;

*)
# show the options
echo “Usage: {start|stop}”
;;
esac

Seems that debian version will work for Fedora too. Still, here’s the script

#!/bin/bash

# To start the script automatically at bootup type the following command
# chkconfig add torrent

RTUSER=username
TORRENT=/usr/bin/rtorrent

case $1 in
start)
#display to user that what is being started
echo “Starting rtorrent”
#start the process and record record it’s pid
start-stop-daemon –start –background –pidfile /var/run/rtorrent.pid –make-pidfile –exec su -c “/usr/bin/screen -dmUS torrent $TORRENT” $RTUSER
#output failure or success
#info on how to interact with the torrent
echo “To interact with the torrent client, you will need to reattach the screen session with following command”
echo “screen -r torrent”
if [[ $? -eq 0 ]]; then
echo “The process started successfully”
else
echo “The process failed to start”
;;

stop)
#display that we are stopping the process
echo “Stopping rtorrent”
#stop the process using pid from start()
start-stop-daemon –stop –pidfile /var/run/rtorrent.pid –name rtorrent
#output success or failure
if [[ $? -eq 0 ]]; then
echo “The process stopped successfully”
else
echo “The process failed to stop”
;;

*)
# show the options
echo “Usage: {start|stop}”
;;
esac

One thing I need to implement is to show status. If it is started or stopped. Reload or restart doesn’t seem to make much sense to me thus I haven’t implemented those.
This is only the initial version it may have bugs. Please report them so they can be corrected.
Debian and fedora users please try it and report if it works.
For user of other distros, if interested, please provide some idea of your distro’s init system or attach some init script so that I can work on it.
For people familiar with init system, it would be great to post your modifications/improvements/bug fixes/ideas to this script.

13 thoughts on “Start rtorrent using init scripts

  1. Aditya

    Thanks a lot… I had been looking for this thing for a LONG time now. I could add it to session startup programs, but that would have meant adding them to all the DEs I used. And that also, did never start downloading properly (dunno… maybe problem with what I had done).

    This method is way better. Thanks…

  2. pacman

    That (debian) script doesn’t work. All parameters need to be used with double dashes: –start not -start
    –background not -background

    also you are using weird quotes

  3. mehul

    pacman, thank you for your comments. I am not planning to migrate from wordpress anytime soon but I will upload the init scripts in text files so people can just download it right away and use it 🙂
    I will stull check up if my init scripts do have this problem.

  4. gaspar

    Hi!
    I copied Debian-version of the script and getting error:

    Executing /etc/init.d/torrent start ..
    /etc/init.d/torrent: line 23: syntax error near unexpected token `;;’
    /etc/init.d/torrent: line 23: `;;’

    Can You post text-version of this script?

  5. lattenwald

    Hello there.
    I am using these scripts on two machines, Gentoo and Debian.
    I had to change some lines to get it working.

    About the problems with your script (for Debian):
    1. ‘if’ statements do not have corresponding ‘fi’ (just a typo)
    2. ‘screen -dm’ starts in background. We need to start it in foreground to get the real pid with ‘–make-pidfile’ option of start-stop-daemon and then put it to background with ‘–background’ option of start-stop-daemon.
    3. start-stop-daemon have an option of ‘–chuid’, so no need in using ‘su -c’ or ‘sudo -u’
    4. we don’t need ‘–name’ in ‘start-stop-daemon –stop’ as we do have the real pid

    Here’s the script that worked for me on Debian (with comments stripped out)

    #!/bin/sh
    RTUSER=username
    RTORRENT=/usr/bin/rtorrent

    case “$1” in
    start)
    echo “Starting rtorrent”
    start-stop-daemon –start –background –pidfile /var/run/rtorrent.pid –make-pidfile –chuid $RTUSER –exec /usr/bin/screen — -DmUS torrent $RTORRENT
    echo “To interact with the torrent client, you will need to reattach the screen session with following command”
    echo “screen -r torrent”
    if [[ $? -eq 0 ]]; then
    echo “The process started successfully”
    else
    echo “The process failed to start”
    fi
    ;;
    stop)
    echo “Stopping rtorrent”
    start-stop-daemon –stop –pidfile /var/run/rtorrent.pid
    if [[ $? -eq 0 ]]; then
    echo “The process stopped successfully”
    else
    echo “The process failed to stop”
    fi
    ;;
    *)
    echo “Usage: {start|stop}”
    ;;
    esac

    Every single option for start-stop-daemon and screen has a sense.
    Script for Gentoo needs such changes too.

    Well, at least that’s the way it worked for me.

  6. mehul

    lattenwald thanks. this is great 🙂
    I will hopefully put up a text files with your modified script, if so allowed by you. So, that people can just download the file and use them.

  7. Peter

    This is useless. Looking great, but if I could clean up your smart quotes and dashes, I’d not need your script in the first place.

Leave a Reply to lattenwald Cancel reply

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