Advertisement
simon24

µTorrent Service Script Debian

Feb 1st, 2017
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.45 KB | None | 0 0
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides:          rtorrent_autostart
  4. # Required-Start:    $local_fs $remote_fs $network $syslog $netdaemons
  5. # Required-Stop:     $local_fs $remote_fs
  6. # Default-Start:     2 3 4 5
  7. # Default-Stop:      0 1 6
  8. # Short-Description: rtorrent script using screen(1)
  9. # Description:       rtorrent script using screen(1) to keep torrents working without the user logging in
  10. ### END INIT INFO
  11. #
  12. #
  13. # Original source: http://forum.utorrent.com/viewtopic.php?id=88044
  14. #
  15. # uTorrent start stop service script
  16. #
  17. # copy to /etc/init.d
  18. # run "update-rc.d utorrent defaults" to install
  19. # run "update-rc.d utorrent remove" to remove
  20. #
  21. #
  22. # version 2 improvments by:
  23. # @author FanFan Huang (kaboom05+utorrentscript@gmail.com)
  24. #
  25. #
  26. UTORRENT_PATH=/opt/utorrent-server-alpha-v3_3/      #where you extracted your utserver executable
  27. LOGFILE=/opt/utorrent-server-alpha-v3_3/utorrent.log    #must be a writable directory
  28. USER=bob                        #any user account you can create the utorrent user if you like
  29. GROUP=users
  30. NICE=15
  31. SCRIPTNAME=/etc/init.d/utorrent #must match this file name
  32.  
  33. DESC="uTorrent Server for Linux"
  34. CHDIR=$UTORRENT_PATH
  35. NAME=utserver
  36. UT_SETTINGS=$UTORRENT_PATH
  37. UT_LOG=$LOGFILE
  38.  
  39. DAEMON_ARGS="-settingspath ${UT_SETTINGS} -logfile ${UT_LOG}"
  40. DAEMON=$CHDIR/$NAME
  41. PIDFILE=/var/run/utorrent.pid
  42. STOP_TIMEOUT=5
  43. INIT_VERBOSE=yes
  44.  
  45. FAILURE=false
  46.  
  47. # Exit if the package is not installed
  48. [ -x "$DAEMON" ] || exit 0
  49.  
  50. # Read configuration variable file if it is present
  51. [ -r /etc/default/$NAME ] && . /etc/default/$NAME
  52.  
  53. # Load the VERBOSE setting and other rcS variables
  54. . /lib/init/vars.sh
  55.  
  56. # Define LSB log_* functions.
  57. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
  58. . /lib/lsb/init-functions
  59.  
  60. #
  61. # Function that starts the daemon/service
  62. #
  63. do_start()
  64. {
  65. FAILURE=false
  66. # Return
  67. # 0 if daemon has been started
  68. # 1 if daemon was already running
  69. # 2 if daemon could not be started
  70. # 3 if port bind failed
  71. start-stop-daemon --start --nicelevel $NICE --quiet --make-pidfile --pidfile $PIDFILE --chuid $USER:$GROUP --chdir $CHDIR --background --exec $DAEMON --test > /dev/null
  72. if [ "$?" = "1" ]; then
  73. return 1
  74. fi
  75. start-stop-daemon --start --nicelevel $NICE --quiet --make-pidfile --pidfile $PIDFILE --chuid $USER:$GROUP --chdir $CHDIR --background --exec $DAEMON -- $DAEMON_ARGS
  76. if [ "$?" != "0" ]; then
  77. return 2
  78. fi
  79. #bind validation
  80. while [ ! -e $LOGFILE ]; do
  81. sleep 1 #Wait for file to be generated
  82. done
  83.  
  84. ######################## DISABLED ENABLE THIS SECTION IF YOU HAVE IPv6 HANGS WITH NO IPv6 Support ################
  85. # while [ ! -n "$(cat $LOGFILE|grep 'IPv6 is installed')" ]; do
  86. # #wait until utorrent has finished bootup (IPv6 MESSAGE is the last message)#
  87. # sleep 1
  88. # done
  89.  
  90. RESULT=$(cat $LOGFILE|grep 'bind failed')
  91. if [ -n "$RESULT" ]; then
  92. return 3
  93. fi
  94. return 0
  95. }
  96.  
  97. #
  98. # Function that stops the daemon/service
  99. #
  100. do_stop()
  101. {
  102. # Return
  103. # 0 if daemon has been stopped
  104. # 1 if daemon was already stopped
  105. # 2 if daemon could not be stopped
  106. # other if a failure occurred
  107. start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
  108. RETVAL="$?"
  109. if [ "$RETVAL" = 2 ]; then
  110. return 2
  111. fi
  112. start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
  113. RETVAL="$?"
  114. if [ "$RETVAL" = 2 ]; then
  115. return 2
  116. fi
  117. #block process until server is completed shutting down fully
  118. while [ -n "$(pidof "$NAME")" ]; do
  119. sleep 1
  120. done
  121. # Many daemons don't delete their pidfiles when they exit.
  122. rm -f $PIDFILE
  123. rm -f $LOGFILE #we don't want to keep our logfile
  124. return "$RETVAL"
  125. }
  126.  
  127. #
  128. # Function that sends a SIGHUP to the daemon/service
  129. #
  130. do_reload() {
  131. #
  132. # If the daemon can reload its configuration without
  133. # restarting (for example, when it is sent a SIGHUP),
  134. # then implement that here.
  135. #
  136. start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
  137. return 0
  138. }
  139.  
  140. msg_start() {
  141. case "$1" in
  142. 0|1)
  143. if [ "$VERBOSE" != no ]; then
  144. log_end_msg 0
  145. fi
  146. ;;
  147. 2)
  148. if [ "$VERBOSE" != no ]; then
  149. log_end_msg 1
  150. fi
  151. ;;
  152. 3)
  153. if [ "$VERBOSE" != no ]; then
  154. log_daemon_msg "Port bind failure detected uTorrent may have limited functionality please change the bind port and restart uTorrent"
  155. log_end_msg 1
  156. fi
  157. ;;
  158. esac
  159. }
  160.  
  161. msg_stop() {
  162. case "$1" in
  163. 0|1)
  164. if [ "$VERBOSE" != no ]; then
  165. log_end_msg 0
  166. fi
  167. ;;
  168. *)
  169. if [ "$VERBOSE" != no ]; then
  170. log_daemon_msg "Failed to stop service exit status $STATUS"
  171. log_end_msg 1
  172. fi
  173. esac
  174. }
  175.  
  176. case "$1" in
  177. start)
  178. if [ "$VERBOSE" != no ]; then
  179. log_daemon_msg "Starting $DESC"
  180. fi
  181. do_start
  182. msg_start "$?"
  183. ;;
  184. stop)
  185. if [ "$VERBOSE" != no ]; then
  186. log_daemon_msg "Stopping $DESC"
  187. fi
  188. do_stop
  189. msg_stop "$?"
  190. ;;
  191. status)
  192. if [ -e "$PIDFILE" ]; then
  193. PID=" PID:($(cat $PIDFILE))"
  194. else
  195. PID=""
  196. fi
  197. status_of_proc "$DAEMON" "uTorrent$PID"
  198. if [ "$?" != "0" ]; then
  199. exit $?
  200. fi
  201. ;;
  202. restart|force-reload)
  203. #
  204. # If the "reload" option is implemented then remove the
  205. # 'force-reload' alias
  206. #
  207. log_daemon_msg "Restarting $DESC"
  208. do_stop
  209. STATUS="$?"
  210. if [ "$STATUS" -ne 0 ] && [ "$STATUS" -ne 1 ]; then
  211. log_daemon_msg "Could not stop exit status $STATUS"
  212. log_end_msg 1
  213. exit 1
  214. fi
  215. do_start
  216. STATUS="$?"
  217. case "$STATUS" in
  218. 0)
  219. log_end_msg 0
  220. ;;
  221. *)
  222. log_daemon_msg "Restart failed start exist status $STATUS"
  223. log_end_msg 1
  224. esac
  225. ;;
  226. log)
  227. if [ -e "$LOGFILE" ]; then
  228. LOG=$(cat $LOGFILE)
  229. echo "$LOG"
  230. else
  231. echo "uTorrent is not running no active log file"
  232. fi
  233. ;;
  234.  
  235. *)
  236. echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload|log}" >&2
  237. exit 3
  238. ;;
  239. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement