SHARE
TWEET

cyla

a guest Jul 5th, 2008 331 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. base=`echo $HOME`
  4. user=`whoami`
  5. config="${base}/.rtorrent.rc"
  6. gconfig="/etc/rtorrent/rtorrent.rc"
  7.  
  8. options="-o import=${gconfig}"
  9.  
  10. scrnname="torrent"
  11.  
  12. DESC="rtorrent"
  13. NAME=`which rtorrent`
  14. DAEMON="${NAME}"
  15.  
  16. getsession() {
  17.     session=`cat "$1" | grep "^[[:space:]]*session[[:space:]]*=" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//" `
  18.     session=`echo ${session} | sed 's|~|'$2'|g'`
  19.     echo $session
  20. }
  21.  
  22. getsocket() {
  23.     socket=`cat "$1" | grep "^[[:space:]]*scgi_local[[:space:]]*=" | sed "s/^[[:space:]]*scgi_local[[:space:]]*=[[:space:]]*//" `
  24.     socket=`echo ${socket} | sed 's|~|'$2'|g'`
  25.     echo $socket
  26. }
  27.  
  28. checkconfig() {
  29.     if ! [ -r "${config}" ] ; then
  30.         eerror "cannot find readable config ${config}. check that it is there and permissions are appropriate"
  31.         return 2
  32.     fi
  33.     if ! [ -r "${gconfig}" ] ; then
  34.         eerror "cannot find readable config ${gconfig}. check that it is there and permissions are appropriate"
  35.         return 3
  36.     fi
  37.     session=`getsession "$gconfig" "$base"`
  38.     if ! [ -d "${session}" ] ; then
  39.         eerror "cannot find readable session directory ${session} from config ${configg}. check permissions"
  40.         return 4
  41.     fi
  42. }
  43.  
  44. checkrunning() {
  45.         session=`getsession "$gconfig" "$base"`
  46.         if [ -s "${session}/rtorrent.lock" ] ; then
  47.                 pid=`cat ${session}/rtorrent.lock | awk -F: '{print($2)}' | sed "s/[^0-9]//g"`
  48.                 if ps -A | grep -sq "${pid}.*rtorrent" ; then
  49.                         echo "rTorrent's already running"
  50.                         return 1
  51.                 else
  52.                         echo "lock file has been found, but rTorrent's not running...deleting lock file"
  53.                 fi
  54.         fi
  55. }
  56.  
  57. d_start() {
  58.         checkconfig || return 1
  59.         checkrunning || return 1
  60.  
  61.         socket=`getsocket "$gconfig" "$base"`
  62.         if [ -S ${socket} ] ; then
  63.                 echo "SCGI socket has been found, but rTorrent's not running...deleting socket file"
  64.                 rm ${socket}
  65.         fi
  66.  
  67.         screen -ls | grep -sq "\.${scrnname}[[:space:]]" || screen -dm -S ${scrnname} 2>&1 1>/dev/null
  68.         screen -S "${scrnname}" -X screen ${DAEMON} ${options} 2>&1 1>/dev/null
  69. }
  70.  
  71. d_stop() {
  72.         session=`getsession "$gconfig" "$base"`
  73.         if ! [ -s ${session}/rtorrent.lock ] ; then
  74.                 return
  75.         fi
  76.         pid=`cat ${session}/rtorrent.lock | awk -F: '{print($2)}' | sed "s/[^0-9]//g"`
  77.         if ps -A | grep -sq ${pid}.*rtorrent ; then # make sure the pid doesn't belong to another process
  78.                 kill -s INT ${pid}
  79.         fi
  80. }
  81.  
  82. case "$1" in
  83.         start)
  84.                 echo -n "Starting $DESC for user: $user"
  85.                 d_start
  86.                 echo "."
  87.         ;;
  88.         stop)
  89.                 echo -n "Stopping $DESC for user: $user"
  90.                 d_stop
  91.                 echo "."
  92.         ;;
  93.         restart|force-reload)
  94.                 echo -n "Restarting $DESC for user: $user"
  95.                 d_stop
  96.                 for (( i = 0 ; i <= 10; i++ )) ; do
  97.                         sleep 1
  98.                         echo -n "."
  99.                 done
  100.                 d_start
  101.                 echo "."
  102.         ;;
  103.         *)
  104.                 exit 1
  105.         ;;
  106. esac
  107.  
  108. exit 0
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top