|> Date: Thu, 4 Sep 2008 13:06:54 +0200 (CEST)
|> From: Sa?a Stupar <[EMAIL PROTECTED]>
|> Subject: Automatically starting user programs on boot
|> To: freebsd-questions@freebsd.org
|> Message-ID:
|>        <[EMAIL PROTECTED]>
|> Content-Type: text/plain;charset=iso-8859-2
|>
|> Hi!
|>
|> On my FBSD 7 server I also use screen with rtorrent. In case of server
|> reboot (power outage, server goes on UPS then power down) the user need
|> manually to login, then start screen and then rtorrent. Is it possible to
|> start this procedure automatically and of course as certain user and not
|> as root?
|> I was thinking about .startup file or something in users home directory.
|>
|> Regards,
|> Sasa

Hi,

I have been using rtorrent and screen for a while now, it is a great
setup. Anyway, I found an rc.d file for debian that I have changed to
make work for me on freebsd 7. I doubt it is as good as it could be,
but hey, it works fine for me :)

You will have to change some of the variables at the top of the file.
It opens screen on boot, names it rtorrent and starts rtorrent, does
its thing in the background. To connect to it type screen -r rtorrent

I placed it in /usr/local/etc/rc.d since it isn't part of the base
distribution. I also named it with a .sh suffix, I forget why but it
is something to do with a controlling terminal, I think. I also have a
nice .screenrc file if you would like since I know how much of a pain
it is to figure out!

$ cat /usr/local/etc/rc.d
#!/bin/sh

# PROVIDE: rtorrent
# REQUIRE: DAEMON

#############
###<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

# system user to run as
user="davidcollins"

# 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="`su $user -c 'echo $HOME'`/.rtorrent.rc"

# set of options to run with
options=""

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

# name of screen session
srnname="rtorrent"

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

. /etc/rc.subr

PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin
name=rtorrent

rcvar=`set_rcvar`
eval "${rcvar}=\${${rcvar}:-'NO'}"

start_precmd=checkcnfg
start_cmd="${name}_start"
stop_cmd="${name}_stop"

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
        err 3 "cannot find rtorrent binary in PATH $PATH"
    fi
    if ! [ -r "${config}" ] ; then
        err 3 "cannot find readable config ${config}. check that it is
there and permissions are appropriate"
    fi
    session=`getsession "$config"`
    if ! [ -d "${session}" ] ; then
        err 3 "cannot find readable session directory ${session} from
config ${config}. check permissions"
    fi
}

rtorrent_start()
{
        echo "Starting $name."
        [ -d "${base}" ] && cd "${base}"
        stty stop undef && stty start undef
        ## start screen with rtorrent inside
        su ${user} -c "screen -dm -S "${srnname}" ${name} ${options} 2>&1
1>/dev/null" | tee -a "$logfile" >&2
}

rtorrent_stop()
{
    echo "Stopping $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"`
        ## make sure the pid doesn't belong to another process
    if ps -A | grep -sq ${pid}.*rtorrent ; then
        kill -s INT ${pid}
    fi
}

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

load_rc_config $name
run_rc_command "$1"
_______________________________________________
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"

Reply via email to