blob: c8d5493888b7080b6f5fca5b4534d6b06c584412 [file] [log] [blame]
Cheng Tang697ce242014-04-27 16:18:17 +08001#!/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# Environment Variable Prerequisites
22#
23# Do not set the variables in this script. Instead put them into a script
24# setenv.sh in CATALINA_BASE/bin to keep your customizations separate.
25#
26# CATALINA_HOME May point at your Catalina "build" directory.
27#
28# CATALINA_BASE (Optional) Base directory for resolving dynamic portions
29# of a Catalina installation. If not present, resolves to
30# the same directory that CATALINA_HOME points to.
31#
32# CATALINA_OUT (Optional) Full path to a file where stdout and stderr
33# will be redirected.
34# Default is $CATALINA_BASE/logs/catalina.out
35#
36# CATALINA_OPTS (Optional) Java runtime options used when the "start",
37# "run" or "debug" command is executed.
38# Include here and not in JAVA_OPTS all options, that should
39# only be used by Tomcat itself, not by the stop process,
40# the version command etc.
41# Examples are heap size, GC logging, JMX ports etc.
42#
43# CATALINA_TMPDIR (Optional) Directory path location of temporary directory
44# the JVM should use (java.io.tmpdir). Defaults to
45# $CATALINA_BASE/temp.
46#
47# JAVA_HOME Must point at your Java Development Kit installation.
48# Required to run the with the "debug" argument.
49#
50# JRE_HOME Must point at your Java Runtime installation.
51# Defaults to JAVA_HOME if empty. If JRE_HOME and JAVA_HOME
52# are both set, JRE_HOME is used.
53#
54# JAVA_OPTS (Optional) Java runtime options used when any command
55# is executed.
56# Include here and not in CATALINA_OPTS all options, that
57# should be used by Tomcat and also by the stop process,
58# the version command etc.
59# Most options should go into CATALINA_OPTS.
60#
61# JAVA_ENDORSED_DIRS (Optional) Lists of of colon separated directories
62# containing some jars in order to allow replacement of APIs
63# created outside of the JCP (i.e. DOM and SAX from W3C).
64# It can also be used to update the XML parser implementation.
65# Defaults to $CATALINA_HOME/endorsed.
66#
67# JPDA_TRANSPORT (Optional) JPDA transport used when the "jpda start"
68# command is executed. The default is "dt_socket".
69#
70# JPDA_ADDRESS (Optional) Java runtime options used when the "jpda start"
71# command is executed. The default is 8000.
72#
73# JPDA_SUSPEND (Optional) Java runtime options used when the "jpda start"
74# command is executed. Specifies whether JVM should suspend
75# execution immediately after startup. Default is "n".
76#
77# JPDA_OPTS (Optional) Java runtime options used when the "jpda start"
78# command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS,
79# and JPDA_SUSPEND are ignored. Thus, all required jpda
80# options MUST be specified. The default is:
81#
82# -agentlib:jdwp=transport=$JPDA_TRANSPORT,
83# address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND
84#
85# CATALINA_PID (Optional) Path of the file which should contains the pid
86# of the catalina startup java process, when start (fork) is
87# used
88#
89# LOGGING_CONFIG (Optional) Override Tomcat's logging config file
90# Example (all one line)
91# LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
92#
93# LOGGING_MANAGER (Optional) Override Tomcat's logging manager
94# Example (all one line)
95# LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
96#
97# $Id: catalina.sh 1498485 2013-07-01 14:37:43Z markt $
98# -----------------------------------------------------------------------------
99
100# OS specific support. $var _must_ be set to either true or false.
101cygwin=false
102darwin=false
103os400=false
104case "`uname`" in
105CYGWIN*) cygwin=true;;
106Darwin*) darwin=true;;
107OS400*) os400=true;;
108esac
109
110# resolve links - $0 may be a softlink
111PRG="$0"
112
113while [ -h "$PRG" ]; do
114 ls=`ls -ld "$PRG"`
115 link=`expr "$ls" : '.*-> \(.*\)$'`
116 if expr "$link" : '/.*' > /dev/null; then
117 PRG="$link"
118 else
119 PRG=`dirname "$PRG"`/"$link"
120 fi
121done
122
123# Get standard environment variables
124PRGDIR=`dirname "$PRG"`
125
126# Only set CATALINA_HOME if not already set
127[ -z "$CATALINA_HOME" ] && CATALINA_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`
128
129# Copy CATALINA_BASE from CATALINA_HOME if not already set
130[ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME"
131
132# Ensure that any user defined CLASSPATH variables are not used on startup,
133# but allow them to be specified in setenv.sh, in rare case when it is needed.
134CLASSPATH=
135
136if [ -r "$CATALINA_BASE/bin/setenv.sh" ]; then
137 . "$CATALINA_BASE/bin/setenv.sh"
138elif [ -r "$CATALINA_HOME/bin/setenv.sh" ]; then
139 . "$CATALINA_HOME/bin/setenv.sh"
140fi
141
142# For Cygwin, ensure paths are in UNIX format before anything is touched
143if $cygwin; then
144 [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
145 [ -n "$JRE_HOME" ] && JRE_HOME=`cygpath --unix "$JRE_HOME"`
146 [ -n "$CATALINA_HOME" ] && CATALINA_HOME=`cygpath --unix "$CATALINA_HOME"`
147 [ -n "$CATALINA_BASE" ] && CATALINA_BASE=`cygpath --unix "$CATALINA_BASE"`
148 [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
149fi
150
151# For OS400
152if $os400; then
153 # Set job priority to standard for interactive (interactive - 6) by using
154 # the interactive priority - 6, the helper threads that respond to requests
155 # will be running at the same priority as interactive jobs.
156 COMMAND='chgjob job('$JOBNAME') runpty(6)'
157 system $COMMAND
158
159 # Enable multi threading
160 export QIBM_MULTI_THREADED=Y
161fi
162
163# Get standard Java environment variables
164if $os400; then
165 # -r will Only work on the os400 if the files are:
166 # 1. owned by the user
167 # 2. owned by the PRIMARY group of the user
168 # this will not work if the user belongs in secondary groups
169 . "$CATALINA_HOME"/bin/setclasspath.sh
170else
171 if [ -r "$CATALINA_HOME"/bin/setclasspath.sh ]; then
172 . "$CATALINA_HOME"/bin/setclasspath.sh
173 else
174 echo "Cannot find $CATALINA_HOME/bin/setclasspath.sh"
175 echo "This file is needed to run this program"
176 exit 1
177 fi
178fi
179
180# Add on extra jar files to CLASSPATH
181if [ ! -z "$CLASSPATH" ] ; then
182 CLASSPATH="$CLASSPATH":
183fi
184CLASSPATH="$CLASSPATH""$CATALINA_HOME"/bin/bootstrap.jar
185
186if [ -z "$CATALINA_OUT" ] ; then
187 CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out
188fi
189
190if [ -z "$CATALINA_TMPDIR" ] ; then
191 # Define the java.io.tmpdir to use for Catalina
192 CATALINA_TMPDIR="$CATALINA_BASE"/temp
193fi
194
195# Add tomcat-juli.jar to classpath
196# tomcat-juli.jar can be over-ridden per instance
197if [ -r "$CATALINA_BASE/bin/tomcat-juli.jar" ] ; then
198 CLASSPATH=$CLASSPATH:$CATALINA_BASE/bin/tomcat-juli.jar
199else
200 CLASSPATH=$CLASSPATH:$CATALINA_HOME/bin/tomcat-juli.jar
201fi
202
203# Bugzilla 37848: When no TTY is available, don't output to console
204have_tty=0
205if [ "`tty`" != "not a tty" ]; then
206 have_tty=1
207fi
208
209# For Cygwin, switch paths to Windows format before running java
210if $cygwin; then
211 JAVA_HOME=`cygpath --absolute --windows "$JAVA_HOME"`
212 JRE_HOME=`cygpath --absolute --windows "$JRE_HOME"`
213 CATALINA_HOME=`cygpath --absolute --windows "$CATALINA_HOME"`
214 CATALINA_BASE=`cygpath --absolute --windows "$CATALINA_BASE"`
215 CATALINA_TMPDIR=`cygpath --absolute --windows "$CATALINA_TMPDIR"`
216 CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
217 JAVA_ENDORSED_DIRS=`cygpath --path --windows "$JAVA_ENDORSED_DIRS"`
218fi
219
220# Set juli LogManager config file if it is present and an override has not been issued
221if [ -z "$LOGGING_CONFIG" ]; then
222 if [ -r "$CATALINA_BASE"/conf/logging.properties ]; then
223 LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
224 else
225 # Bugzilla 45585
226 LOGGING_CONFIG="-Dnop"
227 fi
228fi
229
230if [ -z "$LOGGING_MANAGER" ]; then
231 LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
232fi
233
234# Uncomment the following line to make the umask available when using the
235# org.apache.catalina.security.SecurityListener
236#JAVA_OPTS="$JAVA_OPTS -Dorg.apache.catalina.security.SecurityListener.UMASK=`umask`"
237
238# ----- Execute The Requested Command -----------------------------------------
239
240# Bugzilla 37848: only output this if we have a TTY
241if [ $have_tty -eq 1 ]; then
242 echo "Using CATALINA_BASE: $CATALINA_BASE"
243 echo "Using CATALINA_HOME: $CATALINA_HOME"
244 echo "Using CATALINA_TMPDIR: $CATALINA_TMPDIR"
245 if [ "$1" = "debug" ] ; then
246 echo "Using JAVA_HOME: $JAVA_HOME"
247 else
248 echo "Using JRE_HOME: $JRE_HOME"
249 fi
250 echo "Using CLASSPATH: $CLASSPATH"
251 if [ ! -z "$CATALINA_PID" ]; then
252 echo "Using CATALINA_PID: $CATALINA_PID"
253 fi
254fi
255
256if [ "$1" = "jpda" ] ; then
257 if [ -z "$JPDA_TRANSPORT" ]; then
258 JPDA_TRANSPORT="dt_socket"
259 fi
260 if [ -z "$JPDA_ADDRESS" ]; then
261 JPDA_ADDRESS="8000"
262 fi
263 if [ -z "$JPDA_SUSPEND" ]; then
264 JPDA_SUSPEND="n"
265 fi
266 if [ -z "$JPDA_OPTS" ]; then
267 JPDA_OPTS="-agentlib:jdwp=transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=$JPDA_SUSPEND"
268 fi
269 CATALINA_OPTS="$CATALINA_OPTS $JPDA_OPTS"
270 shift
271fi
272
273if [ "$1" = "debug" ] ; then
274 if $os400; then
275 echo "Debug command not available on OS400"
276 exit 1
277 else
278 shift
279 if [ "$1" = "-security" ] ; then
280 if [ $have_tty -eq 1 ]; then
281 echo "Using Security Manager"
282 fi
283 shift
284 exec "$_RUNJDB" "$LOGGING_CONFIG" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
285 -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
286 -sourcepath "$CATALINA_HOME"/../../java \
287 -Djava.security.manager \
288 -Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \
289 -Dcatalina.base="$CATALINA_BASE" \
290 -Dcatalina.home="$CATALINA_HOME" \
291 -Djava.io.tmpdir="$CATALINA_TMPDIR" \
292 org.apache.catalina.startup.Bootstrap "$@" start
293 else
294 exec "$_RUNJDB" "$LOGGING_CONFIG" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
295 -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
296 -sourcepath "$CATALINA_HOME"/../../java \
297 -Dcatalina.base="$CATALINA_BASE" \
298 -Dcatalina.home="$CATALINA_HOME" \
299 -Djava.io.tmpdir="$CATALINA_TMPDIR" \
300 org.apache.catalina.startup.Bootstrap "$@" start
301 fi
302 fi
303
304elif [ "$1" = "run" ]; then
305
306 shift
307 if [ "$1" = "-security" ] ; then
308 if [ $have_tty -eq 1 ]; then
309 echo "Using Security Manager"
310 fi
311 shift
312 eval exec \"$_RUNJAVA\" \"$LOGGING_CONFIG\" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
313 -Djava.endorsed.dirs=\"$JAVA_ENDORSED_DIRS\" -classpath \"$CLASSPATH\" \
314 -Djava.security.manager \
315 -Djava.security.policy==\"$CATALINA_BASE/conf/catalina.policy\" \
316 -Dcatalina.base=\"$CATALINA_BASE\" \
317 -Dcatalina.home=\"$CATALINA_HOME\" \
318 -Djava.io.tmpdir=\"$CATALINA_TMPDIR\" \
319 org.apache.catalina.startup.Bootstrap "$@" start
320 else
321 eval exec \"$_RUNJAVA\" \"$LOGGING_CONFIG\" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
322 -Djava.endorsed.dirs=\"$JAVA_ENDORSED_DIRS\" -classpath \"$CLASSPATH\" \
323 -Dcatalina.base=\"$CATALINA_BASE\" \
324 -Dcatalina.home=\"$CATALINA_HOME\" \
325 -Djava.io.tmpdir=\"$CATALINA_TMPDIR\" \
326 org.apache.catalina.startup.Bootstrap "$@" start
327 fi
328
329elif [ "$1" = "start" ] ; then
330
331 if [ ! -z "$CATALINA_PID" ]; then
332 if [ -f "$CATALINA_PID" ]; then
333 if [ -s "$CATALINA_PID" ]; then
334 echo "Existing PID file found during start."
335 if [ -r "$CATALINA_PID" ]; then
336 PID=`cat "$CATALINA_PID"`
337 ps -p $PID >/dev/null 2>&1
338 if [ $? -eq 0 ] ; then
339 echo "Tomcat appears to still be running with PID $PID. Start aborted."
340 exit 1
341 else
342 echo "Removing/clearing stale PID file."
343 rm -f "$CATALINA_PID" >/dev/null 2>&1
344 if [ $? != 0 ]; then
345 if [ -w "$CATALINA_PID" ]; then
346 cat /dev/null > "$CATALINA_PID"
347 else
348 echo "Unable to remove or clear stale PID file. Start aborted."
349 exit 1
350 fi
351 fi
352 fi
353 else
354 echo "Unable to read PID file. Start aborted."
355 exit 1
356 fi
357 else
358 rm -f "$CATALINA_PID" >/dev/null 2>&1
359 if [ $? != 0 ]; then
360 if [ ! -w "$CATALINA_PID" ]; then
361 echo "Unable to remove or write to empty PID file. Start aborted."
362 exit 1
363 fi
364 fi
365 fi
366 fi
367 fi
368
369 shift
370 touch "$CATALINA_OUT"
371 if [ "$1" = "-security" ] ; then
372 if [ $have_tty -eq 1 ]; then
373 echo "Using Security Manager"
374 fi
375 shift
376 eval \"$_RUNJAVA\" \"$LOGGING_CONFIG\" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
377 -Djava.endorsed.dirs=\"$JAVA_ENDORSED_DIRS\" -classpath \"$CLASSPATH\" \
378 -Djava.security.manager \
379 -Djava.security.policy==\"$CATALINA_BASE/conf/catalina.policy\" \
380 -Dcatalina.base=\"$CATALINA_BASE\" \
381 -Dcatalina.home=\"$CATALINA_HOME\" \
382 -Djava.io.tmpdir=\"$CATALINA_TMPDIR\" \
383 org.apache.catalina.startup.Bootstrap "$@" start \
384 >> "$CATALINA_OUT" 2>&1 "&"
385
386 else
387 eval \"$_RUNJAVA\" \"$LOGGING_CONFIG\" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
388 -Djava.endorsed.dirs=\"$JAVA_ENDORSED_DIRS\" -classpath \"$CLASSPATH\" \
389 -Dcatalina.base=\"$CATALINA_BASE\" \
390 -Dcatalina.home=\"$CATALINA_HOME\" \
391 -Djava.io.tmpdir=\"$CATALINA_TMPDIR\" \
392 org.apache.catalina.startup.Bootstrap "$@" start \
393 >> "$CATALINA_OUT" 2>&1 "&"
394
395 fi
396
397 if [ ! -z "$CATALINA_PID" ]; then
398 echo $! > "$CATALINA_PID"
399 fi
400
401elif [ "$1" = "stop" ] ; then
402
403 shift
404
405 SLEEP=5
406 if [ ! -z "$1" ]; then
407 echo $1 | grep "[^0-9]" >/dev/null 2>&1
408 if [ $? -gt 0 ]; then
409 SLEEP=$1
410 shift
411 fi
412 fi
413
414 FORCE=0
415 if [ "$1" = "-force" ]; then
416 shift
417 FORCE=1
418 fi
419
420 if [ ! -z "$CATALINA_PID" ]; then
421 if [ -f "$CATALINA_PID" ]; then
422 if [ -s "$CATALINA_PID" ]; then
423 kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1
424 if [ $? -gt 0 ]; then
425 echo "PID file found but no matching process was found. Stop aborted."
426 exit 1
427 fi
428 else
429 echo "PID file is empty and has been ignored."
430 fi
431 else
432 echo "\$CATALINA_PID was set but the specified file does not exist. Is Tomcat running? Stop aborted."
433 exit 1
434 fi
435 fi
436
437 eval \"$_RUNJAVA\" $LOGGING_MANAGER $JAVA_OPTS \
438 -Djava.endorsed.dirs=\"$JAVA_ENDORSED_DIRS\" -classpath \"$CLASSPATH\" \
439 -Dcatalina.base=\"$CATALINA_BASE\" \
440 -Dcatalina.home=\"$CATALINA_HOME\" \
441 -Djava.io.tmpdir=\"$CATALINA_TMPDIR\" \
442 org.apache.catalina.startup.Bootstrap "$@" stop
443
444 if [ ! -z "$CATALINA_PID" ]; then
445 if [ -f "$CATALINA_PID" ]; then
446 while [ $SLEEP -ge 0 ]; do
447 kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1
448 if [ $? -gt 0 ]; then
449 rm -f "$CATALINA_PID" >/dev/null 2>&1
450 if [ $? != 0 ]; then
451 if [ -w "$CATALINA_PID" ]; then
452 cat /dev/null > "$CATALINA_PID"
453 else
454 echo "Tomcat stopped but the PID file could not be removed or cleared."
455 fi
456 fi
457 break
458 fi
459 if [ $SLEEP -gt 0 ]; then
460 sleep 1
461 fi
462 if [ $SLEEP -eq 0 ]; then
463 if [ $FORCE -eq 0 ]; then
464 echo "Tomcat did not stop in time. PID file was not removed."
465 fi
466 fi
467 SLEEP=`expr $SLEEP - 1 `
468 done
469 fi
470 fi
471
472 KILL_SLEEP_INTERVAL=5
473 if [ $FORCE -eq 1 ]; then
474 if [ -z "$CATALINA_PID" ]; then
475 echo "Kill failed: \$CATALINA_PID not set"
476 else
477 if [ -f "$CATALINA_PID" ]; then
478 PID=`cat "$CATALINA_PID"`
479 echo "Killing Tomcat with the PID: $PID"
480 kill -9 $PID
481 while [ $KILL_SLEEP_INTERVAL -ge 0 ]; do
482 kill -0 `cat "$CATALINA_PID"` >/dev/null 2>&1
483 if [ $? -gt 0 ]; then
484 rm -f "$CATALINA_PID" >/dev/null 2>&1
485 if [ $? != 0 ]; then
486 echo "Tomcat was killed but the PID file could not be removed."
487 fi
488 break
489 fi
490 if [ $KILL_SLEEP_INTERVAL -gt 0 ]; then
491 sleep 1
492 fi
493 KILL_SLEEP_INTERVAL=`expr $KILL_SLEEP_INTERVAL - 1 `
494 done
495 if [ $KILL_SLEEP_INTERVAL -gt 0 ]; then
496 echo "Tomcat has not been killed completely yet. The process might be waiting on some system call or might be UNINTERRUPTIBLE."
497 fi
498 fi
499 fi
500 fi
501
502elif [ "$1" = "configtest" ] ; then
503
504 eval \"$_RUNJAVA\" $LOGGING_MANAGER $JAVA_OPTS \
505 -Djava.endorsed.dirs=\"$JAVA_ENDORSED_DIRS\" -classpath \"$CLASSPATH\" \
506 -Dcatalina.base=\"$CATALINA_BASE\" \
507 -Dcatalina.home=\"$CATALINA_HOME\" \
508 -Djava.io.tmpdir=\"$CATALINA_TMPDIR\" \
509 org.apache.catalina.startup.Bootstrap configtest
510 result=$?
511 if [ $result -ne 0 ]; then
512 echo "Configuration error detected!"
513 fi
514 exit $result
515
516elif [ "$1" = "version" ] ; then
517
518 "$_RUNJAVA" \
519 -classpath "$CATALINA_HOME/lib/catalina.jar" \
520 org.apache.catalina.util.ServerInfo
521
522else
523
524 echo "Usage: catalina.sh ( commands ... )"
525 echo "commands:"
526 if $os400; then
527 echo " debug Start Catalina in a debugger (not available on OS400)"
528 echo " debug -security Debug Catalina with a security manager (not available on OS400)"
529 else
530 echo " debug Start Catalina in a debugger"
531 echo " debug -security Debug Catalina with a security manager"
532 fi
533 echo " jpda start Start Catalina under JPDA debugger"
534 echo " run Start Catalina in the current window"
535 echo " run -security Start in the current window with security manager"
536 echo " start Start Catalina in a separate window"
537 echo " start -security Start in a separate window with security manager"
538 echo " stop Stop Catalina, waiting up to 5 seconds for the process to end"
539 echo " stop n Stop Catalina, waiting up to n seconds for the process to end"
540 echo " stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running"
541 echo " stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running"
542 echo " configtest Run a basic syntax check on server.xml - check exit code for result"
543 echo " version What version of tomcat are you running?"
544 echo "Note: Waiting for the process to end and use of the -force option require that \$CATALINA_PID is defined"
545 exit 1
546
547fi