Advertisement
Powderking

rtorrent

Jun 26th, 2011
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. $ cat /etc/init.d/rtorrent
  2. #!/bin/sh
  3. #############
  4. ###<Notes>###
  5. #############
  6. # This script depends on screen.
  7. # For the stop function to work, you must set an
  8. # explicit session directory using ABSOLUTE paths (no, ~ is not absolute) in your rtorrent.rc.
  9. # If you typically just start rtorrent with just "rtorrent" on the
  10. # command line, all you need to change is the "user" option.
  11. # Attach to the screen session as your user with
  12. # "screen -dr rtorrent". Change "rtorrent" with srnname option.
  13. # Licensed under the GPLv2 by lostnihilist: lostnihilist _at_ gmail _dot_ com
  14. ##############
  15. ###</Notes>###
  16. ##############
  17.  
  18. #######################
  19. ##Start Configuration##
  20. #######################
  21. # You can specify your configuration in a different file
  22. # (so that it is saved with upgrades, saved in your home directory,
  23. # or whateve reason you want to)
  24. # by commenting out/deleting the configuration lines and placing them
  25. # in a text file (say /home/user/.rtorrent.init.conf) exactly as you would
  26. # have written them here (you can leave the comments if you desire
  27. # and then uncommenting the following line correcting the path/filename
  28. # for the one you used. note the space after the ".".
  29. # . /etc/rtorrent.init.conf
  30.  
  31. #Do not put a space on either side of the equal signs e.g.
  32. # user = user
  33. # will not work
  34. # system user to run as
  35. user="hoferr"
  36.  
  37. # the system group to run as, not implemented, see d_start for beginning implementation
  38. # group=`id -ng "$user"`
  39.  
  40. # the full path to the filename where you store your rtorrent configuration
  41. config="`su -c 'echo $HOME' $user`/.rtorrent.rc"
  42.  
  43. # set of options to run with
  44. options=""
  45.  
  46. # default directory for screen, needs to be an absolute path
  47. base="`su -c 'echo $HOME' $user`"
  48.  
  49. # name of screen session
  50. srnname="rtorrent"
  51.  
  52. # file to log to (makes for easier debugging if something goes wrong)
  53. logfile="/var/log/rtorrentInit.log"
  54. #######################
  55. ###END CONFIGURATION###
  56. #######################
  57. PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin
  58. DESC="rtorrent"
  59. NAME=rtorrent
  60. DAEMON=$NAME
  61. SCRIPTNAME=/etc/init.d/$NAME
  62.  
  63. checkcnfg() {
  64. exists=0
  65. for i in `echo "$PATH" | tr ':' '\n'` ; do
  66. if [ -f $i/$NAME ] ; then
  67. exists=1
  68. break
  69. fi
  70. done
  71. if [ $exists -eq 0 ] ; then
  72. echo "cannot find rtorrent binary in PATH $PATH" | tee -a "$logfile" >&2
  73. exit 3
  74. fi
  75. if ! [ -r "${config}" ] ; then
  76. echo "cannot find readable config ${config}. check that it is there and permissions are appropriate" | tee -a "$logfile" >&2
  77. exit 3
  78. fi
  79. session=`getsession "$config"`
  80. if ! [ -d "${session}" ] ; then
  81. echo "cannot find readable session directory ${session} from config ${config}. check permissions" | tee -a "$logfile" >&2
  82. exit 3
  83. fi
  84. }
  85.  
  86. d_start() {
  87. [ -d "${base}" ] && cd "${base}"
  88. stty stop undef && stty start undef
  89. 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
  90. # this works for the screen command, but starting rtorrent below adopts screen session gid
  91. # even if it is not the screen session we started (e.g. running under an undesirable gid
  92. #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
  93. su -c "screen -S "${srnname}" -X screen rtorrent ${options} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2
  94. }
  95.  
  96. d_stop() {
  97. session=`getsession "$config"`
  98. if ! [ -s ${session}/rtorrent.lock ] ; then
  99. return
  100. fi
  101. pid=`cat ${session}/rtorrent.lock | awk -F: '{print($2)}' | sed "s/[^0-9]//g"`
  102. if ps -A | grep -sq ${pid}.*rtorrent ; then # make sure the pid doesn't belong to another process
  103. kill -s INT ${pid}
  104. fi
  105. }
  106.  
  107. getsession() {
  108. session=`cat "$1" | grep "^[[:space:]]*session[[:space:]]*=" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//" `
  109. echo $session
  110. }
  111.  
  112. checkcnfg
  113.  
  114. case "$1" in
  115. start)
  116. echo -n "Starting $DESC: $NAME"
  117. d_start
  118. echo "."
  119. ;;
  120. stop)
  121. echo -n "Stopping $DESC: $NAME"
  122. d_stop
  123. echo "."
  124. ;;
  125. restart|force-reload)
  126. echo -n "Restarting $DESC: $NAME"
  127. d_stop
  128. sleep 1
  129. d_start
  130. echo "."
  131. ;;
  132. *)
  133. echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
  134. exit 1
  135. ;;
  136. esac
  137.  
  138. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement