blob: 9a4640dcc0a5ada864620f817d1aa1ca6460bc07 [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# Commons Daemon wrapper script.
20#
21# $Id: daemon.sh 1202058 2011-11-15 06:37:12Z mturk $
22# -----------------------------------------------------------------------------
23#
24# resolve links - $0 may be a softlink
25ARG0="$0"
26while [ -h "$ARG0" ]; do
27 ls=`ls -ld "$ARG0"`
28 link=`expr "$ls" : '.*-> \(.*\)$'`
29 if expr "$link" : '/.*' > /dev/null; then
30 ARG0="$link"
31 else
32 ARG0="`dirname $ARG0`/$link"
33 fi
34done
35DIRNAME="`dirname $ARG0`"
36PROGRAM="`basename $ARG0`"
37while [ ".$1" != . ]
38do
39 case "$1" in
40 --java-home )
41 JAVA_HOME="$2"
42 shift; shift;
43 continue
44 ;;
45 --catalina-home )
46 CATALINA_HOME="$2"
47 shift; shift;
48 continue
49 ;;
50 --catalina-base )
51 CATALINA_BASE="$2"
52 shift; shift;
53 continue
54 ;;
55 --catalina-pid )
56 CATALINA_PID="$2"
57 shift; shift;
58 continue
59 ;;
60 --tomcat-user )
61 TOMCAT_USER="$2"
62 shift; shift;
63 continue
64 ;;
65 * )
66 break
67 ;;
68 esac
69done
70# OS specific support (must be 'true' or 'false').
71cygwin=false;
72darwin=false;
73case "`uname`" in
74 CYGWIN*)
75 cygwin=true
76 ;;
77 Darwin*)
78 darwin=true
79 ;;
80esac
81
82# Use the maximum available, or set MAX_FD != -1 to use that
83test ".$MAX_FD" = . && MAX_FD="maximum"
84# Setup parameters for running the jsvc
85#
86test ".$TOMCAT_USER" = . && TOMCAT_USER=tomcat
87# Set JAVA_HOME to working JDK or JRE
88# JAVA_HOME=/opt/jdk-1.6.0.22
89# If not set we'll try to guess the JAVA_HOME
90# from java binary if on the PATH
91#
92if [ -z "$JAVA_HOME" ]; then
93 JAVA_BIN="`which java 2>/dev/null || type java 2>&1`"
94 test -x "$JAVA_BIN" && JAVA_HOME="`dirname $JAVA_BIN`"
95 test ".$JAVA_HOME" != . && JAVA_HOME=`cd "$JAVA_HOME/.." >/dev/null; pwd`
96else
97 JAVA_BIN="$JAVA_HOME/bin/java"
98fi
99
100# Only set CATALINA_HOME if not already set
101test ".$CATALINA_HOME" = . && CATALINA_HOME=`cd "$DIRNAME/.." >/dev/null; pwd`
102test ".$CATALINA_BASE" = . && CATALINA_BASE="$CATALINA_HOME"
103test ".$CATALINA_MAIN" = . && CATALINA_MAIN=org.apache.catalina.startup.Bootstrap
104test ".$JSVC" = . && JSVC="$CATALINA_BASE/bin/jsvc"
105
106# Ensure that any user defined CLASSPATH variables are not used on startup,
107# but allow them to be specified in setenv.sh, in rare case when it is needed.
108CLASSPATH=
109JAVA_OPTS=
110if [ -r "$CATALINA_BASE/bin/setenv.sh" ]; then
111 . "$CATALINA_BASE/bin/setenv.sh"
112elif [ -r "$CATALINA_HOME/bin/setenv.sh" ]; then
113 . "$CATALINA_HOME/bin/setenv.sh"
114fi
115
116# Add on extra jar files to CLASSPATH
117test ".$CLASSPATH" != . && CLASSPATH="${CLASSPATH}:"
118CLASSPATH="$CLASSPATH$CATALINA_HOME/bin/bootstrap.jar:$CATALINA_HOME/bin/commons-daemon.jar"
119
120test ".$CATALINA_OUT" = . && CATALINA_OUT="$CATALINA_BASE/logs/catalina-daemon.out"
121test ".$CATALINA_TMP" = . && CATALINA_TMP="$CATALINA_BASE/temp"
122
123# Add tomcat-juli.jar to classpath
124# tomcat-juli.jar can be over-ridden per instance
125if [ -r "$CATALINA_BASE/bin/tomcat-juli.jar" ] ; then
126 CLASSPATH="$CLASSPATH:$CATALINA_BASE/bin/tomcat-juli.jar"
127else
128 CLASSPATH="$CLASSPATH:$CATALINA_HOME/bin/tomcat-juli.jar"
129fi
130# Set juli LogManager config file if it is present and an override has not been issued
131if [ -z "$LOGGING_CONFIG" ]; then
132 if [ -r "$CATALINA_BASE/conf/logging.properties" ]; then
133 LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
134 else
135 # Bugzilla 45585
136 LOGGING_CONFIG="-Dnop"
137 fi
138fi
139
140test ".$LOGGING_MANAGER" = . && LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
141JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER"
142
143# Set -pidfile
144test ".$CATALINA_PID" = . && CATALINA_PID="$CATALINA_BASE/logs/catalina-daemon.pid"
145
146# Increase the maximum file descriptors if we can
147if [ "$cygwin" = "false" ]; then
148 MAX_FD_LIMIT=`ulimit -H -n`
149 if [ "$?" -eq 0 ]; then
150 # Darwin does not allow RLIMIT_INFINITY on file soft limit
151 if [ "$darwin" = "true" -a "$MAX_FD_LIMIT" = "unlimited" ]; then
152 MAX_FD_LIMIT=`/usr/sbin/sysctl -n kern.maxfilesperproc`
153 fi
154 test ".$MAX_FD" = ".maximum" && MAX_FD="$MAX_FD_LIMIT"
155 ulimit -n $MAX_FD
156 if [ "$?" -ne 0 ]; then
157 echo "$PROGRAM: Could not set maximum file descriptor limit: $MAX_FD"
158 fi
159 else
160 echo "$PROGRAM: Could not query system maximum file descriptor limit: $MAX_FD_LIMIT"
161 fi
162fi
163
164# ----- Execute The Requested Command -----------------------------------------
165case "$1" in
166 run )
167 shift
168 "$JSVC" $* \
169 $JSVC_OPTS \
170 -java-home "$JAVA_HOME" \
171 -pidfile "$CATALINA_PID" \
172 -wait 10 \
173 -nodetach \
174 -outfile "&1" \
175 -errfile "&2" \
176 -classpath "$CLASSPATH" \
177 "$LOGGING_CONFIG" $JAVA_OPTS $CATALINA_OPTS \
178 -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" \
179 -Dcatalina.base="$CATALINA_BASE" \
180 -Dcatalina.home="$CATALINA_HOME" \
181 -Djava.io.tmpdir="$CATALINA_TMP" \
182 $CATALINA_MAIN
183 exit $?
184 ;;
185 start )
186 "$JSVC" $JSVC_OPTS \
187 -java-home "$JAVA_HOME" \
188 -user $TOMCAT_USER \
189 -pidfile "$CATALINA_PID" \
190 -wait 10 \
191 -outfile "$CATALINA_OUT" \
192 -errfile "&1" \
193 -classpath "$CLASSPATH" \
194 "$LOGGING_CONFIG" $JAVA_OPTS $CATALINA_OPTS \
195 -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" \
196 -Dcatalina.base="$CATALINA_BASE" \
197 -Dcatalina.home="$CATALINA_HOME" \
198 -Djava.io.tmpdir="$CATALINA_TMP" \
199 $CATALINA_MAIN
200 exit $?
201 ;;
202 stop )
203 "$JSVC" $JSVC_OPTS \
204 -stop \
205 -pidfile "$CATALINA_PID" \
206 -classpath "$CLASSPATH" \
207 -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" \
208 -Dcatalina.base="$CATALINA_BASE" \
209 -Dcatalina.home="$CATALINA_HOME" \
210 -Djava.io.tmpdir="$CATALINA_TMP" \
211 $CATALINA_MAIN
212 exit $?
213 ;;
214 version )
215 "$JSVC" \
216 -java-home "$JAVA_HOME" \
217 -pidfile "$CATALINA_PID" \
218 -classpath "$CLASSPATH" \
219 -errfile "&2" \
220 -version \
221 -check \
222 $CATALINA_MAIN
223 if [ "$?" = 0 ]; then
224 "$JAVA_BIN" \
225 -classpath "$CATALINA_HOME/lib/catalina.jar" \
226 org.apache.catalina.util.ServerInfo
227 fi
228 exit $?
229 ;;
230 * )
231 echo "Unknown command: \`$1'"
232 echo "Usage: $PROGRAM ( commands ... )"
233 echo "commands:"
234 echo " run Start Tomcat without detaching from console"
235 echo " start Start Tomcat"
236 echo " stop Stop Tomcat"
237 echo " version What version of commons daemon and Tomcat"
238 echo " are you running?"
239 exit 1
240 ;;
241esac