Guest User

Untitled

a guest
Feb 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 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 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. # If you are running multiple instances of rtorrent,
  13. # all options should be made respective to one another. so the first option for
  14. # config should be related to the same first option for options.
  15. ##############
  16. ###</Notes>###
  17. ##############
  18.  
  19.  
  20. #######################
  21. ##Start Configuration##
  22. #######################
  23. # system user to run as (can only use one)
  24. user=user
  25.  
  26. # config file(s) separate multiple with newlines
  27. config="/home/$user/.rtorrent.rc"
  28.  
  29. #set of options to run with each instance, separated by a new line
  30. options=""
  31. # Examples:
  32. # starts one instance, sourcing both .rtorrent.rc and .rtorrent2.rc
  33. # options="-o import=~/.rtorrent2.rc"
  34. # starts two instances, ignoring .rtorrent.rc for both, and using
  35. # .rtorrent2.rc for the first, and .rtorrent3.rc for the second
  36. # we do not check for valid options
  37. # options="-n -o import=~/.rtorrent2.rc
  38. # -n -o import=rtorrent3.rc"
  39.  
  40. # default directory for screen, needs to be an absolute path
  41. base=/home/${user}
  42.  
  43. # name of screen session, no whitespace allowed
  44. srnname=rtorrent
  45. #######################
  46. ###END CONFIGURATION###
  47. #######################
  48. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  49. DESC="rtorrent"
  50. NAME=rtorrent
  51. DAEMON=/usr/bin/$NAME
  52. SCRIPTNAME=/etc/init.d/$NAME
  53.  
  54. # Gracefully exit if the package has been removed.
  55. test -x $DAEMON || exit 0
  56.  
  57. checkcnfg() {
  58. OLDIFS="$IFS"
  59. IFS=$'\n'
  60. for i in $config ; do
  61. session=$(cat "$i" | grep "^[[:space:]]*session" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//")
  62. if ! [ -r $i ] ; then
  63. echo "cannot find readable config $i. check that it is there and permissions are appropriate">&2
  64. exit 3
  65. elif ! [ -r $session ] ; then
  66. echo "cannot find readable session directory $i. check permissions">&2
  67. exit 3
  68. fi
  69. done
  70. IFS="$OLDIFS"
  71. }
  72.  
  73. d_start() {
  74. [ -d "$base" ] && cd "$base"
  75. stty stop undef && stty start undef
  76. su -c "screen -ls | grep "\.${srnname}[[:space:]]" > /dev/null" $user || su -c "screen -dm -S $srnname" $user
  77. OLDIFS="$IFS"
  78. IFS=$'\n'
  79. if [ -z "$options" ] ; then
  80. sleep 3
  81. su -c "screen -S $srnname -X screen rtorrent" $user
  82. else
  83. for option in $options ; do
  84. sleep 3
  85. su -c "screen -S $srnname -X screen rtorrent $option" $user
  86. done
  87. fi
  88. IFS="$OLDIFS"
  89. }
  90.  
  91. d_stop() {
  92. OLDIFS="$IFS"
  93. IFS=$'\n'
  94. for i in $config ; do
  95. session=$(cat "$i" | grep "^[[:space:]]*session" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//")
  96. pid=$(cat ${session}/rtorrent.lock | sed "s/[^0-9]//g")
  97. # make sure the pid doesn't belong to another process
  98. # skip the pid otherwise
  99. if ps -A | grep ${pid}.*rtorrent > /dev/null ; then
  100. kill -s INT $pid
  101. fi
  102. done
  103. IFS="$OLDIFS"
  104. }
  105.  
  106. checkcnfg
  107.  
  108. case "$1" in
  109. start)
  110. echo -n "Starting $DESC: $NAME"
  111. d_start
  112. echo "."
  113. ;;
  114. stop)
  115. echo -n "Stopping $DESC: $NAME"
  116. d_stop
  117. echo "."
  118. ;;
  119. restart|force-reload)
  120. echo -n "Restarting $DESC: $NAME"
  121. d_stop
  122. sleep 1
  123. d_start
  124. echo "."
  125. ;;
  126. *)
  127. echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
  128. exit 1
  129. ;;
  130. esac
  131.  
  132. exit 0
Add Comment
Please, Sign In to add comment