Sunday, December 12, 2004

Daemon scripts

Daemon scripts: Files in /etc/rc.d/init.d (tomcatd, postgresql, httpd)

tomcatd
#You can add this line for status in tomcatd. It outputs all tomcat processes
ps ax --width=1000 grep "[o]rg.apache.catalina.startup.Bootstrap start" awk '{printf $1 " "}'


postgresql
Here's a brief explanation of unusual features in my daemon scripts
You can make these changes in a daemon script to allow it to connect with jdbc
Edit /etc/rc.d/init.d/postgresql. JDBC needs to connect via tcp/ip, so
add "-i" next to "-p ${PGPORT}" so that the line contains this:
'-i -p ${PGPORT}'


/etc/rc.d/init.d/tomcatd

#!/bin/bash
#
# Startup script for Tomcat
#
# chkconfig: 345 84 16
# description: Tomcat jakarta JSP server
TOMCAT_HOME=/usr/local/tomcat
TOMCAT_START=$TOMCAT_HOME/bin/startup.sh
TOMCAT_STOP=$TOMCAT_HOME/bin/shutdown.sh
#Necessary environment variables
export JAVA_HOME="/usr/local/java/java"
export CATALINA_HOME="/usr/local/tomcat"
#export LD_KERNEL_ASSUME="2.2.5"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
#Check for tomcat script
if [ ! -f $TOMCAT_HOME/bin/catalina.sh ]
then
echo "Tomcat not available..."
exit
fi
start() {
echo -n "Starting Tomcat: "
su - tomcat -c $TOMCAT_START
echo
touch /var/lock/subsys/tomcatd
# We may need to sleep here so it will be up for apache
sleep 3
#Instead should check to see if apache is up by looking for httpd.pid
}
stop() {
echo -n $"Shutting down Tomcat: "
su - tomcat -c $TOMCAT_STOP
rm -f /var/lock/subsys/tomcatd
echo
}
status() {
ps ax --width=1000 grep "[o]rg.apache.catalina.startup.Bootstrap start" awk '{printf $1 " "}' wc awk '{print $2}' > /tmp/tomcat_process_count.txt
read line < /tmp/tomcat_process_count.txt
if [ $line -gt 0 ]; then
echo -n "tomcatd ( pid "
ps ax --width=1000 grep "[o]rg.apache.catalina.startup.Bootstrap start" awk '{printf $1 " "}'
echo -n ") is running..."
echo
else
echo "Tomcat is stopped"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 3
start
;;
status)
status
;;
*)
echo "Usage: tomcatd {startstoprestartstatus}"
exit 1
esac

/etc/rc.d/init.d/postgresqld

#! /bin/sh
# postgresqld This is the init script for starting up the PostgreSQL
# server
#
# chkconfig: 345 83 17
# description: Starts and stops the PostgreSQL backend daemon that handles # all database requests.
# processname: postmaster
# pidfile: /var/run/postmaster.pid
PG_HOME="/usr/local/pgsql"
# Source function library.
INITD=/etc/rc.d/init.d
. $INITD/functions
# Get function listing for cross-distribution logic.
TYPESET=`typeset -F`
# Get config.
. /etc/sysconfig/network
# Find the name of the script
#NAME=`basename $0`
NAME="PostgreSQLd"
# Set defaults for port and database directory
PGPORT=5432
export PGDATA=/usr/local/pgsql/data
# Check that networking is up.
# Pretty much need it for postmaster.
[ "${NETWORKING}" = "no" ] && exit 0
[ -f /$PG_HOME/bin/postmaster ] exit 0
start(){
PSQL_START=$"Starting ${NAME} service: "
echo -n $"Initializing database: "
if [ ! -d $PGDATA ]
then
mkdir -p $PGDATA
chown postgres.postgres $PGDATA
chmod go-rwx $PGDATA
fi
# Make sure the locale from the initdb is preserved for later startups...
[ -f $PG_HOME/i18n ] && cp $PG_HOME/i18n $PGDATA/../initdb.i18n
# Just in case no locale was set, use en_US
[ ! -f $PG_HOME/i18n ] && echo "LANG=en_US" > $PGDATA/../initdb.i18n
# Is expanded this early to be used in the command su runs
echo "export LANG LC_ALL LC_CTYPE LC_COLLATE LC_NUMERIC LC_CTYPE LC_TIME" >> $PGDATA/../initdb.i18n
# Initialize the database
su -l postgres -s /bin/sh -c "$PG_HOME/bin/initdb --pgdata=$PGDATA > /dev/null 2>&1" < /dev/null
[ -f $PGDATA/PG_VERSION ] && echo_success
[ ! -f $PGDATA/PG_VERSION ] && echo_failure
echo
# Check for postmaster already running...
# note that pg_ctl only looks at the data structures in PGDATA
# you really do need the pidof()
pid=`pidof -s $PG_HOME/bin/postmaster`
if [ $pid ] && $PG_HOME/pg_ctl status -D $PGDATA > /dev/null 2>&1
then
echo $"Postmaster already running."
else
#all systems go -- remove any stale lock files
rm -f /tmp/.s.PGSQL.${PGPORT} > /dev/null
echo -n "$PSQL_START"
su -l postgres -s /bin/sh -c "$PG_HOME/bin/pg_ctl -D $PGDATA -p $PG_HOME/bin/postmaster -o '-i -p ${PGPORT}' start > /dev/null 2>&1" < /dev/null
sleep 1
pid=`pidof -s $PG_HOME/bin/postmaster`
if [ $pid ]
then
if echo "$TYPESET"grep "declare -f success" >/dev/null
then
success "$PSQL_START"
else
echo " [ OK ]"
fi
touch /var/lock/subsys/${NAME}
echo $pid > /var/run/postmaster.${PGPORT}.pid
echo
else
if echo "$TYPESET"grep "declare -f failure" >/dev/null
then
failure "$PSQL_START"
else
echo " [ FAILED ]"
fi
echo
fi
fi
}
stop(){
echo -n "Stopping $NAME..."
PSQL_STOP=$"Stopping ${NAME} service: "
su -l postgres -s /bin/sh -c "$PG_HOME/bin/pg_ctl stop -D $PGDATA -s -m fast" > /dev/null 2>&1
ret=$?
if [ $ret -eq 0 ]
then
if echo "$TYPESET"grep "declare -f success" >/dev/null
then
success "$PSQL_STOP"
else
echo " [ OK ]"
fi
else
if echo "$TYPESET"grep "declare -f failure" >/dev/null
then
failure "$PSQL_START"
else
echo " [ FAILED ]"
fi
fi
echo
rm -f /var/run/postmaster.${PGPORT}.pid
rm -f /var/lock/subsys/${NAME}
echo
}
restart(){
stop
start
}
condrestart(){
[ -e /var/lock/subsys/${NAME} ] && restart
}
reload(){
su -l postgres -s /bin/sh -c "$PG_HOME/bin/pg_ctl reload -D $PGDATA -s" > /dev/null 2>&1
}
# This script is slightly unusual in that the name of the daemon (postmaster)
# is not the same as the name of the subsystem (postgresql)
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status postmaster
;;
restart)
restart
;;
condrestart)
condrestart
;;
reloadforce-reload)
reload
;;
*)
echo $"Usage: $0 {startstopstatusrestartcondrestartreloadforce-reload}"
exit 1
esac
exit 0

/etc/rc.d/init.d/httpd

#!/bin/bash
#
# Startup script for the Apache Web Server
#
# chkconfig: 345 85 15
# description: Apache is a World Wide Web server. It is used to serve # HTML files and CGI.
# processname: httpd
# pidfile: /var/run/httpd.pid
# config: /usr/local/apache2/conf/access.conf
# config: /usr/local/apache2/conf/httpd.conf
# config: /usr/local/apache2/conf/srm.conf
# Source function library.
. /etc/rc.d/init.d/functions
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache2/bin/apachectl
httpd=/usr/local/apache2/bin/httpd
prog=httpd
RETVAL=0
# Find the installed modules and convert their names into arguments httpd
# can use.
moduleargs() {
moduledir=/usr/local/apache2/modules
moduleargs=`
/usr/bin/find ${moduledir} -type f -perm -0100 -name "*.so" env -i tr '[:lower:]' '[:upper:]' awk '{gsub(/.*\//,"");gsub(/^MOD_/,"");gsub(/^LIB/,"");gsub(/\.SO$/,"");print "-DHAVE_" $0}'`echo ${moduleargs}
}
# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure. So we just do it the way init scripts
# are expected to behave here.
start() {
echo -n $"Starting $prog: with SSL"
daemon $httpd -DSSL `moduleargs` $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/httpd
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/httpd /var/run/httpd.pid
}
reload() {
echo -n $"Reloading $prog: "
killproc $httpd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
startssl)
start
;;
stop)
stop
;;
status)
status $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f /var/run/httpd.pid ] ; then
stop
start
fi
;;
reload)
reload
;;
gracefulhelpconfigtest)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {startstartsslstoprestartcondrestartreloadstatusfullstatusgracefulhelpconfigtest}"
exit 1
esac
exit $RETVAL

chkconfig
This installs scripts to start at startup and shutdown when system is halted
After you've copied all daemon scripts to /etc/rc.d/init.d, make sure you've changed the chkconfig line to reflect the appropriate runlevels
First make sure they are disabled, or else chkconfig sometimes gets confused
/sbin/chkconfig --del tomcatd
/sbin/chkconfig --del httpd
/sbin/chkconfig --del postgresql
Now add them
/sbin/chkconfig --add tomcatd
/sbin/chkconfig --add httpd
/sbin/chkconfig --add postgresql

No comments:

Post a Comment