Advertisement
Guest User

Init for rtorrent

a guest
Mar 8th, 2012
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.15 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ### BEGIN INIT INFO
  4. # Provides:          rtorrent
  5. # Required-Start:    $remote_fs $syslog
  6. # Required-Stop:     $remote_fs $syslog
  7. # Should-Start:      $portmap
  8. # Should-Stop:       $portmap
  9. # X-Start-Before:    nis
  10. # X-Stop-After:      nis
  11. # Default-Start:     2 3 4 5
  12. # Default-Stop:      0 1 6
  13. # X-Interactive:     false
  14. # Short-Description: Initscript for rtorrent
  15. # Description:       This file should be used to start|stop rtorrent
  16. ### END INIT INFO
  17.  
  18.  
  19. ##Start Configuration##
  20. PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin
  21. DESC="rtorrent"
  22. NAME="rtorrent"
  23. DAEMON=$NAME
  24. SCRIPTNAME=$0
  25.  
  26. # file to log to (makes for easier debugging if something goes wrong)
  27. logfile="/var/log/rtorrentInit.log"
  28.  
  29. # Users list
  30. users="alex liza"
  31.  
  32. ###END CONFIGURATION###
  33.  
  34.  
  35. checkcnfg() {
  36.     exists=0
  37.     for i in `echo "$PATH" | tr ':' '\n'` ; do
  38.         if [ -f $i/$NAME ] ; then
  39.             exists=1
  40.             break
  41.         fi
  42.     done
  43.     if [ $exists -eq 0 ] ; then
  44.         echo "cannot find rtorrent binary in PATH $PATH" | tee -a "$logfile" >&2
  45.         exit 3
  46.     fi
  47.     if ! [ -r "${config}" ] ; then
  48.         echo "cannot find readable config ${config}. check that it is there and permissions are appropriate" | tee -a "$logfile" >&2
  49.         exit 3
  50.     fi
  51.     session=`getsession "$config"`
  52.     if ! [ -d "${session}" ] ; then
  53.         echo "cannot find readable session directory ${session} from config ${config}. check permissions" | tee -a "$logfile" >&2
  54.         exit 3
  55.     fi
  56. }
  57.  
  58.  
  59. d_start() {
  60.     for user in $users; do
  61.         # the full path to the filename where you store your rtorrent configuration
  62.         config="`su -c 'echo $HOME' $user`/.rtorrent.rc"
  63.         # set of options to run with
  64.         options=""
  65.         # default directory for screen, needs to be an absolute path
  66.         base="`su -c 'echo $HOME' $user`"
  67.         # name of screen session
  68.         srnname="rtorrent"
  69.  
  70.         echo "Start for $user; Config='$config'; Base='$base'" | tee -a "$logfile"
  71.  
  72.         checkcnfg
  73.  
  74.         [ -d "${base}" ] && cd "${base}"
  75.         stty stop undef && stty start undef
  76.         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
  77.         # this works for the screen command, but starting rtorrent below adopts screen session gid
  78.         # even if it is not the screen session we started (e.g. running under an undesirable gid
  79.         #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
  80.         su -c "screen -S "${srnname}" -X screen rtorrent ${options} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2
  81.     done
  82. }
  83.  
  84.  
  85. d_stop() {
  86.     for user in $users; do
  87.         # the full path to the filename where you store your rtorrent configuration
  88.         config="`su -c 'echo $HOME' $user`/.rtorrent.rc"
  89.         # set of options to run with
  90.         options=""
  91.         # default directory for screen, needs to be an absolute path
  92.         base="`su -c 'echo $HOME' $user`"
  93.         # name of screen session
  94.         srnname="rtorrent"
  95.  
  96.         checkcnfg
  97.  
  98.         session=`getsession "$config"`
  99.         if ! [ -s ${session}/rtorrent.lock ] ; then
  100.             return
  101.         fi
  102.         pid=`cat ${session}/rtorrent.lock | awk -F: '{print($2)}' | sed "s/[^0-9]//g"`
  103.         if ps -A | grep -sq ${pid}.*rtorrent ; then # make sure the pid doesn't belong to another process
  104.             kill -s INT ${pid}
  105.         fi
  106.     done
  107. }
  108.  
  109.  
  110. getsession() {
  111.     session=`cat "$1" | grep "^[[:space:]]*session[[:space:]]*=" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//" `
  112.     echo $session
  113. }
  114.  
  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
Advertisement