CCL Home Page
Up Directory CCL apache-tomcat.txt
#!/bin/sh
#
# Startup script for the Apache Web Server
# derived from the /etc/init.d/httpd script which came with RH7.0
# Modified dramatically by Jan Labanowski on 2001.04.18
# Note, this scripts needs modified Tomcat's startup.sh, shutdown.sh and
# tomcat.sh. You need to look at versions of these scripts which come with
# this file and carefully read the README.html file.
#

APACHE_HOME=/usr/freeware/apache
APACHE_PID=${APACHE_HOME}/var/run/httpd.pid
APACHE_MODULES=${APACHE_HOME}/libexec
JAKARTA_HOME=/usr/freeware/jakarta-tomcat
TOMCAT_HOME=${JAKARTA_HOME}/jakarta-tomcat-3.2.1
TOMCAT_PID=${TOMCAT_HOME}/logs/tomcat.pid
export JAKARTA_HOME TOMCAT_HOME APACHE_HOME

DATE=`date`;

# Path to the httpd binary.
HTTPD=${APACHE_HOME}/sbin/httpd

moduleargs() {
# this function returns a list like this:
#   -D HAVE_CRYPTOD -D HAVE_PROXY -D HAVE_SSL ... -D HAVE_VHOST_ALIAS
# which lists all the DSO Apache modules available for loading in httpd.conf
        moduledir=$APACHE_MODULES
        moduleargs=
        for module in ${moduledir}/*.so ; do
                if [ -x ${module} ] ; then
                        module=`echo ${module} | awk '{\
                                gsub(".*/","");\
                                gsub("^mod_","");\
                                gsub("^lib","");\
                                gsub("\.so$","");\
                                print toupper($0)}'`
                        moduleargs="${moduleargs} -D HAVE_$module"
                fi
        done
        echo ${moduleargs}
}

is_tomcat_running() {
# returns "yes" if process id of tomcat given as param is among the
# running tomcat processes, and "no" if pid given is not a running
# tomcat process

 theanswer="no"
 for thepin in `ps -ef | grep -v grep | grep Dtomcat.home | awk '{ print $2 }'`
 do
    if [ "${thepin}" == "$1" ] ; then
      theanswer="yes"
    fi
 done
 echo $theanswer
}

is_httpd_running() {
# returns "yes" if the process id of httpd parent process (i.e., the one owned
# by root, not by nobody) given as param is among the running httpd
# parent processes, and "no" if pid given is not a running apache root process

 theanswer="no"
 for thepin in `ps -ef | grep -v grep | grep /usr/freeware/apache/sbin/httpd | awk '{ print $2 }'`
 do
    if [ "${thepin}" == "$1" ] ; then
      theanswer="yes"
    fi
 done
 echo $theanswer
}

status_them() {
# this function checks if Apache and tomcat are running
        echo "Checking if Apache and Tomcat are running"
        if [ -a  $APACHE_PID ] ; then
          pid=`cat $APACHE_PID`
          if [ "`is_httpd_running $pid`" == "yes" ] ; then
              echo "Apache is running. PID of the parent apache is $pid"
          else  
            echo "Apache is not running and was not shut down gracefully..."
          fi
        else
          echo "Apache is not running..."
        fi

        if [ -a  $TOMCAT_PID ] ; then
           pid=`cat $TOMCAT_PID`
           # check if process is still running
           if [ "`is_tomcat_running $pid`" == "yes" ] ; then
              echo "Tomcat is running. PID of Tomcat is $pid"
           else  
              echo "Tomcat is not running and was not shut down gracefully"
           fi
           echo
           return 0
        else
          echo "Tomcat is not running..."
        fi
        echo 
        return 0
}

stop_them() {
# this function shuts down Apache and tomcat
        echo "Shutting down Apache (i.e., httpd): "
        if [ -a  $APACHE_PID ] ; then
          pid=`cat $APACHE_PID`
          if [ "`is_httpd_running $pid`" == "yes" ] ; then
              kill $pid
              sleep 2
          else  
            echo "Apache was not shut down gracefully..."
            rm -f $APACHE_PID
          fi
        else
          echo "Apache is not running..."
        fi

        echo "Shutting down Tomcat: "
        if [ -a  $TOMCAT_PID ] ; then
           pid=`cat $TOMCAT_PID`
           # check if process is still running
           if [ "`is_tomcat_running $pid`" == "yes" ] ; then
             ( su - tomcat -c "echo ========STOP: $DATE =============== >>${TOMCAT_HOME}/logs/t3.2_jk 2>&1  " ) > /dev/null
             ( su - tomcat -c "nohup ${TOMCAT_HOME}/bin/shutdown.sh >>${TOMCAT_HOME}/logs/t3.2_jk 2>&1  "  ) >  /dev/null
             echo
             return 0
           fi
           echo "Warning: Tomcat was not shut down gracefully"
           echo "file $TOMCAT_PID is  removed"
           rm -f $TOMCAT_PID
           echo
           return 0
        else
          echo "Tomcat is not running..."
        fi
        echo 
        return 0  
}

start_them() {
# starts the Tomcat and Apache combination
        echo "Starting Tomcat"
        # check if tomcat.pid file exists [tomcat.sh creates it.]
        if [ -a  $TOMCAT_PID ] ; then
           pid=`cat $TOMCAT_PID`
           # check if process is still running
           if [ "`is_tomcat_running $pid`" == "yes" ] ; then
              echo "Tomcat already running as process $pid. Stop it first..."
              echo "You can stop Tomcat by running:"
              echo "   apache_stop"
              echo "or log in as tomcat and use kill command"
              echo
              return 1
           fi 
           echo "Warning: Tomcat was not shut down gracefully"
           echo "Removing file: $TOMCAT_PID"
           rm -f $TOMCAT_PID
        fi
        ( su - tomcat -c "echo ========START: $DATE =============== >>${TOMCAT_HOME}/logs/t3.2_jk 2>&1  " ) > /dev/null
        ( su - tomcat -c "nohup ${TOMCAT_HOME}/bin/startup.sh >>${TOMCAT_HOME}/logs/t3.2_jk 2>&1  " ) > /dev/null
        echo "Waiting 3 seconds for Tomcat to load"
        sleep 3
        echo "Tomcat is running as process: " `cat $TOMCAT_PID`
        echo "Starting httpd"
        # if pid file for httpd exists
        if [ -a  $APACHE_PID ] ; then
          pid=`cat $APACHE_PID`
          if [ "`is_httpd_running $pid`" == "yes" ] ; then
             echo "Apache is already running as process $pid. Stop it first..."
             echo "You can stop Tomcat by running:"
             echo "   apache_stop"
             echo
             return 1
          fi
          echo "Warning: Apache (httpd) was not shut down gracefully"
          echo "Removing file: $APACHE_PID"
          rm -f $APACHE_PID
        fi
 
        # now, run apache, its httpd.conf puts pid into file $APACHE_PID  
        ${HTTPD} -DSSL `moduleargs` 
        sleep 2
        echo "Apache (i.e., httpd) is running as process:" `cat $APACHE_PID`
        echo
        return 0 
}



case "$1" in
  start)
        start_them
        echo
        ;;
  stop)
        stop_them
        echo
        ;;
  restart)
        stop_them
        sleep 3
        start_them
        echo
        ;;
  status)
	status_them
	;;
  *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
esac

echo 
exit 0
Modified: Fri Apr 20 02:06:49 2001 GMT
Page accessed 10525 times since Tue Feb 4 18:41:15 2003 GMT