1-Overview
The script catalina. sh is the most critical script for starting and disabling tomcat servers.
In addition, startup. sh and shutdown. sh both use different parameters to call the script.
The script is used as follows (derived from the script itself ):
echo "Usage: catalina.sh ( commands ... )" echo "commands:" echo " debug Start Catalina in a debugger" echo " debug -security Debug Catalina with a security manager" echo " embedded Start Catalina in embedded mode" echo " jpda start Start Catalina under JPDA debugger" echo " run Start Catalina in the current window" echo " run -security Start in the current window with security manager" echo " start Start Catalina in a separate window" echo " start -security Start in a separate window with security manager" echo " stop Stop Catalina"
2-script analysis
#! /Bin/sh #=============================================== ====================================## this script sets the correct environment variables and system information, start or stop tomcat server # the specific operation is: # Use the command line parameter as the real parameter of args and call org. apache. catalina. startup. bootstrap. main (String []) #===================================================== ==================================#####start/Stop Script for the CATALINA Server ## Environment Variabl E Prequisites # CATALINA_HOME May point at your Catalina "build" directory. # CATALINA_BASE (Optional) Base directory for resolving dynamic portions # of a Catalina installation. if not present, resolves to # the same directory that CATALINA_HOME points. # CATALINA_OPTS (Optional) Java runtime options used when the "start", # "stop", or "run" command is executed. # CATALINA_TMPDIR (Optional) Direc Using path location of temporary directory # the JVM shocould use (java. io. tmpdir ). defaults to # $ CATALINA_BASE/temp. # JAVA_HOME Must point at your Java Development Kit installation. # JAVA_OPTS (Optional) Java runtime options used when the "start", # "stop", or "run" command is executed. # JPDA_TRANSPORT (Optional) JPDA transport used when the "jpda start" # command is executed. the default is "dt_soc Ket ". # JPDA_ADDRESS (Optional) Java runtime options used when the "jpda start" # command is executed. the default is 8000. # JSSE_HOME (Optional) May point at your Java Secure Sockets Extension # (JSSE) installation, whose JAR files will be added to the # system class path used to start Tomcat. # CATALINA_PID (Optional) Path of the file which shoshould contains the pid # of catalina startup java process, When start (fork) is used # --------------------------------------------------------------------------- # OS specific support. $ var _ must _ be set to either true or false. cygwin = falsecase "'uname'" inCYGWIN *) cygwin = true; esac # PRG is the name of the executed script, which can be considered as PRG = "catalina. sh ", or a symbolic chain pointing to the script. PRG = "$0" # After processing the PRG, The PRG becomes ???? While [-h "$ PRG"]; do ls = 'LS-ld "$ PRG" 'link = 'expr "$ ls ":'. *-> \(. * \) $ ''if expr "$ link ":'. */. * '>/dev/null; then PRG = "$ link" else PRG = 'dirname "$ PRG" '/"$ link" fidone # PRGDIR is the PRG directory path name PRGDIR = 'dirname "$ PRG "' # Run $ CATALINA_HOME/BIN/setenv. sh to set the environment variable # [-r filename] is to determine whether the file exists and read CATALINA_HOME = 'CD "$ PRGDIR /.. "; pwd' if [-r" $ CATALINA_HOME "/bin/setenv. sh]; then. "$ CATALINA_HOME"/bin/setenv. sh Fi # For Cygwin, make sure all path names comply with the UNIX format if $ cygwin; then [-n "$ JAVA_HOME"] & JAVA_HOME = 'cygpath -- unix "$ JAVA_HOME" '[-n "$ CATALINA_HOME"] & CATALINA_HOME = 'cygpath -- unix "$ CATALINA_HOME "'[-n" $ CATALINA_BASE "] & CATALINA_BASE = 'cygpath -- unix" $ CATALINA_BASE "' [-n" $ CLASSPATH "] & CLASSPATH = 'cygpath -- path -- unix "$ CLASSPATH" '[-n "$ JSSE_HOME"] & JSSE_HOME = 'cygpath -- path -- unix "$ JSSE_HOME "'Fi # Set the CLASSPATH variable used to initialize SystemClassLoader: Execute the script setclasspath. sh complete # Note: The Script setclasspath. sh changed the original $ CLASSPATH variable and set it to: # CLASSPATH = $ CATALINA_HOME/bin/bootstrap. jar: $ JAVA_HOME/lib/tools. jar # bootstrap. jar is package org. apache. catalina. startup package file, # contains the startup method org. apache. catalina. startup. bootstrap. main (String [] args) # tools. jar contains the javac compiler, which is used to compile the jsp file into servlet classif [-r "$ CATALINA_HOME"/bin/setclasspath. sh]; Then BASEDIR = "$ CATALINA_HOME ". "$ CATALINA_HOME"/bin/setclasspath. shelse echo "Cannot find $ CATALINA_HOME/bin/setclasspath. sh "echo" This file is needed to run this program "exit 1fi # add some JAR files after the CLASSPATH environment variable # [-n string] to determine whether the string length is not 0; [-z string] determines whether the string length is 0 # They can be used to determine whether an environment variable has been set if [-n "$ JSSE_HOME"]; then CLASSPATH = "$ CLASSPATH": "$ JSSE_HOME"/lib/jcert. jar: "$ JSSE_HOME"/lib/jnet. jar: "$ JSSE_HO ME "/lib/jsse. jarfiCLASSPATH = "$ CLASSPATH": "$ CATALINA_HOME"/bin/bootstrap. jar # specify the CATALINA_BASE variable # If CATALINA_BASE has no value, make CATALINA_BASE = $ CATALINA_HOMEif [-z "$ CATALINA_BASE"]; then CATALINA_BASE = "$ CATALINA_HOME" fi # specify the CATALINA_TMPDIR variable # If CATALINA_TMPDIR has no value, make CATALINA_TMPDIR = $ CATALINA_BASE/tempif [-z "$ CATALINA_TMPDIR"] then # Define the java. io. tmpdir to use for Catalina CATALINA_TMPDIR = "$ CATALI NA_BASE "/tempfi # For Cygwin, convert the path name to WINDOWS format before running the java program if $ cygwin; then JAVA_HOME = 'cygpath -- path -- windows "$ JAVA_HOME" 'catalina_home = 'cygpath -- path -- windows "$ CATALINA_HOME" 'catalina_base = 'cygpath -- path -- windows "$ CATALINA_BASE "' CATALINA_TMPDIR = 'cygpath -- path -- windows "$ CATALINA_TMPDIR" 'classpath = 'cygpath -- path -- windows "$ CLASSPATH" 'jsse_home = 'cygpath -- path -- windows "$ JSSE_HOME" 'f I # display information, and then run echo "Using CATALINA_BASE: $ CATALINA_BASE" echo "Using CATALINA_HOME: $ CATALINA_HOME" echo "Using CATALINA_TMPDIR: $ CATALINA_TMPDIR" echo "Using JAVA_HOME: $ JAVA_HOME "#...... when catalina. when the sh parameter is jpda ...... if ["$1" = "jpda"]; then if [-z "$ JPDA_TRANSPORT"]; then JPDA_TRANSPORT = "dt_socket" fi if [-z "$ JPDA_ADDRESS"]; then JPDA_ADDRESS = "8000" fi if [-z "$ JPDA_OPTS"]; then JPDA_OP TS = "-Xdebug-Xrunjdwp: transport = $ JPDA_TRANSPORT, address = $ JPDA_ADDRESS, server = y, suspend = n "fi CATALINA_OPTS =" $ CATALINA_OPTS $ JPDA_OPTS "shiftfi #...... when catalina. when the sh parameter is debug ...... if ["$1" = "debug"]; then shift if ["$1" = "-security"]; then echo "Using Security Manager" shift exec "$ _ RUNJDB" $ JAVA_OPTS $ CATALINA_OPTS \-Djava. endorsed. dirs = "$ JAVA_ENDORSED_DIRS"-classpath "$ CLASSPATH "\- Sourcepath "$ CATALINA_HOME "/.. /.. /jakarta-tomcat-4.0/catalina/src/share \-Djava. security. manager \-Djava. security. policy = "$ CATALINA_BASE"/conf/catalina. policy \-Dcatalina. base = "$ CATALINA_BASE" \-Dcatalina. home = "$ CATALINA_HOME" \-Djava. io. tmpdir = "$ CATALINA_TMPDIR" \ org. apache. catalina. startup. bootstrap "$ @" start else exec "$ _ RUNJDB" $ JAVA_OPTS $ CATALINA_OPTS \-Djava. endorsed. dirs = "$ JAVA_EN DORSED_DIRS "-classpath" $ CLASSPATH "\-sourcepath" $ CATALINA_HOME "/.. /.. /jakarta-tomcat-4.0/catalina/src/share \-Dcatalina. base = "$ CATALINA_BASE" \-Dcatalina. home = "$ CATALINA_HOME" \-Djava. io. tmpdir = "$ CATALINA_TMPDIR" \ org. apache. catalina. startup. bootstrap "$ @" start fi #...... when catalina. when the sh parameter is embedded ...... elif ["$1" = "embedded"]; then shift echo "Embedded Classpath: $ CLASSPATH" exec" $ _ RUNJAVA "$ JAVA_OPTS $ CATALINA_OPTS \-Djava. endorsed. dirs = "$ JAVA_ENDORSED_DIRS"-classpath "$ CLASSPATH" \-Dcatalina. base = "$ CATALINA_BASE" \-Dcatalina. home = "$ CATALINA_HOME" \-Djava. io. tmpdir = "$ CATALINA_TMPDIR" \ org. apache. catalina. startup. embedded "$ @" # + ---------------------------------------------------- + # | ...... when catalina. when the sh parameter is run ...... | # | start the tomcat server in the current window !!! | # + -------------------------------------------------- + Elif ["$1" = "run"]; then # Remove the parameter run shift # If the parameter is run-security, start Security Manager if ["$1" = "-security"]; then echo "Using Security Manager" shift exec "$ _ RUNJAVA" $ JAVA_OPTS $ CATALINA_OPTS \-Djava. endorsed. dirs = "$ JAVA_ENDORSED_DIRS"-classpath "$ CLASSPATH" \-Djava. security. manager \-Djava. security. policy = "$ CATALINA_BASE"/conf/ca Talina. policy \-Dcatalina. base = "$ CATALINA_BASE" \-Dcatalina. home = "$ CATALINA_HOME" \-Djava. io. tmpdir = "$ CATALINA_TMPDIR" \ org. apache. catalina. startup. bootstrap "$ @" start # If the parameter is run alone, start the tomcat server in this window # The method to start in this window is to use exec, let the current process fork a new process to start tomcat. The current process is the parent process of tomcat # The tomcat startup class is org. apache. catalina. startup. bootstrap. main ("start"); else exec "$ _ RUNJAVA" $ JAVA_OPTS $ CATALINA_OPTS \-Djava. endorsed. dir S = "$ JAVA_ENDORSED_DIRS"-classpath "$ CLASSPATH" \-Dcatalina. base = "$ CATALINA_BASE" \-Dcatalina. home = "$ CATALINA_HOME" \-Djava. io. tmpdir = "$ CATALINA_TMPDIR" \ org. apache. catalina. startup. bootstrap "$ @" start fi # + ---------------------------------------------------- + # | ...... when catalina. when the sh parameter is start ...... | # | start the tomcat server in a new window !!! | # + -------------------------------------------------- + Elif ["$1" = "start"]; then # Remove the start parameter shift # create a file (if the file does not exist) $ CATALINA_BASE/logs/catalina. out touch "$ CATALINA_BASE"/logs/catalina. out # if the parameter is start-security, start Security Manager if ["$1" = "-security"]; then echo "Using Security Manager" shift "$ _ RUNJAVA" $ JAVA_OPTS $ CATALINA_OPTS \-Djava. endorsed. dirs = "$ JAVA_ENDORSED_DIRS"-clas Spath "$ CLASSPATH" \-Djava. security. manager \-Djava. security. policy = "$ CATALINA_BASE"/conf/catalina. policy \-Dcatalina. base = "$ CATALINA_BASE" \-Dcatalina. home = "$ CATALINA_HOME" \-Djava. io. tmpdir = "$ CATALINA_TMPDIR" \ org. apache. catalina. startup. bootstrap "$ @" start \> "$ CATALINA_BASE"/logs/catalina. out 2> & 1 & if [! -Z "$ CATALINA_PID"]; then echo $!> $ CATALINA_PID fi # If the parameter is a standalone start, start tomcat else "$ _ RUNJAVA" $ JAVA_OPTS $ CATALINA_OPTS \-Djava in the new window. endorsed. dirs = "$ JAVA_ENDORSED_DIRS"-classpath "$ CLASSPATH" \-Dcatalina. base = "$ CATALINA_BASE" \-Dcatalina. home = "$ CATALINA_HOME" \-Djava. io. tmpdir = "$ CATALINA_TMPDIR" \ org. apache. catalina. startup. bootstrap "$ @" start \> "$ CATALINA_BASE"/logs/catalina. out 2> & 1 & if [! -Z "$ CATALINA_PID"]; then echo $!> $ CATALINA_PID fi # + ------------------------------------------------------ + # | ...... when catalina. when the sh parameter is stop ...... | # | disable the tomcat server !!! | # + -------------------------------------------------- + Elif ["$1" = "stop"]; then # Remove the parameter stop shift # the class for disabling the tomcat server is org. apache. catalina. startup. bootstrap-> main ("stop"); # Note: java-D sets the system attribute exec "$ _ RUNJAVA" $ JAVA_OPTS $ CATALINA_OPTS \-Djava. endorsed. dirs = "$ JAVA_ENDORSED_DIRS"-classpath "$ CLASSPATH" \-Dcatalina. base = "$ CATALINA_BASE" \-Dcatalina. home = "$ CATALINA_HOME" \-Djava. io. tmpdir = "$ CA TALINA_TMPDIR "\ org. apache. catalina. startup. bootstrap "$ @" stop # + ---------------------------------------------------- + # | ...... when catalina. when sh parameters are unidentifiable ...... | # | print the help information to view the meaning of various parameters !!! | # + ---------------------------------------------------- + Else echo "Usage: catalina. sh (commands ...) "echo" commands: "echo" debug Start Catalina in a debugger "echo" debug-security Debug Catalina with a security manager "echo" embedded Start Catalina in embedded mode "echo" jpda start Start Catalina under JPDA debugger "echo" run Start Catalina in the current window "echo" run-security Start in the current window with security manager "echo" start Start Catalina in a separate window "echo" start-security start in a separate window with security manager "echo" stop Stop Catalina "exit 1fi