tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David Knaack" <dkna...@rdtech.com>
Subject Helper script
Date Wed, 01 Nov 2000 00:04:58 GMT
Greetings,

I wrote a script to make it easy for me to control tomcats
on my virtual hosts.  It works well for me so I thought I'd
share it, just in case someone else might find it useful.

It provides easy start/stop/restart control over one or
all of the servers.

I call it 'tomcat', and store it in /bin

It uses the startup.sh and shutdown.sh scripts with the -f
parameter to specify what server.xml file to use with each
virtual domain.  Check the config section at the top to
set up variables for your installation.

This is suitable for calling from an init script to
control all the tomcat servers by using the 'all' parameter.

Hope someone else finds it useful!
DK

#!/bin/sh
#
# Control tomcat for virtual domains
# hacked together by dknaack@rdtech.com, use at your own risk.

# ----- Installation-specific Configuration options ------

# lookup arrays for virtual server names (all refers to all servers)
# shorthand names for domains
DOMAINIDS="all s1 s2"
# full names for domains, used to refer to server.xml files for the domain
SERVERS="all sample1.com sample2.org"

# full path to startup.sh and shutdown scripts provided with tomcat
STARTUP_SH="/usr/local/jakarta-tomcat/bin/startup.sh"
SHUTDOWN_SH="/usr/local/jakarta-tomcat/bin/shutdown.sh"

# location of server.xml files for virtual domains
CONFIG_DIR="/etc/tomcat"

# server.xml files are named as "sample1.com.xml" where "sample1.com" is
# the domain name as it appears in the SERVERS array above

# ---- End config options ---

# display useage message and exit
function Usage () {
      echo ""
      echo "  USAGE: tomcat [domain id] [command]"
      echo ""
      echo "  COMMANDS:"
      echo "         start   Start tomcat service on server [domain id]"
      echo "         stop    Stop tomcat service on  server [domain id]"
      echo "         restart Restart tomcat service on server [domain id]"
      echo ""
      echo ""
      echo "  Known domain id's are:"
      echo ""
      echo "   ID                 Server name"
      echo "   --                 -----------------------"

      COUNT=1
      for i in $DOMAINIDS; do
         SNAME=`echo $SERVERS | cut -d" " -f$COUNT`
         echo "   $i                 $SNAME"
         COUNT=`expr $COUNT + 1`
      done;

      echo ""
      echo "  EXAMPLE:"
      echo ""
      echo "    Start tomcat for example.com (where example.com has an ID of
ex):"
      echo "    # ./tomcat ex start"
      echo ""
      exit 1
}

function StartServer {
  # $1 = Domain name

  echo -n "Starting tomcat on $1"
  $STARTUP_SH -f $CONFIG_DIR/$1.xml
}

function StopServer {
  # $1 = Domain name

  echo -n "Stopping tomcat on $1:"
  $SHUTDOWN_SH -f $CONFIG_DIR/$1.xml
}

function Control {
  # $1 = Domain name
  # $2 = Action, start, stop, restart or test

   case "$2" in

      start)
         StartServer $1
      ;;

      stop)
         StopServer $1
      ;;

      restart)
         StopServer $1
         StartServer $1
      ;;

      test)
         echo "Testing, Domain = $1    Action = $2"
      ;;

   esac
}



# Make sure user supplied two and only two parameters
if [ $# != 2 ]; then
   echo ""
   echo " ** Incorrect number of parameters **"
   Usage
fi


# this is a really cheezy way to check that the paramaters are
# valid because I couldn't figure out an easy way to check if
# a string is a member of an array
#
ISVALID=0
for i in SERVERS; do
   if [ $i = $1 ]; then
      ISVALID=1
   fi
done;
if [ $ISVALID = 0 ]; then
   echo ""
   echo "** Invalid domain: $1 **"
   Usage
fi

ISVALID=0
for i in start stop restart test; do
   if [ $i = $2 ]; then
      ISVALID=1
   fi
done;
if [ $ISVALID = 0 ]; then
   echo ""
   echo " ** Invalid command: $2 **"
   Usage
fi


# Figure out which server the user has specified
COUNT=1
for i in $DOMAINIDS; do
   if [ $i = $1 ]; then
      DOMAIN_NAME=`echo $SERVERS | cut -d" " -f$COUNT`
   fi
   COUNT=`expr $COUNT + 1`
done;


# Take the action specified
if [ $DOMAIN_NAME = "all" ]; then

  # call Control for all servers (except for 'all')
  for i in $SERVERS; do
    if [ $i != "all" ]; then
       Control $i $2
    fi
  done;

else

   Control $DOMAIN_NAME $2

fi

exit 0



Mime
View raw message