Відмінності між версіями «Служба rtorrent»

Матеріал з Wiki Welard
Перейти до навігації Перейти до пошуку
(Created page with 'Строримо такий файлик з правами на виконання <pre> #!/bin/bash # # chkconfig: - 95 05 # description: rtorrent daemon # process name: fail2ba...')
 
Рядок 1: Рядок 1:
−
Строримо такий файлик з правами на виконання
+
Строримо такий файлик з правами на виконання  
−
<pre>
+
<pre>#!/bin/bash
−
#!/bin/bash
 
 
#
 
#
 
# chkconfig: - 95 05
 
# chkconfig: - 95 05
 
# description: rtorrent daemon
 
# description: rtorrent daemon
−
# process name: fail2ban-server
+
# process name: rtorrent
 
#
 
#
 
#
 
#
Рядок 56: Рядок 55:
 
checkcnfg() {
 
checkcnfg() {
 
     exists=0
 
     exists=0
−
     for i in `echo "$PATH" | tr ':' '\n'` ; do
+
     for i in `echo "$PATH" | tr ':' '\n'`&nbsp;; do
−
         if [ -f $i/$NAME ] ; then
+
         if [ -f $i/$NAME ]&nbsp;; then
 
             exists=1
 
             exists=1
 
             break
 
             break
 
         fi
 
         fi
 
     done
 
     done
−
     if [ $exists -eq 0 ] ; then
+
     if [ $exists -eq 0 ]&nbsp;; then
−
         echo "cannot find rtorrent binary in PATH $PATH" | tee -a "$logfile" >&2
+
         echo "cannot find rtorrent binary in PATH $PATH" | tee -a "$logfile" &gt;&amp;2
 
         exit 3
 
         exit 3
 
     fi
 
     fi
−
     if ! [ -r "${config}" ] ; then  
+
     if&nbsp;! [ -r "${config}" ]&nbsp;; then  
−
         echo "cannot find readable config ${config}. check that it is there and permissions are appropriate" | tee -a "$logfile" >&2
+
         echo "cannot find readable config ${config}. check that it is there and permissions are appropriate" | tee -a "$logfile" &gt;&amp;2
 
         exit 3  
 
         exit 3  
 
     fi  
 
     fi  
 
     session=`getsession "$config"`  
 
     session=`getsession "$config"`  
−
     if ! [ -d "${session}" ] ; then
+
     if&nbsp;! [ -d "${session}" ]&nbsp;; then
−
         echo "cannot find readable session directory ${session} from config ${config}. check permissions" | tee -a "$logfile" >&2
+
         echo "cannot find readable session directory ${session} from config ${config}. check permissions" | tee -a "$logfile" &gt;&amp;2
 
         exit 3
 
         exit 3
 
     fi
 
     fi
Рядок 79: Рядок 78:
 
start() {
 
start() {
 
     echo -n "Starting $DESC: $NAME"
 
     echo -n "Starting $DESC: $NAME"
−
     [ -d "${base}" ] && cd "${base}"
+
     [ -d "${base}" ] &amp;&amp; cd "${base}"
−
     stty stop undef && stty start undef
+
     stty stop undef &amp;&amp; stty start undef
−
     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
+
     su -c "screen -ls | grep -sq "\.${srnname}[[:space:]]" " ${user} || su -c "screen -dm -S ${srnname} 2&gt;&amp;1 1&gt;/dev/null" ${user} | tee -a "$logfile" &gt;&amp;2
 
     # this works for the screen command, but starting rtorrent below adopts screen session gid
 
     # this works for the screen command, but starting rtorrent below adopts screen session gid
 
     # even if it is not the screen session we started (e.g. running under an undesirable gid
 
     # even if it is not the screen session we started (e.g. running under an undesirable gid
−
     #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
+
     #su -c "screen -ls | grep -sq "\.${srnname}[[:space:]]" " ${user} || su -c "sg \"$group\" -c \"screen -fn -dm -S ${srnname} 2&gt;&amp;1 1&gt;/dev/null\"" ${user} | tee -a "$logfile" &gt;&amp;2
−
     su -c "screen -S "${srnname}" -X screen rtorrent ${options} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2
+
     su -c "screen -S "${srnname}" -X screen rtorrent ${options} 2&gt;&amp;1 1&gt;/dev/null" ${user} | tee -a "$logfile" &gt;&amp;2
 
     echo_success
 
     echo_success
 
     echo
 
     echo
Рядок 94: Рядок 93:
 
     echo -n "Stopping $DESC: $NAME"
 
     echo -n "Stopping $DESC: $NAME"
 
     session=`getsession "$config"`
 
     session=`getsession "$config"`
−
     if ! [ -s ${session}/rtorrent.lock ] ; then
+
     if&nbsp;! [ -s ${session}/rtorrent.lock ]&nbsp;; then
 
         return
 
         return
 
     fi
 
     fi
 
     pid=`cat ${session}/rtorrent.lock | awk -F: '{print($2)}' | sed "s/[^0-9]//g"`
 
     pid=`cat ${session}/rtorrent.lock | awk -F: '{print($2)}' | sed "s/[^0-9]//g"`
−
     if ps -A | grep -sq ${pid}.*rtorrent ; then # make sure the pid doesn't belong to another process
+
     if ps -A | grep -sq ${pid}.*rtorrent&nbsp;; then # make sure the pid doesn't belong to another process
 
         kill -s INT ${pid}
 
         kill -s INT ${pid}
 
     fi
 
     fi
Рядок 116: Рядок 115:
 
   start)
 
   start)
 
     start
 
     start
−
    ;;
+
  &nbsp;;;
 
   stop)
 
   stop)
 
     stop
 
     stop
−
    ;;
+
  &nbsp;;;
 
   restart|force-reload)
 
   restart|force-reload)
 
     echo -n "Restarting $DESC: $NAME"
 
     echo -n "Restarting $DESC: $NAME"
Рядок 126: Рядок 125:
 
     sleep 1
 
     sleep 1
 
     start
 
     start
−
    ;;
+
  &nbsp;;;
 
   *)
 
   *)
−
     echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
+
     echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" &gt;&amp;2
 
     exit 1
 
     exit 1
−
    ;;
+
  &nbsp;;;
 
esac
 
esac
 
exit $RETVAL
 
exit $RETVAL
−
</pre>
+
</pre>  
 
Ну і запускаємо  
 
Ну і запускаємо  
 +
 
  chkconfig rtorrent on
 
  chkconfig rtorrent on

Версія за 13:28, 21 травня 2009

Строримо такий файлик з правами на виконання

#!/bin/bash
#
# chkconfig: - 95 05
# description: rtorrent daemon
# process name: rtorrent
#
#
# Author: Andrew G 
# Licensed under the GPLv2

#######################
##Start Configuration##
#######################
# You can specify your configuration in a different file 
# (so that it is saved with upgrades, saved in your home directory,
# or whateve reason you want to)
# by commenting out/deleting the configuration lines and placing them
# in a text file (say /home/user/.rtorrent.init.conf) exactly as you would
# have written them here (you can leave the comments if you desire
# and then uncommenting the following line correcting the path/filename 
# for the one you used. note the space after the ".".
# . /etc/rtorrent.init.conf

#Do not put a space on either side of the equal signs e.g.
# system user to run as
user="'''andrew'''"

# the system group to run as, not implemented, see start for beginning implementation
# group=`id -ng "$user"`

# the full path to the filename where you store your rtorrent configuration
config="`su -c 'echo $HOME' $user`/.rtorrent.rc"

# set of options to run with
options=""

# default directory for screen, needs to be an absolute path
base="`su -c 'echo $HOME' $user`"

# name of screen session
srnname="rtorrent"

# file to log to (makes for easier debugging if something goes wrong)
logfile="/var/log/rtorrentInit.log"

PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin
DESC="rtorrent"
NAME=rtorrent
DAEMON=$NAME
SCRIPTNAME=/etc/init.d/$NAME

. /etc/init.d/functions
RETVAL=0
checkcnfg() {
    exists=0
    for i in `echo "$PATH" | tr ':' '\n'` ; do
        if [ -f $i/$NAME ] ; then
            exists=1
            break
        fi
    done
    if [ $exists -eq 0 ] ; then
        echo "cannot find rtorrent binary in PATH $PATH" | tee -a "$logfile" >&2
        exit 3
    fi
    if ! [ -r "${config}" ] ; then 
        echo "cannot find readable config ${config}. check that it is there and permissions are appropriate" | tee -a "$logfile" >&2
        exit 3 
    fi 
    session=`getsession "$config"` 
    if ! [ -d "${session}" ] ; then
        echo "cannot find readable session directory ${session} from config ${config}. check permissions" | tee -a "$logfile" >&2
        exit 3
    fi
}

start() {
    echo -n "Starting $DESC: $NAME"
    [ -d "${base}" ] && cd "${base}"
    stty stop undef && stty start undef
    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
    # this works for the screen command, but starting rtorrent below adopts screen session gid
    # even if it is not the screen session we started (e.g. running under an undesirable gid
    #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
    su -c "screen -S "${srnname}" -X screen rtorrent ${options} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2
    echo_success
    echo
    RETVAL=0
}

stop() {
    echo -n "Stopping $DESC: $NAME"
    session=`getsession "$config"`
    if ! [ -s ${session}/rtorrent.lock ] ; then
        return
    fi
    pid=`cat ${session}/rtorrent.lock | awk -F: '{print($2)}' | sed "s/[^0-9]//g"`
    if ps -A | grep -sq ${pid}.*rtorrent ; then # make sure the pid doesn't belong to another process
        kill -s INT ${pid}
    fi
    echo_success
    echo
    RETVAL=0
}

getsession() { 
    session=`cat "$1" | grep "^[[:space:]]*session[[:space:]]*=" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//" `
    echo $session
}

checkcnfg

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: $NAME"
    echo 
    stop
    sleep 1
    start
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac
exit $RETVAL

Ну і запускаємо

chkconfig rtorrent on