---
title: "Fedora Tomcat5 service 分析"
author: "leitelyaya"
date: "2010-10-16T08:04:29Z"
tags: ["Cache", "EXT", "F#", "JVM", "Tomcat"]
categories: ["操作系统"]
slug: "fedora-tomcat5-service"
draft: false
lastmod: "2010-10-26T14:04:07Z"
id: 786192
url: "https://leitelyaya.iteye.com/blog/786192"
---

**/etc/init.d/tomcat5:**

001. #!/bin/bash
002. \#
003. # tomcat5      This shell script takes care of starting and stopping Tomcat
004. \#
005. # chkconfig: - 80 20
006. \#
007. ### BEGIN INIT INFO
008. # Provides: tomcat5
009. # Required-Start: $network $syslog
010. # Required-Stop: $network $syslog
011. # Default-Start:
012. # Default-Stop:
013. # Description: Release implementation for Servlet 2.4 and JSP 2.0
014. # Short-Description: start and stop tomcat
015. ### END INIT INFO
016. \#
017. # - originally written by Henri Gomez, Keith Irwin, and Nicolas Mailhot
018. # - heavily rewritten by Deepak Bhole and Jason Corley
019. \#
020. if \[ -r /lib/lsb/init-functions ]; then
021. . /lib/lsb/init-functions
022. else
023. exit 1
024. fi
025. NAME="$(basename $0)"
026. unset ISBOOT
027. if \[ "${NAME:0:1}" = "S" -o "${NAME:0:1}" = "K" ]; then
028. NAME="${NAME:3}"
029. ISBOOT="1"
030. fi
031. # For SELinux we need to use 'runuser' not 'su'
032. if \[ -x "/sbin/runuser" ]; then
033. SU="/sbin/runuser"
034. else
035. SU="su"
036. fi
037. # **Get the tomcat config (use this for environment specific settings)**
038. TOMCAT\_CFG="[/etc/tomcat5/tomcat5.conf](#tomcat5_conf "/etc/tomcat5/tomcat5.conf") "
039. if \[ -r "$TOMCAT\_CFG" ]; then
040. . ${TOMCAT\_CFG}
041. fi
042. # **Get instance specific config file**
043. if \[ -r "/etc/sysconfig/${NAME}" ]; then
044. . [/etc/sysconfig/${NAME}](#tomcat5 "/etc/sysconfig/${NAME}")
045. fi
046. if \[ -z "${CATALINA\_BASE}" ]; then
047. CATALINA\_BASE="${CATALINA\_HOME}"
048. fi
049. # Define which connector port to use
050. CONNECTOR\_PORT="${CONNECTOR\_PORT:-8080
051. }"
052. # Path to the tomcat launch script
053. TOMCAT\_SCRIPT="/usr/bin/dtomcat5"
054. # Path to the script that will refresh jar symlinks on startup
055. TOMCAT\_RELINK\_SCRIPT="${CATALINA\_HOME}/bin/relink"
056. # Tomcat program name
057. TOMCAT\_PROG="$NAME"
058. # Define the tomcat username
059. TOMCAT\_USER="${TOMCAT\_USER:-tomcat}"
060. # Define the tomcat log file
061. TOMCAT\_LOG="${TOMCAT\_LOG:-/var/log/tomcat5/catalina.out}"
062. RETVAL="0"
063. function checkpid() {
064. local i
065. for i in $* ; do
066. if \[ -d "/proc/${i}" ]; then
067. return 0
068. fi
069. done
070. return 1
071. }
072. # Look for open ports, as the function name might imply
073. function findFreePorts() {
074. local isSet1="false"
075. local isSet2="false"
076. local isSet3="false"
077. local lower="8000"
078. randomPort1="0"
079. randomPort2="0"
080. randomPort3="0"
081. local -a listeners="( $(
082. netstat -ntl | \\
083. awk '/^tcp/ {gsub("(.)\*:", "", $4); print $4}'
084. ) )"
085. while \[ "$isSet1" = "false" ] || \\
086. \[ "$isSet2" = "false" ] || \\
087. \[ "$isSet3" = "false" ]; do
088. let port="${lower}+${RANDOM:0:4}"
089. if \[ -z \`expr " ${listeners\[\*]} " : ".\*\\( $port \\).\*"\` ]; then
090. if \[ "$isSet1" = "false" ]; then
091. export randomPort1="$port"
092. isSet1="true"
093. elif \[ "$isSet2" = "false" ]; then
094. export randomPort2="$port"
095. isSet2="true"
096. elif \[ "$isSet3" = "false" ]; then
097. export randomPort3="$port"
098. isSet3="true"
099. fi
100. fi
101. done
102. }
103. function makeHomeDir() {
104. SAVE\_UMASK=$(umask)
105. umask 022
106. if \[ ! -d "${CATALINA\_HOME}" ]; then
107. echo "${CATALINA\_HOME} does not exist, creating"
108. if \[ ! -d "/var/lib/${NAME}" ]; then
109. mkdir -p /var/lib/${NAME}
110. cp -pR /var/lib/tomcat5/* /var/lib/${NAME}
111. fi
112. mkdir -p ${CATALINA\_HOME} ${CATALINA\_HOME}/conf /var/cache/${NAME}/temp \\
113. /var/cache/${NAME}/work /var/log/${NAME}
114. for i in temp work; do
115. ln -fs /var/cache/${NAME}/${i} ${CATALINA\_HOME}/${i}
116. done
117. for i in common server shared webapps; do
118. ln -fs /var/lib/${NAME}/${i} ${CATALINA\_HOME}/${i}
119. done
120. ln -fs /var/log/${NAME} ${CATALINA\_HOME}/logs
121. cp -pLR /etc/tomcat5/* ${CATALINA\_HOME}/conf/
122. cp -pLR /usr/share/tomcat5/bin ${CATALINA\_HOME}
123. cp -pLR /var/cache/tomcat5/work/* ${CATALINA\_HOME}/work/
124. chown ${TOMCAT\_USER}:${TOMCAT\_USER} /var/log/${NAME}
125. fi
126. umask ${SAVE\_UMASK}
127. }
128. function parseOptions() {
129. options=""
130. options="$options $(
131. awk '!/^#/ && !/^$/ { ORS=" "; print "export ", $0, ";" }' \\
132. $TOMCAT\_CFG
133. )"
134. if \[ -r "/etc/sysconfig/${NAME}" ]; then
135. options="$options $(
136. awk '!/^#/ && !/^$/ { ORS=" ";
137. print "export ", $0, ";" }' \\
138. /etc/sysconfig/${NAME}
139. )"
140. fi
141. TOMCAT\_SCRIPT="$options $TOMCAT\_SCRIPT"
142. }
143. # See how we were called.
144. function start() {
145. echo -n "Starting ${TOMCAT\_PROG}: "
146. if \[ -f "/var/lock/subsys/${NAME}" ] ; then
147. if \[ -f "/var/run/${NAME}.pid" ]; then
148. read kpid &lt; /var/run/${NAME}.pid
149. if checkpid $kpid 2&gt;&1; then
150. echo "$NAME process already running"
151. return -1
152. else
153. echo "lock file found but no process running for"
154. echo "pid $kpid, continuing"
155. fi
156. fi
157. fi
158. export CATALINA\_PID="/var/run/${NAME}.pid"
159. touch $CATALINA\_PID
160. chown ${TOMCAT\_USER}:${TOMCAT\_USER} $CATALINA\_PID
161. if \[ "$CATALINA\_HOME" != "/usr/share/tomcat5" ]; then
162. # Create a tomcat directory if it doesn't exist
163. makeHomeDir
164. # If CATALINA\_HOME doesn't exist modify port number so that
165. # multiple instances don't interfere with each other
166. findFreePorts
167. sed -i -e "s/8005/${randomPort1}/g" -e "s/8080/${CONNECTOR\_PORT}/g" \\
168. -e "s/8009/${randomPort2}/g" -e "s/8443/${randomPort3}/g" \\
169. ${CATALINA\_HOME}/conf/server.xml
170. fi
171. touch $TOMCAT\_LOG
172. chown ${TOMCAT\_USER}:${TOMCAT\_USER} $TOMCAT\_LOG
173. if \[ "$RELINK" = "yes" ]; then
174. $TOMCAT\_RELINK\_SCRIPT
175. fi
176. $SU - $TOMCAT\_USER -c "$TOMCAT\_SCRIPT start" &gt;&gt; $TOMCAT\_LOG 2&gt;&1
177. RETVAL="$?"
178. if \[ "$RETVAL" -eq 0 ]; then
179. log\_success\_msg
180. touch /var/lock/subsys/${NAME}
181. else
182. log\_failure\_msg
183. fi
184. #echo
185. return $RETVAL
186. }
187. function status() {
188. RETVAL="1"
189. if \[ -f "/var/run/${NAME}.pid" ]; then
190. read kpid &lt; /var/run/${NAME}.pid
191. if checkpid $kpid 2&gt;&1; then
192. echo "$0 is already running (${kpid})"
193. RETVAL="0"
194. else
195. echo "lock file found but no process running for pid $kpid"
196. fi
197. else
198. pid="$(pgrep -u ${TOMCAT\_USER} -G ${TOMCAT\_USER} java)"
199. if \[ -n "$pid" ]; then
200. echo "$0 running (${pid}) but no PID file exists"
201. RETVAL="0"
202. else
203. echo "$0 is stopped"
204. fi
205. fi
206. return $RETVAL
207. }
208. function stop() {
209. local STOP\_VERBOSE="false"
210. echo -n "Stopping $TOMCAT\_PROG: "
211. if \[ -f "/var/lock/subsys/${NAME}" ]; then
212. $SU - $TOMCAT\_USER -c "$TOMCAT\_SCRIPT stop" &gt;&gt; $TOMCAT\_LOG 2&gt;&1
213. RETVAL="$?"
214. if \[ "$RETVAL" -eq "0" ]; then
215. count="0"
216. if \[ -f "/var/run/${NAME}.pid" ]; then
217. read kpid &lt; /var/run/${NAME}.pid
218. until \[ "$(ps --pid $kpid | grep -c $kpid)" -eq "0" ] || \\
219. \[ "$count" -gt "$SHUTDOWN\_WAIT" ]; do
220. if \[ "$STOP\_VERBOSE" = "true" ]; then
221. echo -n -e "\\nwaiting for processes $kpid to exit"
222. fi
223. sleep 1
224. let count="${count}+1"
225. done
226. if \[ "$count" -gt "$SHUTDOWN\_WAIT" ]; then
227. if \[ "$STOP\_VERBOSE" = "true" ]; then
228. echo -n -e "\\nkilling processes which didn't stop"
229. echo -n -e "after "
230. echo -n "$SHUTDOWN\_WAIT seconds"
231. fi
232. kill -9 $kpid
233. fi
234. if \[ "$STOP\_VERBOSE" = "true" -a "$count" -gt "0" ]; then
235. echo -n -e "\\n"
236. fi
237. log\_success\_msg
238. else
239. log\_failure\_msg
240. fi
241. rm -f /var/lock/subsys/$NAME /var/run/$NAME.pid
242. else
243. log\_failure\_msg
244. fi
245. else
246. log\_failure\_msg
247. fi
248. #echo
249. }
250. # See how we were called.
251. case "$1" in
252. start)
253. parseOptions
254. start
255. ;;
256. stop)
257. parseOptions
258. stop
259. ;;
260. restart)
261. parseOptions
262. stop
263. sleep 2
264. start
265. ;;
266. condrestart|try-restart)
267. if \[ -f "/var/run/${NAME}.pid" ]; then
268. parseOptions
269. stop
270. start
271. fi
272. ;;
273. reload)
274. RETVAL="3"
275. ;;
276. force-reload)
277. if \[ -f "/var/run/${NAME}.pid" ]; then
278. stop
279. start
280. fi
281. ;;
282. status)
283. status
284. ;;
285. version)
286. parseOptions
287. "${JAVA\_HOME}/bin/java" \\
288. -classpath "${CATALINA\_HOME}/server/lib/\[tomcat5]\[catalina].jar" \\
289. org.apache.catalina.util.ServerInfo
290. ;;
291. \*)
292. echo "Usage: $TOMCAT\_PROG {start|stop|restart|condrestart|try-restart|reload|force-reload|status|version}"
293. exit 1
294. esac
295. exit $RETVAL

**/etc/tomcat5/tomcat5.conf:**

01. # **System-wide**  configuration file for tomcat5 services
02. # This will be sourced by tomcat5 and any secondary service
03. # Values will be overridden by service-specific configuration
04. # files in /etc/sysconfig
05. # Use this one to change default values for all services
06. # Change the service specific ones to affect only one service
07. # (see, for instance, /etc/sysconfig/tomcat5)
08. \#
09. # tomcat5 service configuration file
10. # you could also override JAVA\_HOME here
11. # Where your java installation lives
12. JAVA\_HOME="/usr/lib/jvm/java-1.6.0"
13. # Where your tomcat installation lives
14. # That change from spanvious RPM where TOMCAT\_HOME
15. # used to be /var/tomcat.
16. # Now /var/tomcat will be the base for webapps only
17. CATALINA\_HOME="/usr/share/tomcat5"
18. JASPER\_HOME="/usr/share/tomcat5"
19. CATALINA\_TMPDIR="/usr/share/tomcat5/temp"
20. JAVA\_ENDORSED\_DIRS="/usr/share/tomcat5/common/endorsed"
21. # You can pass some parameters to java
22. # here if you wish to
23. #JAVA\_OPTS="-Xminf0.1 -Xmaxf0.3"
24. # Use JAVA\_OPTS to set java.library.path for libtcnative.so
25. #JAVA\_OPTS="-Djava.library.path=/usr/lib64"
26. # Bug 190:
27. # https://www.jpackage.org/bugzilla/show\_bug.cgi?id=190
28. # System property catalina.ext.dirs should be set to its default value
29. # for ExtensionValidator to be functional.
30. JAVA\_OPTS="$JAVA\_OPTS -Dcatalina.ext.dirs=$CATALINA\_HOME/shared/lib:$CATALINA\_HOME/common/lib"
31. # What user should run tomcat
32. TOMCAT\_USER="tomcat"
33. # You can change your tomcat locale here
34. #LANG=en\_US
35. # Time to wait in seconds, before killing process
36. SHUTDOWN\_WAIT=30
37. # Set the TOMCAT\_PID location
38. CATALINA\_PID=/var/run/tomcat5.pid
39. # Connector port is 8080 for this tomcat5 instance
40. #CONNECTOR\_PORT=8080
41. # Change to yes if you want the tomcat init script to run the relink script
42. RELINK="no"
43. # If you wish to further customize your tomcat environment,
44. # put your own definitions here
45. # (i.e. LD\_LIBRARY\_PATH for some jdbc drivers)
46. # Just do not forget to export them :)

**/etc/tomcat5/tomcat5.conf:**

01. # S**ervice-specific**
02. configuration file for tomcat5 services
03. # This will be sourced by the SysV service script after the global
04. # configuration file /etc/tomcat5/tomcat5.conf, thus allowing values
05. # to be overridden on a per-service way
06. \#
07. # NEVER change the init script itself:
08. # To change values for all services make your changes in
09. # /etc/tomcat5/tomcat5.conf
10. # To change values
11. for a specific service, change it here
12. # To create a new service
13. , create a link
14. from /etc/init.d/ to
15. # /etc/init.d/tomcat5 (do not copy the init script) and make a copy
16. of the
17. # /etc/sysconfig/tomcat5 file to /etc/sysconfig/ and change
18. # the property values so the two services won't conflict
19. # Register the new service in the system as usual (see chkconfig and similars)
20. \#