1#!/bin/sh
2
3# Licensed to the Apache Software Foundation (ASF) under one or more
4# contributor license agreements.  See the NOTICE file distributed with
5# this work for additional information regarding copyright ownership.
6# The ASF licenses this file to You under the Apache License, Version 2.0
7# (the "License"); you may not use this file except in compliance with
8# the License.  You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18# -----------------------------------------------------------------------------
19# Control Script for the CATALINA Server
20#
21# For supported commands call "catalina.sh help" or see the usage section at
22# the end of this file.
23#
24# Environment Variable Prerequisites
25#
26#   Do not set the variables in this script. Instead put them into a script
27#   setenv.sh in CATALINA_BASE/bin to keep your customizations separate.
28#
29#   CATALINA_HOME   May point at your Catalina "build" directory.
30#
31#   CATALINA_BASE   (Optional) Base directory for resolving dynamic portions
32#                   of a Catalina installation.  If not present, resolves to
33#                   the same directory that CATALINA_HOME points to.
34#
35#   CATALINA_OUT    (Optional) Full path to a file where stdout and stderr
36#                   will be redirected.
37#                   Default is $CATALINA_BASE/logs/catalina.out
38#
39#   CATALINA_OUT_CMD (Optional) Command which will be executed and receive
40#                   as its stdin the stdout and stderr from the Tomcat java
41#                   process. If CATALINA_OUT_CMD is set, the value of
42#                   CATALINA_OUT will be used as a named pipe.
43#                   No default.
44#                   Example (all one line)
45#                   CATALINA_OUT_CMD="/usr/bin/rotatelogs -f $CATALINA_BASE/logs/catalina.out.%Y-%m-%d.log 86400"
46#
47#   CATALINA_OPTS   (Optional) Java runtime options used when the "start",
48#                   "run" or "debug" command is executed.
49#                   Include here and not in JAVA_OPTS all options, that should
50#                   only be used by Tomcat itself, not by the stop process,
51#                   the version command etc.
52#                   Examples are heap size, GC logging, JMX ports etc.
53#
54#   CATALINA_TMPDIR (Optional) Directory path location of temporary directory
55#                   the JVM should use (java.io.tmpdir).  Defaults to
56#                   $CATALINA_BASE/temp.
57#
58#   JAVA_HOME       Must point at your Java Development Kit installation.
59#                   Required to run the with the "debug" argument.
60#
61#   JRE_HOME        Must point at your Java Runtime installation.
62#                   Defaults to JAVA_HOME if empty. If JRE_HOME and JAVA_HOME
63#                   are both set, JRE_HOME is used.
64#
65#   JAVA_OPTS       (Optional) Java runtime options used when any command
66#                   is executed.
67#                   Include here and not in CATALINA_OPTS all options, that
68#                   should be used by Tomcat and also by the stop process,
69#                   the version command etc.
70#                   Most options should go into CATALINA_OPTS.
71#
72#   JAVA_ENDORSED_DIRS (Optional) Lists of of colon separated directories
73#                   containing some jars in order to allow replacement of APIs
74#                   created outside of the JCP (i.e. DOM and SAX from W3C).
75#                   It can also be used to update the XML parser implementation.
76#                   This is only supported for Java <= 8.
77#                   Defaults to $CATALINA_HOME/endorsed.
78#
79#   JPDA_TRANSPORT  (Optional) JPDA transport used when the "jpda start"
80#                   command is executed. The default is "dt_socket".
81#
82#   JPDA_ADDRESS    (Optional) Java runtime options used when the "jpda start"
83#                   command is executed. The default is localhost:8000.
84#
85#   JPDA_SUSPEND    (Optional) Java runtime options used when the "jpda start"
86#                   command is executed. Specifies whether JVM should suspend
87#                   execution immediately after startup. Default is "n".
88#
89#   JPDA_OPTS       (Optional) Java runtime options used when the "jpda start"
90#                   command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS,
91#                   and JPDA_SUSPEND are ignored. Thus, all required jpda
92#                   options MUST be specified. The default is:
93#
94#                   -agentlib:jdwp=transport=$JPDA_TRANSPORT,
95#                       address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND
96#
97#   JSSE_OPTS       (Optional) Java runtime options used to control the TLS
98#                   implementation when JSSE is used. Default is:
99#                   "-Djdk.tls.ephemeralDHKeySize=2048"
100#
101#   CATALINA_PID    (Optional) Path of the file which should contains the pid
102#                   of the catalina startup java process, when start (fork) is
103#                   used
104#
105#   CATALINA_LOGGING_CONFIG (Optional) Override Tomcat's logging config file
106#                   Example (all one line)
107#                   CATALINA_LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
108#
109#   LOGGING_CONFIG  Deprecated
110#                   Use CATALINA_LOGGING_CONFIG
111#                   This is only used if CATALINA_LOGGING_CONFIG is not set
112#                   and LOGGING_CONFIG starts with "-D..."
113#
114#   LOGGING_MANAGER (Optional) Override Tomcat's logging manager
115#                   Example (all one line)
116#                   LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
117#
118#   UMASK           (Optional) Override Tomcat's default UMASK of 0027
119#
120#   USE_NOHUP       (Optional) If set to the string true the start command will
121#                   use nohup so that the Tomcat process will ignore any hangup
122#                   signals. Default is "false" unless running on HP-UX in which
123#                   case the default is "true"
124# -----------------------------------------------------------------------------
125
126# OS specific support.  $var _must_ be set to either true or false.
127cygwin=false
128darwin=false
129os400=false
130hpux=false
131case "`uname`" in
132CYGWIN*) cygwin=true;;
133Darwin*) darwin=true;;
134OS400*) os400=true;;
135HP-UX*) hpux=true;;
136esac
137
138# resolve links - $0 may be a softlink
139PRG="$0"
140
141while [ -h "$PRG" ]; do
142  ls=`ls -ld "$PRG"`
143  link=`expr "$ls" : '.*-> \(.*\)$'`
144  if expr "$link" : '/.*' > /dev/null; then
145    PRG="$link"
146  else
147    PRG=`dirname "$PRG"`/"$link"
148  fi
149done
150
151# Get standard environment variables
152PRGDIR=`dirname "$PRG"`
153
154# Only set CATALINA_HOME if not already set
155[ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`
156
157# Copy CATALINA_BASE from CATALINA_HOME if not already set
158[ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME"
159
160# Ensure that any user defined CLASSPATH variables are not used on startup,
161# but allow them to be specified in setenv.sh, in rare case when it is needed.
162CLASSPATH=
163
164if [ -r "$CATALINA_BASE/bin/setenv.sh" ]; then
165  . "$CATALINA_BASE/bin/setenv.sh"
166elif [ -r "$CATALINA_HOME/bin/setenv.sh" ]; then
167  . "$CATALINA_HOME/bin/setenv.sh"
168fi
169
170# For Cygwin, ensure paths are in UNIX format before anything is touched
171if $cygwin; then
172  [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
173  [ -n "$JRE_HOME" ] && JRE_HOME=`cygpath --unix "$JRE_HOME"`
174  [ -n "$CATALINA_HOME" ] && CATALINA_HOME=`cygpath --unix "$CATALINA_HOME"`
175  [ -n "$CATALINA_BASE" ] && CATALINA_BASE=`cygpath --unix "$CATALINA_BASE"`
176  [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
177fi
178
179# Ensure that neither CATALINA_HOME nor CATALINA_BASE contains a colon
180# as this is used as the separator in the classpath and Java provides no
181# mechanism for escaping if the same character appears in the path.
182case $CATALINA_HOME in
183  *:*) echo "Using CATALINA_HOME:   $CATALINA_HOME";
184       echo "Unable to start as CATALINA_HOME contains a colon (:) character";
185       exit 1;
186esac
187case $CATALINA_BASE in
188  *:*) echo "Using CATALINA_BASE:   $CATALINA_BASE";
189       echo "Unable to start as CATALINA_BASE contains a colon (:) character";
190       exit 1;
191esac
192
193# For OS400
194if $os400; then
195  # Set job priority to standard for interactive (interactive - 6) by using
196  # the interactive priority - 6, the helper threads that respond to requests
197  # will be running at the same priority as interactive jobs.
198  COMMAND='chgjob job('$JOBNAME') runpty(6)'
199  system $COMMAND
200
201  # Enable multi threading
202  export QIBM_MULTI_THREADED=Y
203fi
204
205# Get standard Java environment variables
206if $os400; then
207  # -r will Only work on the os400 if the files are:
208  # 1. owned by the user
209  # 2. owned by the PRIMARY group of the user
210  # this will not work if the user belongs in secondary groups
211  . "$CATALINA_HOME"/bin/setclasspath.sh
212else
213  if [ -r "$CATALINA_HOME"/bin/setclasspath.sh ]; then
214    . "$CATALINA_HOME"/bin/setclasspath.sh
215  else
216    echo "Cannot find $CATALINA_HOME/bin/setclasspath.sh"
217    echo "This file is needed to run this program"
218    exit 1
219  fi
220fi
221
222# Add on extra jar files to CLASSPATH
223if [ ! -z "$CLASSPATH" ] ; then
224  CLASSPATH="$CLASSPATH":
225fi
226CLASSPATH="$CLASSPATH""$CATALINA_HOME"/bin/bootstrap.jar
227
228if [ -z "$CATALINA_OUT" ] ; then
229  CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out
230fi
231
232if [ -z "$CATALINA_TMPDIR" ] ; then
233  # Define the java.io.tmpdir to use for Catalina
234  CATALINA_TMPDIR="$CATALINA_BASE"/temp
235fi
236
237# Add tomcat-juli.jar to classpath
238# tomcat-juli.jar can be over-ridden per instance
239if [ -r "$CATALINA_BASE/bin/tomcat-juli.jar" ] ; then
240  CLASSPATH=$CLASSPATH:$CATALINA_BASE/bin/tomcat-juli.jar
241else
242  CLASSPATH=$CLASSPATH:$CATALINA_HOME/bin/tomcat-juli.jar
243fi
244
245# Bugzilla 37848: When no TTY is available, don't output to console
246have_tty=0
247if [ -t 0 ]; then
248    have_tty=1
249fi
250
251# For Cygwin, switch paths to Windows format before running java
252if $cygwin; then
253  JAVA_HOME=`cygpath --absolute --windows "$JAVA_HOME"`
254  JRE_HOME=`cygpath --absolute --windows "$JRE_HOME"`
255  CATALINA_HOME=`cygpath --absolute --windows "$CATALINA_HOME"`
256  CATALINA_BASE=`cygpath --absolute --windows "$CATALINA_BASE"`
257  CATALINA_TMPDIR=`cygpath --absolute --windows "$CATALINA_TMPDIR"`
258  CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
259  [ -n "$JAVA_ENDORSED_DIRS" ] && JAVA_ENDORSED_DIRS=`cygpath --path --windows "$JAVA_ENDORSED_DIRS"`
260fi
261
262if [ -z "$JSSE_OPTS" ] ; then
263  JSSE_OPTS="-Djdk.tls.ephemeralDHKeySize=2048"
264fi
265JAVA_OPTS="$JAVA_OPTS $JSSE_OPTS"
266
267# Register custom URL handlers
268# Do this here so custom URL handles (specifically 'war:...') can be used in the security policy
269JAVA_OPTS="$JAVA_OPTS -Djava.protocol.handler.pkgs=org.apache.catalina.webresources"
270
271# Check for the deprecated LOGGING_CONFIG
272# Only use it if CATALINA_LOGGING_CONFIG is not set and LOGGING_CONFIG starts with "-D..."
273if [ -z "$CATALINA_LOGGING_CONFIG" ]; then
274  case $LOGGING_CONFIG in
275    -D*) CATALINA_LOGGING_CONFIG="$LOGGING_CONFIG"
276  esac
277fi
278
279# Set juli LogManager config file if it is present and an override has not been issued
280if [ -z "$CATALINA_LOGGING_CONFIG" ]; then
281  if [ -r "$CATALINA_BASE"/conf/logging.properties ]; then
282    CATALINA_LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
283  else
284    # Bugzilla 45585
285    CATALINA_LOGGING_CONFIG="-Dnop"
286  fi
287fi
288
289if [ -z "$LOGGING_MANAGER" ]; then
290  LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
291fi
292
293# Set UMASK unless it has been overridden
294if [ -z "$UMASK" ]; then
295    UMASK="0027"
296fi
297umask $UMASK
298
299# Java 9 no longer supports the java.endorsed.dirs
300# system property. Only try to use it if
301# JAVA_ENDORSED_DIRS was explicitly set
302# or CATALINA_HOME/endorsed exists.
303ENDORSED_PROP=ignore.endorsed.dirs
304if [ -n "$JAVA_ENDORSED_DIRS" ]; then
305    ENDORSED_PROP=java.endorsed.dirs
306fi
307if [ -d "$CATALINA_HOME/endorsed" ]; then
308    ENDORSED_PROP=java.endorsed.dirs
309fi
310
311# Make the umask available when using the org.apache.catalina.security.SecurityListener
312JAVA_OPTS="$JAVA_OPTS -Dorg.apache.catalina.security.SecurityListener.UMASK=`umask`"
313
314if [ -z "$USE_NOHUP" ]; then
315    if $hpux; then
316        USE_NOHUP="true"
317    else
318        USE_NOHUP="false"
319    fi
320fi
321unset _NOHUP
322if [ "$USE_NOHUP" = "true" ]; then
323    _NOHUP="nohup"
324fi
325
326# Add the JAVA 9 specific start-up parameters required by Tomcat
327JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.lang=ALL-UNNAMED"
328JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.io=ALL-UNNAMED"
329JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.util=ALL-UNNAMED"
330JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.util.concurrent=ALL-UNNAMED"
331JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED"
332export JDK_JAVA_OPTIONS
333
334# ----- Execute The Requested Command -----------------------------------------
335
336# Bugzilla 37848: only output this if we have a TTY
337if [ $have_tty -eq 1 ]; then
338  echo "Using CATALINA_BASE:   $CATALINA_BASE"
339  echo "Using CATALINA_HOME:   $CATALINA_HOME"
340  echo "Using CATALINA_TMPDIR: $CATALINA_TMPDIR"
341  if [ "$1" = "debug" ] ; then
342    echo "Using JAVA_HOME:       $JAVA_HOME"
343  else
344    echo "Using JRE_HOME:        $JRE_HOME"
345  fi
346  echo "Using CLASSPATH:       $CLASSPATH"
347  echo "Using CATALINA_OPTS:   $CATALINA_OPTS"
348  if [ ! -z "$CATALINA_PID" ]; then
349    echo "Using CATALINA_PID:    $CATALINA_PID"
350  fi
351fi
352
353if [ "$1" = "jpda" ] ; then
354  if [ -z "$JPDA_TRANSPORT" ]; then
355    JPDA_TRANSPORT="dt_socket"
356  fi
357  if [ -z "$JPDA_ADDRESS" ]; then
358    JPDA_ADDRESS="localhost:8000"
359  fi
360  if [ -z "$JPDA_SUSPEND" ]; then
361    JPDA_SUSPEND="n"
362  fi
363  if [ -z "$JPDA_OPTS" ]; then
364    JPDA_OPTS="-agentlib:jdwp=transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND"
365  fi
366  CATALINA_OPTS="$JPDA_OPTS $CATALINA_OPTS"
367  shift
368fi
369
370if [ "$1" = "debug" ] ; then
371  if $os400; then
372    echo "Debug command not available on OS400"
373    exit 1
374  else
375    shift
376    if [ "$1" = "-security" ] ; then
377      if [ $have_tty -eq 1 ]; then
378        echo "Using Security Manager"
379      fi
380      shift
381      eval exec "\"$_RUNJDB\"" "\"$CATALINA_LOGGING_CONFIG\"" $LOGGING_MANAGER "$JAVA_OPTS" "$CATALINA_OPTS" \
382        -D$ENDORSED_PROP="$JAVA_ENDORSED_DIRS" \
383        -classpath "$CLASSPATH" \
384        -sourcepath "$CATALINA_HOME"/../../java \
385        -Djava.security.manager \
386        -Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \
387        -Dcatalina.base="$CATALINA_BASE" \
388        -Dcatalina.home="$CATALINA_HOME" \
389        -Djava.io.tmpdir="$CATALINA_TMPDIR" \
390        org.apache.catalina.startup.Bootstrap "$@" start
391    else
392      eval exec "\"$_RUNJDB\"" "\"$CATALINA_LOGGING_CONFIG\"" $LOGGING_MANAGER "$JAVA_OPTS" "$CATALINA_OPTS" \
393        -D$ENDORSED_PROP="$JAVA_ENDORSED_DIRS" \
394        -classpath "$CLASSPATH" \
395        -sourcepath "$CATALINA_HOME"/../../java \
396        -Dcatalina.base="$CATALINA_BASE" \
397        -Dcatalina.home="$CATALINA_HOME" \
398        -Djava.io.tmpdir="$CATALINA_TMPDIR" \
399        org.apache.catalina.startup.Bootstrap "$@" start
400    fi
401  fi
402
403elif [ "$1" = "run" ]; then
404
405  shift
406  if [ "$1" = "-security" ] ; then
407    if [ $have_tty -eq 1 ]; then
408      echo "Using Security Manager"
409    fi
410    shift
411    eval exec "\"$_RUNJAVA\"" "\"$CATALINA_LOGGING_CONFIG\"" $LOGGING_MANAGER "$JAVA_OPTS" "$CATALINA_OPTS" \
412      -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \
413      -classpath "\"$CLASSPATH\"" \
414      -Djava.security.manager \
415      -Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \
416      -Dcatalina.base="\"$CATALINA_BASE\"" \
417      -Dcatalina.home="\"$CATALINA_HOME\"" \
418      -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
419      org.apache.catalina.startup.Bootstrap "$@" start
420  else
421    eval exec "\"$_RUNJAVA\"" "\"$CATALINA_LOGGING_CONFIG\"" $LOGGING_MANAGER "$JAVA_OPTS" "$CATALINA_OPTS" \
422      -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \
423      -classpath "\"$CLASSPATH\"" \
424      -Dcatalina.base="\"$CATALINA_BASE\"" \
425      -Dcatalina.home="\"$CATALINA_HOME\"" \
426      -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
427      org.apache.catalina.startup.Bootstrap "$@" start
428  fi
429
430elif [ "$1" = "start" ] ; then
431
432  if [ ! -z "$CATALINA_PID" ]; then
433    if [ -f "$CATALINA_PID" ]; then
434      if [ -s "$CATALINA_PID" ]; then
435        echo "Existing PID file found during start."
436        if [ -r "$CATALINA_PID" ]; then
437          PID=`cat "$CATALINA_PID"`
438          ps -p $PID >/dev/null 2>&1
439          if [ $? -eq 0 ] ; then
440            echo "Tomcat appears to still be running with PID $PID. Start aborted."
441            echo "If the following process is not a Tomcat process, remove the PID file and try again:"
442            ps -f -p $PID
443            exit 1
444          else
445            echo "Removing/clearing stale PID file."
446            rm -f "$CATALINA_PID" >/dev/null 2>&1
447            if [ $? != 0 ]; then
448              if [ -w "$CATALINA_PID" ]; then
449                cat /dev/null > "$CATALINA_PID"
450              else
451                echo "Unable to remove or clear stale PID file. Start aborted."
452                exit 1
453              fi
454            fi
455          fi
456        else
457          echo "Unable to read PID file. Start aborted."
458          exit 1
459        fi
460      else
461        rm -f "$CATALINA_PID" >/dev/null 2>&1
462        if [ $? != 0 ]; then
463          if [ ! -w "$CATALINA_PID" ]; then
464            echo "Unable to remove or write to empty PID file. Start aborted."
465            exit 1
466          fi
467        fi
468      fi
469    fi
470  fi
471
472  shift
473  if [ -z "$CATALINA_OUT_CMD" ] ; then
474    touch "$CATALINA_OUT"
475  else
476    if [ ! -e "$CATALINA_OUT" ]; then
477      if ! mkfifo "$CATALINA_OUT"; then
478        echo "cannot create named pipe $CATALINA_OUT. Start aborted."
479        exit 1
480      fi
481    elif [ ! -p "$CATALINA_OUT" ]; then
482      echo "$CATALINA_OUT exists and is not a named pipe. Start aborted."
483      exit 1
484    fi
485    $CATALINA_OUT_CMD <"$CATALINA_OUT" &
486  fi
487  if [ "$1" = "-security" ] ; then
488    if [ $have_tty -eq 1 ]; then
489      echo "Using Security Manager"
490    fi
491    shift
492    eval $_NOHUP "\"$_RUNJAVA\"" "\"$CATALINA_LOGGING_CONFIG\"" $LOGGING_MANAGER "$JAVA_OPTS" "$CATALINA_OPTS" \
493      -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \
494      -classpath "\"$CLASSPATH\"" \
495      -Djava.security.manager \
496      -Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \
497      -Dcatalina.base="\"$CATALINA_BASE\"" \
498      -Dcatalina.home="\"$CATALINA_HOME\"" \
499      -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
500      org.apache.catalina.startup.Bootstrap "$@" start \
501      >> "$CATALINA_OUT" 2>&1 "&"
502
503  else
504    eval $_NOHUP "\"$_RUNJAVA\"" "\"$CATALINA_LOGGING_CONFIG\"" $LOGGING_MANAGER "$JAVA_OPTS" "$CATALINA_OPTS" \
505      -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \
506      -classpath "\"$CLASSPATH\"" \
507      -Dcatalina.base="\"$CATALINA_BASE\"" \
508      -Dcatalina.home="\"$CATALINA_HOME\"" \
509      -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
510      org.apache.catalina.startup.Bootstrap "$@" start \
511      >> "$CATALINA_OUT" 2>&1 "&"
512
513  fi
514
515  if [ ! -z "$CATALINA_PID" ]; then
516    echo $! > "$CATALINA_PID"
517  fi
518
519  echo "Tomcat started."
520
521elif [ "$1" = "stop" ] ; then
522
523  shift
524
525  SLEEP=5
526  if [ ! -z "$1" ]; then
527    echo $1 | grep "[^0-9]" >/dev/null 2>&1
528    if [ $? -gt 0 ]; then
529      SLEEP=$1
530      shift
531    fi
532  fi
533
534  FORCE=0
535  if [ "$1" = "-force" ]; then
536    shift
537    FORCE=1
538  fi
539
540  if [ ! -z "$CATALINA_PID" ]; then
541    if [ -f "$CATALINA_PID" ]; then
542      if [ -s "$CATALINA_PID" ]; then
543        kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1
544        if [ $? -gt 0 ]; then
545          echo "PID file found but either no matching process was found or the current user does not have permission to stop the process. Stop aborted."
546          exit 1
547        fi
548      else
549        echo "PID file is empty and has been ignored."
550      fi
551    else
552      echo "\$CATALINA_PID was set but the specified file does not exist. Is Tomcat running? Stop aborted."
553      exit 1
554    fi
555  fi
556
557  eval "\"$_RUNJAVA\"" $LOGGING_MANAGER "$JAVA_OPTS" \
558    -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \
559    -classpath "\"$CLASSPATH\"" \
560    -Dcatalina.base="\"$CATALINA_BASE\"" \
561    -Dcatalina.home="\"$CATALINA_HOME\"" \
562    -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
563    org.apache.catalina.startup.Bootstrap "$@" stop
564
565  # stop failed. Shutdown port disabled? Try a normal kill.
566  if [ $? != 0 ]; then
567    if [ ! -z "$CATALINA_PID" ]; then
568      echo "The stop command failed. Attempting to signal the process to stop through OS signal."
569      kill -15 `cat "$CATALINA_PID"` >/dev/null 2>&1
570    fi
571  fi
572
573  if [ ! -z "$CATALINA_PID" ]; then
574    if [ -f "$CATALINA_PID" ]; then
575      while [ $SLEEP -ge 0 ]; do
576        kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1
577        if [ $? -gt 0 ]; then
578          rm -f "$CATALINA_PID" >/dev/null 2>&1
579          if [ $? != 0 ]; then
580            if [ -w "$CATALINA_PID" ]; then
581              cat /dev/null > "$CATALINA_PID"
582              # If Tomcat has stopped don't try and force a stop with an empty PID file
583              FORCE=0
584            else
585              echo "The PID file could not be removed or cleared."
586            fi
587          fi
588          echo "Tomcat stopped."
589          break
590        fi
591        if [ $SLEEP -gt 0 ]; then
592          sleep 1
593        fi
594        if [ $SLEEP -eq 0 ]; then
595          echo "Tomcat did not stop in time."
596          if [ $FORCE -eq 0 ]; then
597            echo "PID file was not removed."
598          fi
599          echo "To aid diagnostics a thread dump has been written to standard out."
600          kill -3 `cat "$CATALINA_PID"`
601        fi
602        SLEEP=`expr $SLEEP - 1 `
603      done
604    fi
605  fi
606
607  KILL_SLEEP_INTERVAL=5
608  if [ $FORCE -eq 1 ]; then
609    if [ -z "$CATALINA_PID" ]; then
610      echo "Kill failed: \$CATALINA_PID not set"
611    else
612      if [ -f "$CATALINA_PID" ]; then
613        PID=`cat "$CATALINA_PID"`
614        echo "Killing Tomcat with the PID: $PID"
615        kill -9 $PID
616        while [ $KILL_SLEEP_INTERVAL -ge 0 ]; do
617            kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1
618            if [ $? -gt 0 ]; then
619                rm -f "$CATALINA_PID" >/dev/null 2>&1
620                if [ $? != 0 ]; then
621                    if [ -w "$CATALINA_PID" ]; then
622                        cat /dev/null > "$CATALINA_PID"
623                    else
624                        echo "The PID file could not be removed."
625                    fi
626                fi
627                echo "The Tomcat process has been killed."
628                break
629            fi
630            if [ $KILL_SLEEP_INTERVAL -gt 0 ]; then
631                sleep 1
632            fi
633            KILL_SLEEP_INTERVAL=`expr $KILL_SLEEP_INTERVAL - 1 `
634        done
635        if [ $KILL_SLEEP_INTERVAL -lt 0 ]; then
636            echo "Tomcat has not been killed completely yet. The process might be waiting on some system call or might be UNINTERRUPTIBLE."
637        fi
638      fi
639    fi
640  fi
641
642elif [ "$1" = "configtest" ] ; then
643
644    eval "\"$_RUNJAVA\"" $LOGGING_MANAGER "$JAVA_OPTS" \
645      -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \
646      -classpath "\"$CLASSPATH\"" \
647      -Dcatalina.base="\"$CATALINA_BASE\"" \
648      -Dcatalina.home="\"$CATALINA_HOME\"" \
649      -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
650      org.apache.catalina.startup.Bootstrap configtest
651    result=$?
652    if [ $result -ne 0 ]; then
653        echo "Configuration error detected!"
654    fi
655    exit $result
656
657elif [ "$1" = "version" ] ; then
658
659    "$_RUNJAVA"   \
660      -classpath "$CATALINA_HOME/lib/catalina.jar" \
661      org.apache.catalina.util.ServerInfo
662
663else
664
665  echo "Usage: catalina.sh ( commands ... )"
666  echo "commands:"
667  if $os400; then
668    echo "  debug             Start Catalina in a debugger (not available on OS400)"
669    echo "  debug -security   Debug Catalina with a security manager (not available on OS400)"
670  else
671    echo "  debug             Start Catalina in a debugger"
672    echo "  debug -security   Debug Catalina with a security manager"
673  fi
674  echo "  jpda start        Start Catalina under JPDA debugger"
675  echo "  run               Start Catalina in the current window"
676  echo "  run -security     Start in the current window with security manager"
677  echo "  start             Start Catalina in a separate window"
678  echo "  start -security   Start in a separate window with security manager"
679  echo "  stop              Stop Catalina, waiting up to 5 seconds for the process to end"
680  echo "  stop n            Stop Catalina, waiting up to n seconds for the process to end"
681  echo "  stop -force       Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running"
682  echo "  stop n -force     Stop Catalina, wait up to n seconds and then use kill -KILL if still running"
683  echo "  configtest        Run a basic syntax check on server.xml - check exit code for result"
684  echo "  version           What version of tomcat are you running?"
685  echo "Note: Waiting for the process to end and use of the -force option require that \$CATALINA_PID is defined"
686  exit 1
687
688fi
689