Chinaunix首页 | 论坛 | 博客
  • 博客访问: 529585
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: Python/Ruby

2012-05-23 21:04:49


最最简单的tomcat init.d 脚本,有待日后继续完善。聊胜于无。记下先。

PS:关于判断是否已经运行,应该多注意一下 catalina.sh 中可选参数 CATALINA_PID 指出了保存pid的文件,可以借此来判断是否正在运行。

点击(此处)折叠或打开

  1. #!/bin/bash
  2. #
  3. # tomcat6 This shell script takes care of starting and stopping Tomcat
  4. #
  5. # chkconfig: - 80 20
  6. #
  7. ### BEGIN INIT INFO
  8. # Provides: tomcat6
  9. # Required-Start: $network $syslog
  10. # Required-Stop: $network $syslog
  11. # Default-Start:
  12. # Default-Stop:
  13. # Description: Release implementation for Servlet 2.5 and JSP 2.1
  14. # Short-Description: start and stop tomcat
  15. ### END INIT INFO

  16. ## Source function library.
  17. #. /etc/rc.d/init.d/functions


  18. NAME="$(basename $0)"
  19. if [ "${NAME:0:1}" = "S" -o "${NAME:0:1}" = "K" ]; then
  20.     NAME="${NAME:3}"
  21. fi

  22. export JAVA_HOME=/usr/java/default
  23. export JAVA_OPTS="-Dfile.encoding=UTF-8 \
  24. -XX:MaxPermSize=128m \
  25. -Xms512m -Xmx3072m"
  26. export PATH=$JAVA_HOME/bin:$PATH
  27. TOMCAT_HOME=/usr/share/$NAME
  28. TOMCAT_USER=tomcat
  29. SHUTDOWN_WAIT=20

  30. if [ -x "/sbin/runuser" ]; then
  31.     SU="/sbin/runuser -s /bin/sh"
  32. else
  33.     SU="/bin/su -s /bin/sh"
  34. fi

  35. start() {
  36.     echo "Starting ${NAME}"
  37.     $SU - $TOMCAT_USER -c "$TOMCAT_HOME/bin/catalina.sh start"
  38.     return 0
  39. }

  40. stop() {
  41.     echo "Stoping ${NAME}"
  42.     $SU - $TOMCAT_USER -c "$TOMCAT_HOME/bin/catalina.sh stop"
  43.     return 0
  44. }

  45. case $1 in
  46.   start)
  47.     start
  48.     ;;
  49.   stop)
  50.     stop
  51.     ;;
  52.   restart)
  53.     stop
  54.     start
  55.     ;;
  56.   *)
  57.     echo "Usage: $NAME {start | stop | restart}"
  58.     ;;
  59. esac
  60. exit 0


===================================== 2012/06/26 更新

找了个时间,整理总结了一下,给出了个稍微复杂点的例子,相信能够应付大部分需要使用的情形了。在实践中学习。同时给出了一个SVN的链接地址,可能会不定时的更新,造成与现在粘贴的代码不一致额。


    以指定的用户启动、停止给定的命令。并创建锁文件、PID文件、给定命令输入日志文件。

    主要用于与 chkconfig、 service 命令进行对接。因此该文件放在 /etc/init.d/ 目录下.


点击(此处)折叠或打开

  1. #!/bin/bash

  2. # chkconfig: 2345 60 60
  3. # description:
  4. #
  5. ### BEGIN INIT INFO
  6. # Provides: center
  7. # Required-Start: $network
  8. # Required-Stop: $network
  9. # Should-Start:
  10. # Should-Stop:
  11. # X-Start-Before:
  12. # X-Stop-After:
  13. # X-Interactive: false
  14. # Defalt-Start: 2 3 4 5
  15. # Default-Stop: 0 1 6
  16. # Short-Description:
  17. # Description:
  18. ### END INIT INFO
  19. #
  20. # NOTICE: This script should be ran with super user

  21. ########################### Common Init

  22. ## Source function library.
  23. #. /etc/rc.d/init.d/functions
  24. # Source LSB function library.
  25. if [ -r /lib/lsb/init-functions ]; then
  26.   . /lib/lsb/init-functions
  27. else
  28.   exit 1
  29. fi
  30. DISTRIB_ID=`lsb_release -i -s 2>/dev/null`

  31. # For SELinux we need to use 'runuser' not 'su'
  32. if [ -x "/sbin/runuser" ]; then
  33.   SU="/sbin/runuser -s /bin/sh"
  34. else
  35.   SU="/bin/su -s /bin/sh"
  36. fi


  37. ########################### Prepare
  38. NAME="$(basename $0)"
  39. if [ "${NAME:0:1}" = "S" -o "${NAME:0:1}" = "K" ]; then
  40.   NAME="${NAME:3}"
  41. fi

  42. ########################### Settings

  43. RUNNING_USER=$USER
  44. CMD_START="/path/to/app start"
  45. CMD_STOP="/path/to/app stop"
  46. LOCK_FILE="/var/lock/subsys/${NAME}"
  47. PID_FILE="/var/run/${NAME}.pid"
  48. LOG_FILE="/var/log/${NAME}.log"
  49. SHUTDOWN_WAIT=3

  50. ########################### ENV PASSED
  51. ENV_PASS="export PID_FILE='${PID_FILE}';"

  52. # when using tomcat, uncomment the following command
  53. # export CATALINA_PID=${PID_FILE}
  54. #ENV_PASS="export CATALINA_PID='${PID_FILE}'; ${ENV_PASS}"

  55. ########################### Functions

  56. function start() {
  57.   echo -n "Starting ${NAME}: "

  58. # is running ?
  59.   if [ -f "${LOCK_FILE}" ]; then
  60.     if [ -f "${PID_FILE}" ]; then
  61.       read kpid < "${LOCK_FILE}"
  62.       if [ -d "/proc/${kpid}" ]; then
  63.         log_success_msg
  64.         if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
  65.           echo
  66.         fi
  67.         return
  68.       fi
  69.     fi
  70.   fi

  71. # fix permissions on the log and pid files
  72.   touch "${PID_FILE}" 2>&1
  73.   RETVAL=$?
  74.   if [ "${RETVAL}" -ne "0" ]; then
  75.     log_failure_msg "Error creating pid file '${PID_FILE}'. ${RETVAL}"
  76.     return
  77.   fi

  78.   chown ${RUNNING_USER}:${RUNNING_USER} "${PID_FILE}"
  79.   RETVAL=$?
  80.   if [ "${RETVAL}" -ne "0" ]; then
  81.     log_failure_msg "Error chown pid file '${PID_FILE}' to usr '${RUNNING_USER}'. ${RETVAL}"
  82.     return
  83.   fi

  84.   touch "${LOG_FILE}" 2>&1
  85.   RETVAL=$?
  86.   if [ "${RETVAL}" -ne "0" ]; then
  87.     log_failure_msg "Error creating log file '${LOG_FILE}'. ${RETVAL}"
  88.     return
  89.   fi

  90.   chown ${RUNNING_USER}:${RUNNING_USER} "${LOG_FILE}"
  91.   RETVAL=$?
  92.   if [ "${RETVAL}" -ne "0" ]; then
  93.     log_failure_msg "Error chown log file '${LOG_FILE}' to usr '${RUNNING_USER}'. ${RETVAL}"
  94.     return
  95.   fi

  96.   $SU - ${RUNNING_USER} -c "${ENV_PASS} ${CMD_START} >> ${LOG_FILE} 2>&1"
  97.   #$SU - ${RUNNING_USER} -c "${ENV_PASS} ${CMD_START} "
  98.   RETVAL=$?
  99.   if [ "${RETVAL}" -eq "0" ]; then
  100.     log_success_msg
  101.     touch "${LOCK_FILE}"
  102.   else
  103.     log_failure_msg "Error executing command. ${RETVAL}"
  104.   fi

  105.   if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
  106.     echo
  107.   fi
  108. }

  109. function stop() {
  110.   echo -n "Stopping ${NAME}: "
  111.   if [ -f "${LOCK_FILE}" ]; then

  112. # stop command is empty? Then kill pid, and remove pid file
  113.     if [ -z "${CMD_STOP}" ]; then
  114.       if [ -f "${PID_FILE}" ]; then
  115.         read kpid < "${PID_FILE}"
  116.         if [ -n "${kpid}" ]; then
  117.           if [ -d "/proc/${kpid}" ]; then
  118.             kill ${kpid}
  119.           fi
  120.           log_success_msg
  121.         else
  122.           log_success_msg "PID file is empty, remove it"
  123.         fi
  124.         rm -f "${PID_FILE}"
  125.       else
  126.         log_failure_msg "PID file is not exists"
  127.       fi

  128.       if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
  129.         echo
  130.       fi

  131.     else
  132.       $SU - ${RUNNING_USER} -c "${ENV_PASS} ${CMD_STOP} >> ${LOG_FILE} 2>&1"
  133.       #$SU - ${RUNNING_USER} -c "${ENV_PASS} ${CMD_STOP}"
  134.     fi

  135. # If process is still running, wait some seconds and kill it.
  136.     if [ -f "${PID_FILE}" ]; then
  137.       read kpid < "${PID_FILE}"
  138.       if [ -n "${kpid}" ]; then
  139.         if [ -d "/proc/${kpid}" ]; then
  140.           count=0
  141.           until [ "$(ps --pid $kpid | grep -c $kpid)" -eq "0" ] || \
  142.                 [ "$count" -gt "$SHUTDOWN_WAIT" ]; do
  143.             sleep 1
  144.             let count="${count}+1"
  145.           done

  146.           if [ "$count" -gt "$SHUTDOWN_WAIT" ]; then
  147.             echo
  148.             log_warning_msg "killing processes which did not stop \
  149. after ${SHUTDOWN_WAIT} seconds"
  150.           fi

  151.           kill -9 $kpid
  152.         fi

  153.         log_success_msg

  154.       else
  155.         log_success_msg "PID file is empty, remove it"
  156.       fi
  157.       rm -f "${PID_FILE}" "${LOCK_FILE}"
  158.     else
  159.       log_success_msg
  160.     fi
  161.   else
  162.     log_success_msg
  163.   fi

  164.   if [ "$DISTRIB_ID" = "MandrivaLinux" ]; then
  165.     echo
  166.   fi

  167. }

  168. function status() {
  169.   if [ -f "${PID_FILE}" ]; then
  170.     read kpid < "${PID_FILE}"
  171.     if [ -n "${kpid}" ]; then
  172.       if [ -d "/proc/${kpid}" ]; then
  173.         log_success_msg "${NAME} (pid ${kpid}) is running..."
  174.       else
  175.         log_failure_msg "PID file exists, but process is not running"
  176.       fi
  177.     else
  178.       log_failure_msg "PID file exists, but could not read pid"
  179.     fi
  180.   else
  181.     log_success_msg "${NAME} is stopped"
  182.   fi
  183. }

  184. function usage() {
  185.   echo "Usage: $0 {start|stop|restart|status}"
  186. }


  187. ########################### Executing


  188. case "$1" in
  189.   start)
  190.     start
  191.     ;;
  192.   stop)
  193.     stop
  194.     ;;
  195.   restart)
  196.     stop
  197.     start
  198.     ;;
  199.   status)
  200.     status
  201.     ;;
  202.   *)
  203.     usage
  204.     ;;
  205. esac

   只是简单的启动,停止相应的程序,并且将pid保存到由 app.init.d 指定的PID文件中。

   该文件可以放在相应应用的根目录下。


点击(此处)折叠或打开

  1. #!/bin/bash

  2. NAME="$(basename $0)"

  3. WORK_DIR="$(cd "$(dirname "$0")" && pwd)"
  4. cd "${WORK_DIR}"

  5. JAVA_OPTS="
  6.   -XX:PermSize=512M
  7.   -XX:MaxPermSize=512M
  8.   -Xms1024M
  9.   -Xmx2048M
  10.   -Xss1024K
  11. "

  12. MAIN_CLASS=me.test.SampleMain
  13. XX_DIR=

  14. # resolv CLASSPATH
  15. for jar in lib/*.jar
  16. do
  17.   CLASSPATH=${CLASSPATH}:"${WORK_DIR}/${jar}"
  18. done
  19. CLASSPATH=.:./conf:${CLASSPATH}
  20. CLASSPATH="${WORK_DIR}/another.jar":${CLASSPATH}
  21. CLASSPATH="${XX_DIR}":${CLASSPATH}
  22. export CLASSPATH
  23. #echo "Using classpath: ${CLASSPATH}"

  24. # giving a default pid
  25. if [ -z "${PID_FILE}" ]; then
  26.   PID_FILE="${WORK_DIR}/${NAME}.pid"
  27. fi

  28. function start() {
  29.   #java ${JAVA_OPTS} ${MAIN_CLASS} &
  30.   sleep 1000&
  31.   PID=$!
  32.   echo ${PID} > ${PID_FILE}
  33. }

  34. function stop() {
  35.   #return;
  36.   if [[ -f "${PID_FILE}" ]]; then
  37.     read kpid < "${PID_FILE}"
  38.     if [ -n "${kpid}" ] && [ -d "/proc/${kpid}" ]; then
  39.       kill ${kpid}
  40.     fi
  41.     rm -f "${PID_FILE}"
  42.   fi
  43. }

  44. function usage() {
  45.   echo "Usage: $0 {start|stop}"
  46. }


  47. case "$1" in
  48.   start)
  49.     start
  50.     ;;
  51.   stop)
  52.     stop
  53.     ;;
  54.   restart)
  55.     stop
  56.     start
  57.     ;;
  58.   *)
  59.     usage
  60.     ;;
  61. esac




一个参考用的示例文件:

(参考用, 由于该文件内容太长,现在仅仅给出链接)
本文件是在CentOS 6.2 上使用Yum安装时生成的init.d脚本,仅供将来对上面的改造用。
(因为想在运行多个Tomcat实例,使用yum源安装的还不知道该怎样配置)



参考
Tomcat init.d script

《》
         --《10.2. Parameter Substitution》
阅读(1285) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~