ITeye博客

Fedora Tomcat5 service 分析

leitelyaya · 2010-10-16 08:04 · Linux · 操作系统 · Markdown

/etc/init.d/tomcat5:

  1. #!/bin/bash
  2. #
  3. # tomcat5      This shell script takes care of starting and stopping Tomcat
  4. #
  5. # chkconfig: - 80 20
  6. #
  7. ### BEGIN INIT INFO
  8. # Provides: tomcat5
  9. # Required-Start: $network $syslog
  10. # Required-Stop: $network $syslog
  11. # Default-Start:
  12. # Default-Stop:
  13. # Description: Release implementation for Servlet 2.4 and JSP 2.0
  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. if [ -r /lib/lsb/init-functions ]; then
  21. . /lib/lsb/init-functions
  22. else
  23. exit 1
  24. fi
  25. NAME="$(basename $0)"
  26. unset ISBOOT
  27. if [ "${NAME:0:1}" = "S" -o "${NAME:0:1}" = "K" ]; then
  28. NAME="${NAME:3}"
  29. ISBOOT="1"
  30. fi
  31. # For SELinux we need to use 'runuser' not 'su'
  32. if [ -x "/sbin/runuser" ]; then
  33. SU="/sbin/runuser"
  34. else
  35. SU="su"
  36. fi
  37. # Get the tomcat config (use this for environment specific settings)
  38. TOMCAT_CFG="/etc/tomcat5/tomcat5.conf "
  39. if [ -r "$TOMCAT_CFG" ]; then
  40. . ${TOMCAT_CFG}
  41. fi
  42. # Get instance specific config file
  43. if [ -r "/etc/sysconfig/${NAME}" ]; then
  44. . /etc/sysconfig/${NAME}
  45. fi
  46. if [ -z "${CATALINA_BASE}" ]; then
  47. CATALINA_BASE="${CATALINA_HOME}"
  48. fi
  49. # Define which connector port to use
  50. CONNECTOR_PORT="${CONNECTOR_PORT:-8080
  51. }"
  52. # Path to the tomcat launch script
  53. TOMCAT_SCRIPT="/usr/bin/dtomcat5"
  54. # Path to the script that will refresh jar symlinks on startup
  55. TOMCAT_RELINK_SCRIPT="${CATALINA_HOME}/bin/relink"
  56. # Tomcat program name
  57. TOMCAT_PROG="$NAME"
  58. # Define the tomcat username
  59. TOMCAT_USER="${TOMCAT_USER:-tomcat}"
  60. # Define the tomcat log file
  61. TOMCAT_LOG="${TOMCAT_LOG:-/var/log/tomcat5/catalina.out}"
  62. RETVAL="0"
  63. function checkpid() {
  64. local i
  65. for i in $* ; do
  66. if [ -d "/proc/${i}" ]; then
  67. return 0
  68. fi
  69. done
  70. return 1
  71. }
  72. # Look for open ports, as the function name might imply
  73. function findFreePorts() {
  74. local isSet1="false"
  75. local isSet2="false"
  76. local isSet3="false"
  77. local lower="8000"
  78. randomPort1="0"
  79. randomPort2="0"
  80. randomPort3="0"
  81. local -a listeners="( $(
  82. netstat -ntl | \
  83. awk '/^tcp/ {gsub("(.)*:", "", $4); print $4}'
  84. ) )"
  85. while [ "$isSet1" = "false" ] || \
  86. [ "$isSet2" = "false" ] || \
  87. [ "$isSet3" = "false" ]; do
  88. let port="${lower}+${RANDOM:0:4}"
  89. if [ -z `expr " ${listeners[*]} " : ".*\( $port \).*"` ]; then
  90. if [ "$isSet1" = "false" ]; then
  91. export randomPort1="$port"
  92. isSet1="true"
  93. elif [ "$isSet2" = "false" ]; then
  94. export randomPort2="$port"
  95. isSet2="true"
  96. elif [ "$isSet3" = "false" ]; then
  97. export randomPort3="$port"
  98. isSet3="true"
  99. 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 < /var/run/${NAME}.pid
  149. if checkpid $kpid 2>&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" >> $TOMCAT_LOG 2>&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 < /var/run/${NAME}.pid
  191. if checkpid $kpid 2>&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" >> $TOMCAT_LOG 2>&1
  213. RETVAL="$?"
  214. if [ "$RETVAL" -eq "0" ]; then
  215. count="0"
  216. if [ -f "/var/run/${NAME}.pid" ]; then
  217. read kpid < /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:

  1. # System-wide  configuration file for tomcat5 services
  2. # This will be sourced by tomcat5 and any secondary service
  3. # Values will be overridden by service-specific configuration
  4. # files in /etc/sysconfig
  5. # Use this one to change default values for all services
  6. # Change the service specific ones to affect only one service
  7. # (see, for instance, /etc/sysconfig/tomcat5)
  8. #
  9. # 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:

  1. # Service-specific
  2. configuration file for tomcat5 services
  3. # This will be sourced by the SysV service script after the global
  4. # configuration file /etc/tomcat5/tomcat5.conf, thus allowing values
  5. # to be overridden on a per-service way
  6. #
  7. # NEVER change the init script itself:
  8. # To change values for all services make your changes in
  9. # /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. #

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。