1 |
#!/bin/bash |
2 |
# |
3 |
# tomcat This shell script takes care of starting and stopping Tomcat |
4 |
# |
5 |
# chkconfig: - 80 20 |
6 |
# |
7 |
### BEGIN INIT INFO |
8 |
# Provides: tomcat |
9 |
# Required-Start: $network $syslog |
10 |
# Required-Stop: $network $syslog |
11 |
# Default-Start: |
12 |
# Default-Stop: |
13 |
# Description: Release implementation for Servlet 3.0 and JSP 2.2 |
14 |
# Short-Description: start and stop tomcat |
15 |
### END INIT INFO |
16 |
# |
17 |
# - originally written by Henri Gomez, Keith Irwin, and Nicolas Mailhot |
18 |
# - heavily rewritten by Deepak Bhole and Jason Corley |
19 |
# |
20 |
|
21 |
## Source function library. |
22 |
#. /etc/rc.d/init.d/functions |
23 |
# Source LSB function library. |
24 |
if [ -r /lib/lsb/init-functions ]; then |
25 |
. /lib/lsb/init-functions |
26 |
else |
27 |
exit 1 |
28 |
fi |
29 |
|
30 |
DISTRIB_ID=`lsb_release -i -s 2>/dev/null` |
31 |
|
32 |
NAME="$(basename $0)" |
33 |
unset ISBOOT |
34 |
if [ "${NAME:0:1}" = "S" -o "${NAME:0:1}" = "K" ]; then |
35 |
NAME="${NAME:3}" |
36 |
ISBOOT="1" |
37 |
fi |
38 |
|
39 |
# For SELinux we need to use 'runuser' not 'su' |
40 |
if [ -x "/sbin/runuser" ]; then |
41 |
SU="/sbin/runuser -s /bin/sh" |
42 |
else |
43 |
SU="/bin/su -s /bin/sh" |
44 |
fi |
45 |
|
46 |
# Get the tomcat config (use this for environment specific settings) |
47 |
TOMCAT_CFG="/etc/tomcat/tomcat.conf" |
48 |
if [ -r "$TOMCAT_CFG" ]; then |
49 |
. $TOMCAT_CFG |
50 |
fi |
51 |
|
52 |
# Get instance specific config file |
53 |
if [ -r "/etc/sysconfig/${NAME}" ]; then |
54 |
. /etc/sysconfig/${NAME} |
55 |
fi |
56 |
|
57 |
# Define which connector port to use |
58 |
CONNECTOR_PORT="${CONNECTOR_PORT:-8080}" |
59 |
|
60 |
# Path to the tomcat launch script |
61 |
TOMCAT_SCRIPT="/usr/sbin/tomcat" |
62 |
|
63 |
# Tomcat program name |
64 |
TOMCAT_PROG="${NAME}" |
65 |
|
66 |
# Define the tomcat username |
67 |
TOMCAT_USER="${TOMCAT_USER:-tomcat}" |
68 |
|
69 |
# Define the tomcat log file |
70 |
TOMCAT_LOG="${TOMCAT_LOG:-${CATALINA_HOME}/logs/${NAME}-initd.log}" |
71 |
|
72 |
|
73 |
RETVAL="0" |
74 |
|
75 |
# Look for open ports, as the function name might imply |
76 |
function findFreePorts() { |
77 |
local isSet1="false" |
78 |
local isSet2="false" |
79 |
local isSet3="false" |
80 |
local lower="8000" |
81 |
randomPort1="0" |
82 |
randomPort2="0" |
83 |
randomPort3="0" |
84 |
local -a listeners="( $( |
85 |
netstat -ntl | \ |
86 |
awk '/^tcp/ {gsub("(.)*:", "", $4); print $4}' |
87 |
) )" |
88 |
while [ "$isSet1" = "false" ] || \ |
89 |
[ "$isSet2" = "false" ] || \ |
90 |
[ "$isSet3" = "false" ]; do |
91 |
let port="${lower}+${RANDOM:0:4}" |
92 |
if [ -z `expr " ${listeners[*]} " : ".*\( $port \).*"` ]; then |
93 |
if [ "$isSet1" = "false" ]; then |
94 |
export randomPort1="$port" |
95 |
isSet1="true" |
96 |
elif [ "$isSet2" = "false" ]; then |
97 |
export randomPort2="$port" |
98 |
isSet2="true" |
99 |
elif [ "$isSet3" = "false" ]; then |
100 |
export randomPort3="$port" |
101 |
isSet3="true" |
102 |
fi |
103 |
fi |
104 |
done |
105 |
} |
106 |
|
107 |
function makeHomeDir() { |
108 |
if [ ! -d "$CATALINA_HOME" ]; then |
109 |
echo "$CATALINA_HOME does not exist, creating" |
110 |
if [ ! -d "/usr/share/${NAME}" ]; then |
111 |
mkdir /usr/share/${NAME} |
112 |
cp -pLR /usr/share/tomcat/* /usr/share/${NAME} |
113 |
fi |
114 |
mkdir -p /var/log/${NAME} \ |
115 |
/var/cache/${NAME} \ |
116 |
/var/tmp/${NAME} |
117 |
ln -fs /var/cache/${NAME} ${CATALINA_HOME}/work |
118 |
ln -fs /var/tmp/${NAME} ${CATALINA_HOME}/temp |
119 |
cp -pLR /usr/share/${NAME}/bin $CATALINA_HOME |
120 |
cp -pLR /usr/share/${NAME}/conf $CATALINA_HOME |
121 |
ln -fs /usr/share/java/tomcat ${CATALINA_HOME}/lib |
122 |
ln -fs /usr/share/tomcat/webapps ${CATALINA_HOME}/webapps |
123 |
chown ${TOMCAT_USER}:${TOMCAT_USER} /var/log/${NAME} |
124 |
fi |
125 |
} |
126 |
|
127 |
function parseOptions() { |
128 |
options="" |
129 |
options="$options $( |
130 |
awk '!/^#/ && !/^$/ { ORS=" "; print "export ", $0, ";" }' \ |
131 |
$TOMCAT_CFG |
132 |
)" |
133 |
if [ -r "/etc/sysconfig/${NAME}" ]; then |
134 |
options="$options $( |
135 |
awk '!/^#/ && !/^$/ { ORS=" "; |
136 |
print "export ", $0, ";" }' \ |
137 |
/etc/sysconfig/${NAME} |
138 |
)" |
139 |
fi |
140 |
TOMCAT_SCRIPT="$options ${TOMCAT_SCRIPT}" |
141 |
} |
142 |
|
143 |
# See how we were called. |
144 |
function start() { |
145 |
|
146 |
echo -n "Starting ${TOMCAT_PROG}: " |
147 |
if [ "$RETVAL" != "0" ]; then |
148 |
log_failure_msg |
149 |
return |
150 |
fi |
151 |
if [ -f "/var/lock/subsys/${NAME}" ]; then |
152 |
if [ -s "/var/run/${NAME}.pid" ]; then |
153 |
read kpid < /var/run/${NAME}.pid |
154 |
# if checkpid $kpid 2>&1; then |
155 |
if [ -d "/proc/${kpid}" ]; then |
156 |
log_success_msg |
157 |
if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then |
158 |
echo |
159 |
fi |
160 |
return 0 |
161 |
fi |
162 |
fi |
163 |
fi |
164 |
# fix permissions on the log and pid files |
165 |
export CATALINA_PID="/var/run/${NAME}.pid" |
166 |
touch $CATALINA_PID 2>&1 || RETVAL="4" |
167 |
if [ "$RETVAL" -eq "0" -a "$?" -eq "0" ]; then |
168 |
chown ${TOMCAT_USER}:${TOMCAT_USER} $CATALINA_PID |
169 |
fi |
170 |
[ "$RETVAL" -eq "0" ] && touch $TOMCAT_LOG 2>&1 || RETVAL="4" |
171 |
if [ "$RETVAL" -eq "0" -a "$?" -eq "0" ]; then |
172 |
chown ${TOMCAT_USER}:${TOMCAT_USER} $TOMCAT_LOG |
173 |
fi |
174 |
if [ "$CATALINA_HOME" != "/usr/share/tomcat" -a "$RETVAL" -eq "0" ]; then |
175 |
# Create a tomcat directory if it doesn't exist |
176 |
makeHomeDir |
177 |
# If CATALINA_HOME doesn't exist modify port number so that |
178 |
# multiple instances don't interfere with each other |
179 |
findFreePorts |
180 |
sed -i -e "s/8005/${randomPort1}/g" -e "s/8080/${CONNECTOR_PORT}/g" \ |
181 |
-e "s/8009/${randomPort2}/g" -e "s/8443/${randomPort3}/g" \ |
182 |
${CATALINA_HOME}/conf/server.xml |
183 |
fi |
184 |
parseOptions |
185 |
if [ "$RETVAL" -eq "0" -a "$SECURITY_MANAGER" = "true" ]; then |
186 |
$SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} start-security" \ |
187 |
>> ${TOMCAT_LOG} 2>&1 || RETVAL="4" |
188 |
else |
189 |
|
190 |
[ "$RETVAL" -eq "0" ] && $SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} start" >> ${TOMCAT_LOG} 2>&1 || RETVAL="4" |
191 |
fi |
192 |
if [ "$RETVAL" -eq "0" ]; then |
193 |
log_success_msg |
194 |
touch /var/lock/subsys/${NAME} |
195 |
else |
196 |
log_failure_msg "Error code ${RETVAL}" |
197 |
fi |
198 |
if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then |
199 |
echo |
200 |
fi |
201 |
} |
202 |
|
203 |
function stop() { |
204 |
echo -n "Stopping ${TOMCAT_PROG}: " |
205 |
if [ -f "/var/lock/subsys/${NAME}" ]; then |
206 |
parseOptions |
207 |
if [ "$RETVAL" -eq "0" ]; then |
208 |
touch /var/lock/subsys/${NAME} 2>&1 || RETVAL="4" |
209 |
[ "$RETVAL" -eq "0" ] && $SU - $TOMCAT_USER -c "${TOMCAT_SCRIPT} stop" >> ${TOMCAT_LOG} 2>&1 || RETVAL="4" |
210 |
fi |
211 |
if [ "$RETVAL" -eq "0" ]; then |
212 |
count="0" |
213 |
if [ -s "/var/run/${NAME}.pid" ]; then |
214 |
read kpid < /var/run/${NAME}.pid |
215 |
until [ "$(ps --pid $kpid | grep -c $kpid)" -eq "0" ] || \ |
216 |
[ "$count" -gt "$SHUTDOWN_WAIT" ]; do |
217 |
if [ "$SHUTDOWN_VERBOSE" = "true" ]; then |
218 |
echo "waiting for processes $kpid to exit" |
219 |
fi |
220 |
sleep 1 |
221 |
let count="${count}+1" |
222 |
done |
223 |
if [ "$count" -gt "$SHUTDOWN_WAIT" ]; then |
224 |
if [ "$SHUTDOWN_VERBOSE" = "true" ]; then |
225 |
log_warning_msg "killing processes which did not stop after ${SHUTDOWN_WAIT} seconds" |
226 |
fi |
227 |
kill -9 $kpid |
228 |
fi |
229 |
log_success_msg |
230 |
fi |
231 |
rm -f /var/lock/subsys/${NAME} /var/run/${NAME}.pid |
232 |
else |
233 |
log_failure_msg |
234 |
RETVAL="4" |
235 |
fi |
236 |
else |
237 |
log_success_msg |
238 |
RETVAL="0" |
239 |
fi |
240 |
if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then |
241 |
echo |
242 |
fi |
243 |
} |
244 |
|
245 |
function usage() |
246 |
{ |
247 |
echo "Usage: $0 {start|stop|restart|condrestart|try-restart|reload|force-reload|status|version}" |
248 |
RETVAL="2" |
249 |
} |
250 |
|
251 |
# See how we were called. |
252 |
RETVAL="0" |
253 |
case "$1" in |
254 |
start) |
255 |
start |
256 |
;; |
257 |
stop) |
258 |
stop |
259 |
;; |
260 |
restart) |
261 |
stop |
262 |
start |
263 |
;; |
264 |
condrestart|try-restart) |
265 |
if [ -s "/var/run/${NAME}.pid" ]; then |
266 |
stop |
267 |
start |
268 |
fi |
269 |
;; |
270 |
reload) |
271 |
RETVAL="3" |
272 |
;; |
273 |
force-reload) |
274 |
if [ -s "/var/run/${NAME}.pid" ]; then |
275 |
stop |
276 |
start |
277 |
fi |
278 |
;; |
279 |
status) |
280 |
if [ -s "/var/run/${NAME}.pid" ]; then |
281 |
read kpid < /var/run/${NAME}.pid |
282 |
if [ -d "/proc/${kpid}" ]; then |
283 |
log_success_msg "${NAME} (pid ${kpid}) is running..." |
284 |
RETVAL="0" |
285 |
else |
286 |
# The pid file exists but the process is not running |
287 |
log_warning_msg "PID file exists, but process is not running" |
288 |
RETVAL="1" |
289 |
fi |
290 |
else |
291 |
pid="$(/usr/bin/pgrep -d , -u ${TOMCAT_USER} -G ${TOMCAT_USER} java)" |
292 |
if [ -z "$pid" ]; then |
293 |
# status ${NAME} |
294 |
# RETVAL="$?" |
295 |
log_success_msg "${NAME} is stopped" |
296 |
RETVAL="3" |
297 |
else |
298 |
log_success_msg "${NAME} (pid $pid) is running..." |
299 |
RETVAL="0" |
300 |
fi |
301 |
fi |
302 |
if [ -f /var/lock/subsys/${NAME} ]; then |
303 |
pid="$(/usr/bin/pgrep -d , -u ${TOMCAT_USER} -G ${TOMCAT_USER} java)" |
304 |
# The lockfile exists but the process is not running |
305 |
if [ -z "$pid" ]; then |
306 |
log_failure_msg "${NAME} lockfile exists but process is not running" |
307 |
RETVAL="2" |
308 |
fi |
309 |
fi |
310 |
;; |
311 |
version) |
312 |
${TOMCAT_SCRIPT} version |
313 |
;; |
314 |
*) |
315 |
usage |
316 |
;; |
317 |
esac |
318 |
|
319 |
exit $RETVAL |