Blame SOURCES/scl-tomcat-initd.sh

8aa4af
#!/bin/bash
8aa4af
#
8aa4af
# rh-thermostat16-thermostat-tomcat    This shell script takes care of deploying SCL-ized thermostat
8aa4af
#                                  webapp.
8aa4af
#
8aa4af
# chkconfig: - 80 20
8aa4af
#
8aa4af
### BEGIN INIT INFO
8aa4af
# Provides: __service_name__
8aa4af
# Required-Start: $network $syslog
8aa4af
# Required-Stop: $network $syslog
8aa4af
# Default-Start:
8aa4af
# Default-Stop:
8aa4af
# Description: Thermostat tomcat start wrapper
8aa4af
# Short-Description: start and stop Thermostat's tomcat
8aa4af
### END INIT INFO
8aa4af
#
8aa4af
# Clone of tomcat6's init script.
8aa4af
#
8aa4af
8aa4af
## Source function library.
8aa4af
#. /etc/rc.d/init.d/functions
8aa4af
# Source LSB function library.
8aa4af
if [ -r /lib/lsb/init-functions ]; then
8aa4af
    . /lib/lsb/init-functions
8aa4af
else
8aa4af
    exit 1
8aa4af
fi
8aa4af
8aa4af
DISTRIB_ID=`lsb_release -i -s 2>/dev/null`
8aa4af
8aa4af
NAME="$(basename $0)"
8aa4af
unset ISBOOT
8aa4af
if [ "${NAME:0:1}" = "S" -o "${NAME:0:1}" = "K" ]; then
8aa4af
    NAME="${NAME:3}"
8aa4af
    ISBOOT="1"
8aa4af
fi
8aa4af
8aa4af
# For SELinux we need to use 'runuser' not 'su'
8aa4af
if [ -x "/sbin/runuser" ]; then
8aa4af
    SU="/sbin/runuser -s /bin/sh"
8aa4af
else
8aa4af
    SU="/bin/su -s /bin/sh"
8aa4af
fi
8aa4af
8aa4af
# Get the tomcat config (use this for environment specific settings)
8aa4af
TOMCAT_CFG="/etc/tomcat6/tomcat6.conf"
8aa4af
if [ -r "$TOMCAT_CFG" ]; then
8aa4af
    . $TOMCAT_CFG
8aa4af
fi
8aa4af
8aa4af
# Get instance specific config file
8aa4af
if [ -r "/etc/sysconfig/${NAME}" ]; then
8aa4af
    . /etc/sysconfig/${NAME}
8aa4af
fi
8aa4af
8aa4af
# Define which connector port to use
8aa4af
CONNECTOR_PORT="${CONNECTOR_PORT:-8080}"
8aa4af
8aa4af
# Path to the tomcat launch script
8aa4af
TOMCAT_SCRIPT="/usr/sbin/tomcat6"
8aa4af
8aa4af
# Tomcat program name
8aa4af
TOMCAT_PROG="${NAME}"
8aa4af
        
8aa4af
# Define the tomcat username
8aa4af
TOMCAT_USER="${TOMCAT_USER:-tomcat}"
8aa4af
8aa4af
# Define the tomcat log file
8aa4af
TOMCAT_LOG="${TOMCAT_LOG:-/var/log/${NAME}-initd.log}"
8aa4af
8aa4af
# Define the pid file name
8aa4af
# If change is needed, use sysconfig instead of here
8aa4af
export CATALINA_PID="${CATALINA_PID:-/var/run/${NAME}.pid}"
8aa4af
8aa4af
RETVAL="0"
8aa4af
8aa4af
function parseOptions() {
8aa4af
    options=""
8aa4af
    options="$options $(
8aa4af
                 awk '!/^#/ && !/^$/ { ORS=" "; print "export ", $0, ";" }' \
8aa4af
                 $TOMCAT_CFG
8aa4af
             )"
8aa4af
    if [ -r "/etc/sysconfig/${NAME}" ]; then
8aa4af
        options="$options $(
8aa4af
                     awk '!/^#/ && !/^$/ { ORS=" "; 
8aa4af
                                           print "export ", $0, ";" }' \
8aa4af
                     /etc/sysconfig/${NAME}
8aa4af
                 )"
8aa4af
    fi
8aa4af
    TOMCAT_SCRIPT="$options ${TOMCAT_SCRIPT}"
8aa4af
}
8aa4af
8aa4af
# rhbz 757632
8aa4af
function version() {
8aa4af
	parseOptions
8aa4af
	$SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} version" >> ${TOMCAT_LOG} 2>&1 || RETVAL="4"
8aa4af
}
8aa4af
8aa4af
# See how we were called.
8aa4af
function start() {
8aa4af
  
8aa4af
   echo -n "Starting ${TOMCAT_PROG}: "
8aa4af
   if [ "$RETVAL" != "0" ]; then 
8aa4af
     log_failure_msg
8aa4af
     return
8aa4af
   fi
8aa4af
   if [ -f "/var/lock/subsys/${NAME}" ]; then
8aa4af
        if [ -f "${CATALINA_PID}" ]; then
8aa4af
            read kpid < ${CATALINA_PID}
8aa4af
#           if checkpid $kpid 2>&1; then
8aa4af
            if [ -d "/proc/${kpid}" ]; then
8aa4af
                log_success_msg
8aa4af
                if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
8aa4af
                    echo
8aa4af
                fi
8aa4af
                RETVAL="0"
8aa4af
                return
8aa4af
            fi
8aa4af
        fi
8aa4af
    fi
8aa4af
    # fix permissions on the log and pid files
8aa4af
    touch $CATALINA_PID 2>&1 || RETVAL="4"
8aa4af
    if [ "$RETVAL" -eq "0" -a "$?" -eq "0" ]; then 
8aa4af
      chown ${TOMCAT_USER}:${TOMCAT_USER} $CATALINA_PID
8aa4af
    fi
8aa4af
    parseOptions
8aa4af
    if [ "$RETVAL" -eq "0" -a "$SECURITY_MANAGER" = "true" ]; then
8aa4af
        $SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} start-security" \
8aa4af
            >> ${TOMCAT_LOG} 2>&1 || RETVAL="4"
8aa4af
    else
8aa4af
       
8aa4af
       [ "$RETVAL" -eq "0" ] && $SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} start" >> ${TOMCAT_LOG} 2>&1 || RETVAL="4"
8aa4af
    fi
8aa4af
    if [ "$RETVAL" -eq "0" ]; then 
8aa4af
        log_success_msg
8aa4af
        touch /var/lock/subsys/${NAME}
8aa4af
    else
8aa4af
        log_failure_msg "Error code ${RETVAL}"
8aa4af
    fi
8aa4af
    if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
8aa4af
        echo
8aa4af
    fi
8aa4af
}
8aa4af
8aa4af
function stop() {
8aa4af
    echo -n "Stopping ${TOMCAT_PROG}: "
8aa4af
    if [ -f "/var/lock/subsys/${NAME}" ]; then
8aa4af
      parseOptions
8aa4af
      if [ "$RETVAL" -eq "0" ]; then
8aa4af
         touch /var/lock/subsys/${NAME} 2>&1 || RETVAL="4"
8aa4af
         [ "$RETVAL" -eq "0" ] && $SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} stop" >> ${TOMCAT_LOG} 2>&1 || RETVAL="4"
8aa4af
      fi
8aa4af
      if [ "$RETVAL" -eq "0" ]; then
8aa4af
         count="0"
8aa4af
         if [ -f "${CATALINA_PID}" ]; then
8aa4af
            read kpid < ${CATALINA_PID}
8aa4af
            until [ "$(ps --pid $kpid | grep -c $kpid)" -eq "0" ] || \
8aa4af
                      [ "$count" -gt "$SHUTDOWN_WAIT" ]; do
8aa4af
                    if [ "$SHUTDOWN_VERBOSE" = "true" ]; then
8aa4af
                        echo "waiting for processes $kpid to exit"
8aa4af
                    fi
8aa4af
                    sleep 1
8aa4af
                    let count="${count}+1"
8aa4af
                done
8aa4af
                if [ "$count" -gt "$SHUTDOWN_WAIT" ]; then
8aa4af
                    if [ "$SHUTDOWN_VERBOSE" = "true" ]; then
8aa4af
                        log_warning_msg "killing processes which did not stop after ${SHUTDOWN_WAIT} seconds"
8aa4af
                    fi
8aa4af
                    kill -9 $kpid
8aa4af
                fi
8aa4af
                log_success_msg
8aa4af
            fi
8aa4af
            rm -f /var/lock/subsys/${NAME} ${CATALINA_PID}
8aa4af
        else
8aa4af
            log_failure_msg
8aa4af
            RETVAL="4"
8aa4af
        fi
8aa4af
    else
8aa4af
        log_success_msg
8aa4af
        RETVAL="0"
8aa4af
    fi
8aa4af
    if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
8aa4af
        echo
8aa4af
    fi
8aa4af
}
8aa4af
8aa4af
function status()
8aa4af
{
8aa4af
   checkpidfile 
8aa4af
   if [ "$RETVAL" -eq "0" ]; then
8aa4af
      log_success_msg "${NAME} (pid ${kpid}) is running..."
8aa4af
   elif [ "$RETVAL" -eq "1" ]; then
8aa4af
      log_failure_msg "PID file exists, but process is not running"
8aa4af
   else 
8aa4af
      checklockfile
8aa4af
      if [ "$RETVAL" -eq "2" ]; then
8aa4af
         log_failure_msg "${NAME} lockfile exists but process is not running"
8aa4af
      else
8aa4af
         pid="$(/usr/bin/pgrep -u ${TOMCAT_USER} -f tomcat)"
8aa4af
         if [ -z "$pid" ]; then
8aa4af
             log_success_msg "${NAME} is stopped"
8aa4af
             RETVAL="3"
8aa4af
         else
8aa4af
             log_success_msg "${NAME} (pid $pid) is running..."
8aa4af
             RETVAL="0"
8aa4af
         fi
8aa4af
      fi
8aa4af
  fi
8aa4af
}
8aa4af
8aa4af
function checklockfile()
8aa4af
{
8aa4af
   if [ -f /var/lock/subsys/${NAME} ]; then
8aa4af
      pid="$(/usr/bin/pgrep -u ${TOMCAT_USER} -f tomcat)"
8aa4af
# The lockfile exists but the process is not running
8aa4af
      if [ -z "$pid" ]; then
8aa4af
         RETVAL="2"
8aa4af
      fi
8aa4af
   fi
8aa4af
}
8aa4af
8aa4af
function checkpidfile()
8aa4af
{
8aa4af
   if [ -f "${CATALINA_PID}" ]; then
8aa4af
      read kpid < ${CATALINA_PID}
8aa4af
      if [ -d "/proc/${kpid}" ]; then
8aa4af
# The pid file exists and the process is running
8aa4af
          RETVAL="0"
8aa4af
      else
8aa4af
# The pid file exists but the process is not running
8aa4af
         RETVAL="1"
8aa4af
         return
8aa4af
      fi
8aa4af
   fi
8aa4af
# pid file does not exist and program is not running
8aa4af
   RETVAL="3"
8aa4af
}
8aa4af
8aa4af
function usage()
8aa4af
{
8aa4af
   echo "Usage: $0 {start|stop|restart|condrestart|try-restart|reload|force-reload|status|version}"
8aa4af
   RETVAL="2"
8aa4af
}
8aa4af
8aa4af
# See how we were called.
8aa4af
RETVAL="0"
8aa4af
case "$1" in
8aa4af
    start)
8aa4af
        start
8aa4af
        ;;
8aa4af
    stop)
8aa4af
        stop
8aa4af
        ;;
8aa4af
    restart)
8aa4af
        stop
8aa4af
        start
8aa4af
        ;;
8aa4af
    condrestart|try-restart)
8aa4af
        if [ -f "/var/run/${NAME}.pid" ]; then
8aa4af
            stop
8aa4af
            start
8aa4af
        fi
8aa4af
        ;;
8aa4af
    reload)
8aa4af
        RETVAL="3"
8aa4af
        ;;
8aa4af
    force-reload)
8aa4af
        if [ -f "/var/run/${NAME}.pid" ]; then
8aa4af
            stop
8aa4af
            start
8aa4af
        fi
8aa4af
        ;;
8aa4af
    status)
8aa4af
        status
8aa4af
        ;;
8aa4af
    version)
8aa4af
	 	version
8aa4af
#        ${TOMCAT_SCRIPT} version
8aa4af
        ;;
8aa4af
    *)
8aa4af
      usage
8aa4af
      ;;
8aa4af
esac
8aa4af
8aa4af
exit $RETVAL