FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

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


Steps to get started

The gentoo version of the script
Code:

#!/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
Code:

#!/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
Code:

#!/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.



1 blog comments below

This is how I did it on xubuntu 10.04, but should work on other distros:

Code:

#! /bin/bash
### BEGIN INIT INFO
# Provides: rtorrent
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: rtorrent
# Description: rtorrent client running on background using screen
### END INIT INFO

. /lib/lsb/init-functions

[ -f /etc/default/rcS ] && . /etc/default/rcS
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc

test -x $DAEMON || exit 0

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

case $1 in
start)
#start the process and record record it's pid
start-stop-daemon --start --background --pidfile /var/run/rtorrent.pid --make-pidfile --chuid user --exec /usr/local/bin/rtorrent --startas /usr/bin/screen -- -dmUS rtorrent /usr/local/bin/rtorrent
if [[ $? -eq 0 ]]; then echo "Rtorrent successfuly started. Use screen -r rtorrent to attach." ; else echo "Something went wrong during startup. Return code:" $?;fi;
;;
stop)
#cannot stop the process using pid because inside pid file is different pid, dont ask why - dont know. So using name of process
start-stop-daemon --stop --name rtorrent --exec /usr/local/bin/rtorrent --retry 5 --quiet
if [[ $? -eq 0 ]]; then echo "Rtorrent successfuly stoped." ; else echo "Something went wrong during stoping. Return code:" $?;fi;
;;

*)
# show the options
echo "Usage: {start|stop}"
;;
esac
hraju on Mon Aug 02, 2010 11:40 pm



FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.