Debian Lenny Init script

60 views
Skip to first unread message

David Post

unread,
Jan 8, 2010, 1:04:19 AM1/8/10
to rubyrep
I have written an init script that I have tested on Debian Lenny. It
is based loosely off of Arndts script from an earlier post but with a
config file for proxy/replication and includes status and sync.

It logs to /var/log/rubyrep/<whatModeYouAreRunning>.log
configs are expected in /etc/rubyrep/rubyrep.conf

Here are the files

/etc/init.d/rubyrep

#!/bin/bash
#
# /etc/init.d/rubyrep
#
# Written by David Post
# Functions provided by this file
# * Start
# * Stop
# * Restart
# * Sync
# * Status
# Configuration for use with this script should be kept in /etc/
rubyrep/rubyrep.conf
# The only configuration that I am writing right now is proxy, conf
and rubyrep.
if [ -f /etc/rubyrep/rubyrep.conf ]
then
. /etc/rubyrep/rubyrep.conf
fi
if [ ! -n "$CONFIG" ]
then
echo "No config file specified in rubyrep.conf"
exit 1
fi
[ -r /etc/rubyrep/"$CONFIG" ] || { echo "File not found: $CONFIG";
exit 1; }
CONFIG=/etc/rubyrep/"$CONFIG"
# These are some functions to make this check if RubyRep is running
before starting it.
# This checks if it is running or not
# RUN = is it running proxy or replicate, this may be different than
the configuration file says. e.g.
# The just changed the config file
function test_running(){
PID=""
if (("$PROXY"))
then
PID=`ps -Af | grep rubyrep| grep proxy | grep java |gawk '{print
$2}'`
if [ -n "$PID" ]
then
RUN=P
else
PID=`ps -Af | grep rubyrep| grep replicate | grep java |gawk
'{print $2}'`
[ ! -n "$PID" ] || RUN=R
fi
else
PID=`ps -Af | grep rubyrep| grep replicate | grep java |gawk '{print
$2}'`
if [ -n "$PID" ]
then
RUN=R
else
PID=`ps -Af | grep rubyrep| grep proxy | grep java |gawk '{print
$2}'`
[ ! -n "$PID" ] || RUN=P
fi
fi
}

case "$1" in
start)
test_running
if [ -n "$PID" ]
then
echo "RubyRep already running."
exit 0
fi
if (("$PROXY"))
then
echo "Starting RubyRep proxy"
$RUBYREP proxy 2>&1 | gawk '{print strftime("%Y-%m-% %T",systime
()), $0; fflush();}' >> /var/log/rubyrep/proxy.log &
else
echo "Starting rubyrep replication"
$RUBYREP replicate -c $CONFIG 2>&1 | gawk '{print strftime("%Y-%m-
%d %T",systime()), $0; fflush();}' >> /var/log/rubyrep/replication.log
&
fi
;;
stop)
test_running
case "$RUN" in
P)
echo "Stopping RubyRep proxy"
kill `ps -Af | grep rubyrep | grep proxy | grep java | gawk
'{print $2}'` > /dev/null
;;
R)
echo "Stopping RubyRep replication"
kill `ps -Af | grep rubyrep | grep replicate | grep java | gawk
'{print $2}'` > /dev/null
;;
*)
echo "RubyRep not running"
;;
esac
;;
restart)
test_running
if [ -n "$PID" ]
then
$0 stop
sleep 1
fi
$0 start
;;
sync)
echo "Syncing databases..."
/opt/rubyrep-1.1.0/rubyrep "sync -c $CONFIG"
;;
status)
if (("$PROXY"))
then
echo -e "RubyRep Proxy is \\c"
ps -Af | grep rubyrep| grep proxy | grep java >/dev/null || echo -e
not \\c
else
echo -e "RubyRep Replication is \\c"
ps -Af | grep rubyrep| grep replicate | grep java >/dev/null ||
echo -e not \\c
fi
echo running
;;
test) # This ones sort of a secret that I used for testing if the
function test_running() was working or not
test_running
if [ -n "$PID" ]
then
echo "RubyRep is running on PID $PID"
else
echo "RubyRep is not running"
fi
;;

*)
echo "Usage: $0 {start|stop|restart|sync|status}"
;;
esac
exit 0

/etc/rubyrep/rubyrep.conf

PROXY=0
CONFIG="sepia.conf"
RUBYREP="/opt/rubyrep-1.1.0/rubyrep"


CONFIG is assumed to be in /etc/rubyrep and has this prepended to it
in the init script.

Let me know what you think.

Reply all
Reply to author
Forward
0 new messages