LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Ubuntu (http://www.linuxquestions.org/questions/ubuntu-63/)
-   -   screen & rtorrent init.d start (http://www.linuxquestions.org/questions/ubuntu-63/screen-and-rtorrent-init-d-start-621832/)

imagineaxion 02-17-2008 03:58 PM

screen & rtorrent init.d start
 
Hi

I have recently setup rtorrent to run on my Ubuntu server.
I have set it up to automatically start on boot which is now working fine.
rtorrent runs in screen and this all boots up and starts 100% as I want it to.

The problem is when I for example make some changes to the config file and want to restart the service

When I run the command
Code:

/etc/init.d/rtorrentInit stop
It stops the screen and rtorrent which is great.

The problem is trying to restart it. If I run
Code:

/etc/init.d/rtorrentInit start
nothing happens

I then have to reboot the server for screen and rtorrent to start again as they do automatically on boot

Now I am sure you can see how this can be a problem as it is a server and I really do not need to be restarting my it every 5 minutes while making changes

Your help is much anticipated and will be much appreciated

Thanks
G

unSpawn 02-17-2008 08:17 PM

Quote:

Originally Posted by imagineaxion (Post 3060549)
If I run
Code:

/etc/init.d/rtorrentInit start
nothing happens

Maybe posting the contents of your rtorrentInit script could help people make sense of it?

Mephisto 02-17-2008 09:16 PM

also try and reattach the screen and see if there is any messages there:

Code:

screen -ls
to list all detached screens. Assuming there is only one
Code:

screen -r
to reattach it.

At a guess your stop script is stopping rtorrent but not terminating the screen. Then when you try to start it again it fails because there is already a screen of that name.

imagineaxion 02-18-2008 03:03 PM

Hi

Thanks for the reply.

Been at work and unable to reply

OK, so here is the script file from init.d

Code:

#!/bin/sh
#############
###<Notes>###
#############
# This script depends on screen.
# For the stop function to work, you must set an
# explicit session directory using ABSOLUTE paths (no, ~ is not absolute) in your rtorrent.rc.
# If you typically just start rtorrent with just "rtorrent" on the
# command line, all you need to change is the "user" option.
# Attach to the screen session as your user with
# "screen -dr rtorrent". Change "rtorrent" with srnname option.
# Licensed under the GPLv2 by lostnihilist: lostnihilist _at_ gmail _dot_ com
##############
###</Notes>###
##############

#######################
##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.
# user = user
# will not work
# system user to run as
user=torrnt

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

# the full path to the filename where you store your rtorrent configuration
config=/home/torrnt/.rtorrent.rc

# set of options to run with
options=""

# default directory for screen, needs to be an absolute path
base=/home/torrnt/

# name of screen session
srnname="srntorrent"

# file to log to (makes for easier debugging if something goes wrong)
logfile="/var/log/rtorrentInit.log"
#######################
###END CONFIGURATION###
#######################
PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin
DESC="rtorrent"
NAME=rtorrent
DAEMON=$NAME
SCRIPTNAME=/etc/init.d/$NAME

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
}

d_start() {
  [ -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
}

d_stop() {
    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
}

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

checkcnfg

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

exit 0

Hope you can see the problem from this

Thanks again!
G

imagineaxion 02-18-2008 05:39 PM

Mephisto

You could be right. If that is the case? How do I get the stop command to kill both.

I am not 100% sure if this is the case and I am unable to test it at this stage as I can't risk needed to reboot.
Should have a chance tomorrow.

In the mean time any suggestions on the script I posted and how I could get them both to stop if this is the case would be much appreciated

Thanks
G

Mephisto 02-18-2008 08:33 PM

Well based on the script it looks like it reuses the screen if it exists so you should be fine there. I would need to install rtorrent to try and decipher the problem, I will try tomorrow night on a VM. Sorry I can not be faster, but I did not bring my VMs with me tonight and do not want to install rtorrent on my main laptop.

Anyway a few notes:
- the init script does use a named screen (srntorrent) running as the user torrnt
- init logs are located at /var/log/rtorrentInit.log


First things first, when you start and stop the rtorrent do you use
Code:

/etc/init.d/rtorrentInit start|stop
or
Code:

sudo/etc/init.d/rtorrentInit start|stop
if you are not doing a sudo, are you sure rtorrent is being stopped? It is set to run as a specific user so I would not expect a different normal user to be able to stop it.

If you are using sudo try:
Code:

tail -n20 /var/log/rtorrentInit.log
and see if you spot any obvious errors.

you could try
Code:

screen -r srntorrent
and look around but based on the script I do not think you will see anything there. Never know though.

imagineaxion 02-19-2008 07:22 AM

Hi Mephisto

I had a look in the log files but there is nothing useful in there. In fact I think there is nothing in their since I first starting setting this up and had some permission problems accessing the .rtorrent.rc file.

I will get a snap of this later anyway to make sure.

Quote:

First things first, when you start and stop the rtorrent do you use

Code:
/etc/init.d/rtorrentInit start|stop

or

Code:
sudo/etc/init.d/rtorrentInit start|stop
I have been using sudo

I have also checked to make sure that they are no longer running using
ps -ef | grep torrent

This actually shows if either rtorrent or the screen (srntorrent) are running
I believe (will confirm tonight) both are killed with the command
Code:

sudo /etc/init.d/rtorrentInit stop

imagineaxion 02-19-2008 04:20 PM

Hi

Ahh OK...

When I use the command
Code:

sudo /etc/init.d/rtorrentInit stop
it does just stop rtorrent and using the command
Code:

sudo /etc/init.d/rtorrentInit start
does bring it back up

Think I may have been using the kill command to end screen as well thinking I needed to and expecting the init script to create a new screen session.

So why is it that I cannot start it if I kill of screen. Should it not just create a new one?

Thanks
G

onepostonly 04-19-2008 02:32 PM

Try

Code:

/etc/init.d/rtorrentInit stop
screen -wipe
/etc/init.d/rtorrentInit start

The "screen -wipe" part might be just what you need, it removes destroyed sessions


All times are GMT -5. The time now is 08:29 AM.