Advertisement
fata1ex

Untitled

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