Verybad

Автостарт rtorrent в Arch Linux

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