# Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.
# ----------------------------------------------------------------------------- # Start Script for the CATALINA Server # -----------------------------------------------------------------------------
while [ -h "$PRG" ] ; do ls=`ls -ld "$PRG"` link=`expr "$ls" : '.*-> \(.*\)$'` if expr "$link" : '/.*' > /dev/null; then PRG="$link" else PRG=`dirname "$PRG"`/"$link" fi done
PRGDIR=`dirname "$PRG"` EXECUTABLE=catalina.sh
# Check that target executable exists if$os400; then # -x will Only work on the os400 if the files are: # 1. owned by the user # 2. owned by the PRIMARY group of the user # this will not work if the user belongs in secondary groups eval else if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then echo"Cannot find $PRGDIR/$EXECUTABLE" echo"The file is absent or does not have execute permission" echo"This file is needed to run this program" exit 1 fi fi
➜ bin catalina.sh Using CATALINA_BASE: /Users/qishengli/software/apache-tomcat-8.5.32 Using CATALINA_HOME: /Users/qishengli/software/apache-tomcat-8.5.32 Using CATALINA_TMPDIR: /Users/qishengli/software/apache-tomcat-8.5.32/temp Using JRE_HOME: /Users/qishengli/software/jdk8/jre Using CLASSPATH: /Users/qishengli/software/apache-tomcat-8.5.32/bin/bootstrap.jar:/Users/qishengli/software/apache-tomcat-8.5.32/bin/tomcat-juli.jar Usage: catalina.sh ( commands ... ) commands: debug Start Catalina in a debugger debug -security Debug Catalina with a security manager jpda start Start Catalina under JPDA debugger run Start Catalina in the current window run -security Start in the current window with security manager start Start Catalina in a separate window start -security Start in a separate window with security manager stop Stop Catalina, waiting up to 5 seconds for the process to end stop n Stop Catalina, waiting up to n seconds for the process to end stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running configtest Run a basic syntax check on server.xml - check exit code for result version What version of tomcat are you running? Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined
比如version:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
➜ bin catalina.sh version Using CATALINA_BASE: /Users/qishengli/software/apache-tomcat-8.5.32 Using CATALINA_HOME: /Users/qishengli/software/apache-tomcat-8.5.32 Using CATALINA_TMPDIR: /Users/qishengli/software/apache-tomcat-8.5.32/temp Using JRE_HOME: /Users/qishengli/software/jdk8/jre Using CLASSPATH: /Users/qishengli/software/apache-tomcat-8.5.32/bin/bootstrap.jar:/Users/qishengli/software/apache-tomcat-8.5.32/bin/tomcat-juli.jar Server version: Apache Tomcat/8.5.32 Server built: Jun 20 2018 19:50:35 UTC Server number: 8.5.32.0 OS Name: Mac OS X OS Version: 11.6 Architecture: aarch64 JVM Version: 1.8.0_282-b08 JVM Vendor: Azul Systems, Inc.
➜ bin cat setenv.sh export CATALINA_OPTS="-agentpath:/Users/qishengli/Downloads/async-profiler-2.5-macos/build/libasyncProfiler.so=start,event=cpu,interval=1ms,file=profile.html -Djava.rmi.server.logCalls=true -Dsun.rmi.server.logLevel=debug"
对应的脚本位置:
1 2 3 4 5 6 7
➜ bin grep -n setenv catalina.sh 24:# setenv.sh in CATALINA_BASE/bin to keep your customizations separate. 145:# but allow them to be specified in setenv.sh, in rare case when it is needed. 148:if [ -r "$CATALINA_BASE/bin/setenv.sh" ]; then 149: . "$CATALINA_BASE/bin/setenv.sh" 150:elif [ -r "$CATALINA_HOME/bin/setenv.sh" ]; then 151: . "$CATALINA_HOME/bin/setenv.sh"
/Users/qishengli/software/apache-tomcat-8.5.32/bin/catalina.sh run NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED -Dcatalina.base=/Users/qishengli/Library/Caches/JetBrains/IntelliJIdea2021.2/tomcat/15632928-a384-44e8-ba78-fe9ca3f37059 [2021-10-31 05:19:05,458] Artifact web:war exploded: Waiting for server connection to start artifact deployment...
catalina.sh run starts tomcat in the foreground, displaying the logs on the console that you started it. Hitting Ctrl-C will terminate tomcat.
startup.sh will start tomcat in the background. You’ll have to tail -f logs/catalina.out to see the logs.
Both will do the same things, apart from the foreground/background distinction.
后续的流程就到了java代码里
Java代码中的启动流程
Bootstrap
The purpose of this roundabout approach is to keep the Catalina internal classes (and any other classes they depend on, such as an XML parser) out of the system class path and therefore not visible to application level classes.
38 # List of comma-separated paths defining the contents of the "common" 39 # classloader. 53 common.loader="${catalina.base}/lib","${catalina.base}/lib/*.jar","${catalina.home}/lib","${catalina.home}/lib/*.jar"
56 # List of comma-separated paths defining the contents of the "server" 57 # classloader. 71 server.loader=
73 # 74 # List of comma-separated paths defining the contents of the "shared" 75 # classloader. 90 shared.loader=
"javaee connector"#5620 prio=4 os_prio=31 cpu=17.64ms elapsed=926.91s tid=0x000000036be2e400 nid=0x4e78b runnable [0x000000039bbb9000] java.lang.Thread.State: RUNNABLE at java.net.SocketInputStream.socketRead0(java.base@11.0.12/Native Method) at java.net.SocketInputStream.socketRead(java.base@11.0.12/SocketInputStream.java:115) at java.net.SocketInputStream.read(java.base@11.0.12/SocketInputStream.java:168) at java.net.SocketInputStream.read(java.base@11.0.12/SocketInputStream.java:140) at java.io.BufferedInputStream.fill(java.base@11.0.12/BufferedInputStream.java:252) at java.io.BufferedInputStream.read(java.base@11.0.12/BufferedInputStream.java:271) - locked <0x0000000794f63408> (a java.io.BufferedInputStream) at java.io.DataInputStream.readByte(java.base@11.0.12/DataInputStream.java:270) at sun.rmi.transport.StreamRemoteCall.executeCall(java.rmi@11.0.12/StreamRemoteCall.java:240) at sun.rmi.server.UnicastRef.invoke(java.rmi@11.0.12/UnicastRef.java:164) at jdk.jmx.remote.internal.rmi.PRef.invoke(jdk.remoteref/Unknown Source) at javax.management.remote.rmi.RMIConnectionImpl_Stub.invoke(java.management.rmi@11.0.12/Unknown Source) at javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection.invoke(java.management.rmi@11.0.12/RMIConnector.java:1021) at com.intellij.javaee.oss.util.AbstractConnectorCommand.invokeOperation(AbstractConnectorCommand.java:139) at org.jetbrains.idea.tomcat.admin.TomcatAdminServerBase$2.doExecute(TomcatAdminServerBase.java:159) at org.jetbrains.idea.tomcat.admin.TomcatAdminServerBase$2.doExecute(TomcatAdminServerBase.java:155) at com.intellij.javaee.oss.util.AbstractConnectorCommand$1.call(AbstractConnectorCommand.java:36) at java.util.concurrent.FutureTask.run(java.base@11.0.12/FutureTask.java:264) at java.util.concurrent.ThreadPoolExecutor.runWorker(java.base@11.0.12/ThreadPoolExecutor.java:1128) at java.util.concurrent.ThreadPoolExecutor$Worker.run(java.base@11.0.12/ThreadPoolExecutor.java:628) at java.lang.Thread.run(java.base@11.0.12/Thread.java:829)
/* * Decompiled with CFR. * * Could not load the following classes: * org.jetbrains.idea.tomcat.admin.TomcatJmxAdminServerBase * org.jetbrains.idea.tomcat.admin.TomcatJmxAdminServerBase$TomcatConnectorCommandBase */ package org.jetbrains.idea.tomcat.admin;