Advertisement
dropbox1349

rtorrent autostart script

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