Fedora Tomcat5 service 分析

/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.         
  59. # Define the tomcat username
  60. TOMCAT_USER="${TOMCAT_USER:-tomcat}"
  61. # Define the tomcat log file
  62. TOMCAT_LOG="${TOMCAT_LOG:-/var/log/tomcat5/catalina.out}"
  63. RETVAL="0"
  64. function checkpid() {
  65.     local i
  66.     for i in $* ; do
  67.         if [ -d "/proc/${i}" ]; then
  68.             return 0
  69.         fi
  70.     done
  71.     return 1
  72. }
  73. # Look for open ports, as the function name might imply
  74. function findFreePorts() {
  75.     local isSet1="false"
  76.     local isSet2="false"
  77.     local isSet3="false"
  78.     local lower="8000"
  79.     randomPort1="0"
  80.     randomPort2="0"
  81.     randomPort3="0"
  82.     local -a listeners="( $(
  83.                         netstat -ntl | \
  84.                         awk '/^tcp/ {gsub("(.)*:", "", $4); print $4}'
  85.                     ) )"
  86.     while [ "$isSet1" = "false" ] || \
  87.           [ "$isSet2" = "false" ] || \
  88.           [ "$isSet3" = "false" ]; do
  89.         let port="${lower}+${RANDOM:0:4}"
  90.         if [ -z `expr " ${listeners[*]} " : ".*\( $port \).*"` ]; then
  91.             if [ "$isSet1" = "false" ]; then
  92.                 export randomPort1="$port"
  93.                 isSet1="true"
  94.             elif [ "$isSet2" = "false" ]; then
  95.                 export randomPort2="$port"
  96.                 isSet2="true"
  97.             elif [ "$isSet3" = "false" ]; then
  98.                 export randomPort3="$port"
  99.                 isSet3="true"
  100.             fi
  101.         fi
  102.     done
  103. }
  104. function makeHomeDir() {
  105.     SAVE_UMASK=$(umask)
  106.     umask 022
  107.     if [ ! -d "${CATALINA_HOME}" ]; then
  108.         echo "${CATALINA_HOME} does not exist, creating"
  109.         if [ ! -d "/var/lib/${NAME}" ]; then
  110.             mkdir -p /var/lib/${NAME}
  111.             cp -pR /var/lib/tomcat5/* /var/lib/${NAME}
  112.         fi
  113.         mkdir -p ${CATALINA_HOME} ${CATALINA_HOME}/conf /var/cache/${NAME}/temp \
  114.             /var/cache/${NAME}/work /var/log/${NAME}
  115.         for i in temp work; do
  116.             ln -fs /var/cache/${NAME}/${i} ${CATALINA_HOME}/${i}
  117.         done
  118.         for i in common server shared webapps; do
  119.             ln -fs /var/lib/${NAME}/${i} ${CATALINA_HOME}/${i}
  120.         done
  121.         ln -fs /var/log/${NAME} ${CATALINA_HOME}/logs
  122.         cp -pLR /etc/tomcat5/* ${CATALINA_HOME}/conf/
  123.         cp -pLR /usr/share/tomcat5/bin ${CATALINA_HOME}
  124.         cp -pLR /var/cache/tomcat5/work/* ${CATALINA_HOME}/work/
  125.         chown ${TOMCAT_USER}:${TOMCAT_USER} /var/log/${NAME}
  126.     fi
  127.     umask ${SAVE_UMASK}
  128. }
  129. function parseOptions() {
  130.     options=""
  131.     options="$options $(
  132.                  awk '!/^#/ && !/^$/ { ORS=" "; print "export ", $0, ";" }' \
  133.                  $TOMCAT_CFG
  134.              )"
  135.     if [ -r "/etc/sysconfig/${NAME}" ]; then
  136.         options="$options $(
  137.                      awk '!/^#/ && !/^$/ { ORS=" "; 
  138.                                            print "export ", $0, ";" }' \
  139.                      /etc/sysconfig/${NAME}
  140.                  )"
  141.     fi
  142.     TOMCAT_SCRIPT="$options $TOMCAT_SCRIPT"
  143. }
  144. # See how we were called.
  145. function start() {
  146.     echo -n "Starting ${TOMCAT_PROG}: "
  147.     if [ -f "/var/lock/subsys/${NAME}" ] ; then
  148.         if [ -f "/var/run/${NAME}.pid" ]; then
  149.             read kpid < /var/run/${NAME}.pid
  150.                 if checkpid $kpid 2>&1; then
  151.                     echo "$NAME process already running"
  152.                         return -1
  153.                     else
  154.                         echo "lock file found but no process running for"
  155.                         echo "pid $kpid, continuing"
  156.                 fi
  157.         fi
  158.     fi
  159.     export CATALINA_PID="/var/run/${NAME}.pid"
  160.     touch $CATALINA_PID
  161.     chown ${TOMCAT_USER}:${TOMCAT_USER} $CATALINA_PID
  162.     if [ "$CATALINA_HOME" != "/usr/share/tomcat5" ]; then
  163.         # Create a tomcat directory if it doesn't exist
  164.         makeHomeDir
  165.         # If CATALINA_HOME doesn't exist modify port number so that
  166.         # multiple instances don't interfere with each other
  167.         findFreePorts
  168.         sed -i -e "s/8005/${randomPort1}/g" -e "s/8080/${CONNECTOR_PORT}/g" \
  169.             -e "s/8009/${randomPort2}/g" -e "s/8443/${randomPort3}/g" \
  170.             ${CATALINA_HOME}/conf/server.xml
  171.     fi
  172.     touch $TOMCAT_LOG
  173.     chown ${TOMCAT_USER}:${TOMCAT_USER} $TOMCAT_LOG
  174.     if [ "$RELINK" = "yes" ]; then
  175.         $TOMCAT_RELINK_SCRIPT
  176.     fi
  177.     $SU - $TOMCAT_USER -c "$TOMCAT_SCRIPT start" >> $TOMCAT_LOG 2>&1
  178.     RETVAL="$?"
  179.     if [ "$RETVAL" -eq 0 ]; then 
  180.         log_success_msg
  181.         touch /var/lock/subsys/${NAME}
  182.     else
  183.         log_failure_msg
  184.     fi
  185.     #echo
  186.     return $RETVAL
  187. }
  188. function status() {
  189.     RETVAL="1"
  190.     if [ -f "/var/run/${NAME}.pid" ]; then
  191.         read kpid < /var/run/${NAME}.pid
  192.         if checkpid $kpid 2>&1; then
  193.             echo "$0 is already running (${kpid})"
  194.             RETVAL="0"
  195.         else
  196.             echo "lock file found but no process running for pid $kpid"
  197.         fi
  198.     else
  199.         pid="$(pgrep -u ${TOMCAT_USER} -G ${TOMCAT_USER} java)"
  200.         if [ -n "$pid" ]; then
  201.             echo "$0 running (${pid}) but no PID file exists"
  202.             RETVAL="0"
  203.         else
  204.             echo "$0 is stopped"
  205.         fi
  206.     fi
  207.     return $RETVAL
  208. }
  209. function stop() {
  210.     local STOP_VERBOSE="false"
  211.     echo -n "Stopping $TOMCAT_PROG: "
  212.     if [ -f "/var/lock/subsys/${NAME}" ]; then
  213.         $SU - $TOMCAT_USER -c "$TOMCAT_SCRIPT stop" >> $TOMCAT_LOG 2>&1
  214.         RETVAL="$?"
  215.         if [ "$RETVAL" -eq "0" ]; then
  216.             count="0"
  217.             if [ -f "/var/run/${NAME}.pid" ]; then
  218.                 read kpid < /var/run/${NAME}.pid
  219.                 until [ "$(ps --pid $kpid | grep -c $kpid)" -eq "0" ] || \
  220.                       [ "$count" -gt "$SHUTDOWN_WAIT" ]; do
  221.                     if [ "$STOP_VERBOSE" = "true" ]; then
  222.                         echo -n -e "\nwaiting for processes $kpid to exit"
  223.                     fi
  224.                     sleep 1
  225.                     let count="${count}+1"
  226.                 done
  227.                 if [ "$count" -gt "$SHUTDOWN_WAIT" ]; then
  228.                     if [ "$STOP_VERBOSE" = "true" ]; then
  229.                         echo -n -e "\nkilling processes which didn't stop"
  230.                         echo -n -e "after "
  231.                         echo -n "$SHUTDOWN_WAIT seconds"
  232.                     fi
  233.                     kill -9 $kpid
  234.                 fi
  235.                 if [ "$STOP_VERBOSE" = "true" -a "$count" -gt "0" ]; then
  236.                     echo -n -e "\n"
  237.                 fi
  238.                 log_success_msg
  239.             else
  240.                 log_failure_msg
  241.             fi
  242.             rm -f /var/lock/subsys/$NAME /var/run/$NAME.pid
  243.         else
  244.             log_failure_msg
  245.         fi
  246.     else
  247.         log_failure_msg
  248.     fi
  249.     #echo
  250. }
  251. # See how we were called.
  252. case "$1" in
  253.     start)
  254.         parseOptions
  255.         start
  256.         ;;
  257.     stop)
  258.         parseOptions
  259.         stop
  260.         ;;
  261.     restart)
  262.         parseOptions
  263.         stop
  264.         sleep 2    
  265.         start
  266.         ;;
  267.     condrestart|try-restart)
  268.         if [ -f "/var/run/${NAME}.pid" ]; then
  269.             parseOptions
  270.             stop
  271.             start
  272.         fi
  273.         ;;
  274.     reload)
  275.         RETVAL="3"
  276.         ;;
  277.     force-reload)
  278.         if [ -f "/var/run/${NAME}.pid" ]; then
  279.             stop
  280.             start
  281.         fi
  282.         ;;
  283.     status)
  284.         status
  285.         ;;
  286.     version)
  287.         parseOptions
  288.         "${JAVA_HOME}/bin/java" \
  289.             -classpath "${CATALINA_HOME}/server/lib/[tomcat5][catalina].jar" \
  290.             org.apache.catalina.util.ServerInfo
  291.         ;;
  292.     *)
  293.         echo "Usage: $TOMCAT_PROG {start|stop|restart|condrestart|try-restart|reload|force-reload|status|version}"
  294.         exit 1
  295. esac
  296. 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. #

猜你喜欢

转载自leitelyaya.iteye.com/blog/786192
今日推荐