手机版
你好,游客 登录 注册
背景:
阅读新闻

Tomcat服务 启动/停止/重启脚本

[日期:2012-11-29] 来源:Linux社区  作者:linux5588 [字体:大 中 小]

Linux下tomcat服务启动/停止/重启脚本

适用情况:有多个tomcat同时使用,监听不同的端口,如在/usr/local下有多个tomcat目录名字依次是tomcat1、tomcat2、tomcat3....tomcat12

使用service tomcat 的方式启动停止tomcat服务,使用标签代表一个或几个tomcat服务,如:

boofm 代表 tomcat1、tomcat2、tomcat3,这样启动的时候就可以这么启动

  1. service tomcat bookfm start

all 代表所有的tomcat,这样启动的时候就可以这么启动

  1. service tomcat all start

把脚本复制到/etc/init.d/下,命名为tomcat,就可以实现service 的方式来启动tomcat服务

效果如下:

使用方式:service tomcat xxx start|stop|restart|status

  1. [root@server1 ~]# service tomcat bookfm start
  2. tomcat1 starting,Please wait 2s...
  3. tomcat2 starting,Please wait 2s...
  4. tomcat3 starting,Please wait 2s...
  5. [root@server1 ~]#
  1. [root@server1 ~]# service tomcat bookfm stop
  2. tomcat1 stopping,Please wait 2s...
  3. tomcat2 stopping,Please wait 2s...
  4. tomcat3 stopping,Please wait 2s...
  5. [root@server1 ~]#
  1. [root@server1 ~]# service tomcat bookfm restart
  2. tomcat1 is not running...
  3. tomcat1 starting,Please wait 2s...
  4. tomcat2 is not running...
  5. tomcat2 starting,Please wait 2s...
  6. tomcat3 is not running...
  7. tomcat3 starting,Please wait 2s...
  1. [root@server1 ~]# service tomcat bookfm status
  2. tomcat1 is running
  3. tomcat2 is running
  4. tomcat3 is running
  5. [root@server1 ~]#

第二版

  1. #!/bin/bash
  2. # Apache Tomcat daemon
  3. #
  4. # chkconfig: 345 10 10
  5. # description: Apache Tomcat daemon
  6. #
  7. # processname: tomcat
  8. export Java_HOME=/usr/local/java
  9. JAVA_RUN="/usr/local/java/bin/java"
  10. usage="{bookfm|edumh|all} {start|stop|restart|status}"
  11. JAVA_OPTS=""
  12. #JAVA_OPTS="-server -Xms512m -Xmx512m \
  13. # -XX:PermSize=16m -XX:MaxPermSize=64m -XX:MaxNewSize=64m \
  14. # -XX:-UseGCOverheadLimit -XX:+UseConcMarkSweepGC"
  15. #judge $1 $2 whether null
  16. if [ "$1" == "" -o "$2" == "" ];then
  17. echo
  18. echo "Usage: $0 $usage"
  19. echo
  20. exit 1
  21. fi
  22. #judge $1
  23. case $1 in
  24. "bookfm")
  25. tomcats="1 2 3"
  26. ;;
  27. "edumh")
  28. tomcats="12"
  29. ;;
  30. "all")
  31. tomcats="1 2 3 12"
  32. ;;
  33. *)
  34. echo "Usage: $0 $usage"
  35. ;;
  36. esac
  37. tab=$1
  38. #define start function
  39. tomcatstart() {
  40. for i in $tomcats
  41. do
  42. TOMCAT="tomcat$i"
  43. run_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties")
  44. LOGGING_CONFIG="-Djava.util.logging.config.file=/usr/local/$TOMCAT/conf/logging.properties \
  45. -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
  46. JAVA_ENDORSED_DIRS="-Djava.endorsed.dirs=/usr/local/$TOMCAT/common/endorsed"
  47. CLASSPATH="-classpath :/usr/local/$TOMCAT/bin/bootstrap.jar:/usr/local/$TOMCAT/bin/commons-logging-api.jar"
  48. CATALINA_BASE="-Dcatalina.base=/usr/local/$TOMCAT \
  49. -Dcatalina.home=/usr/local/$TOMCAT"
  50. CATALINA_TMPDIR="-Djava.io.tmpdir=/usr/local/$TOMCAT/temp"
  51. CATALINA_OUT="/usr/local/$TOMCAT/logs/catalina.out"
  52. cd /usr/local/$TOMCAT
  53. if [ "${run_status}X" != "X" ];then
  54. echo "$tab==>$TOMCAT is already running..."
  55. else
  56. $JAVA_RUN $JAVA_OPTS $LOGGING_CONFIG $JAVA_ENDORSED_DIRS $CATALINA_BASE $CATALINA_TMPDIR $CLASSPATH \
  57. org.apache.catalina.startup.Bootstrap start > $CATALINA_OUT 2>&1 &
  58. echo $! > /tmp/$TOMCAT.pid
  59. echo "$tab==>$TOMCAT starting,Please wait 2s..."
  60. sleep 2
  61. fi
  62. done
  63. }
  64. #define stop function
  65. tomcatstop() {
  66. for j in $tomcats
  67. do
  68. TOMCAT="tomcat$j"
  69. run1_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties")
  70. if [ "${run1_status}X" == "X" ];then
  71. echo "$TOMCAT is not running..."
  72. else
  73. kill -9 `cat /tmp/$TOMCAT.pid`
  74. echo "$TOMCAT stopping,Please wait 2s..."
  75. sleep 2
  76. fi
  77. done
  78. }
  79. #define restart function
  80. tomcatrestart() {
  81. for m in $tomcats
  82. do
  83. TOMCAT="tomcat$m"
  84. run2_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties")
  85. LOGGING_CONFIG="-Djava.util.logging.config.file=/usr/local/$TOMCAT/conf/logging.properties \
  86. -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
  87. JAVA_ENDORSED_DIRS="-Djava.endorsed.dirs=/usr/local/$TOMCAT/common/endorsed"
  88. CLASSPATH="-classpath :/usr/local/$TOMCAT/bin/bootstrap.jar:/usr/local/$TOMCAT/bin/commons-logging-api.jar"
  89. CATALINA_BASE="-Dcatalina.base=/usr/local/$TOMCAT \
  90. -Dcatalina.home=/usr/local/$TOMCAT"
  91. CATALINA_TMPDIR="-Djava.io.tmpdir=/usr/local/$TOMCAT/temp"
  92. CATALINA_OUT="/usr/local/$TOMCAT/logs/catalina.out"
  93. if [ "${run2_status}X" == "X" ];then
  94. echo "$tab==>$TOMCAT is not running..."
  95. $JAVA_RUN $JAVA_OPTS $LOGGING_CONFIG $JAVA_ENDORSED_DIRS $CATALINA_BASE $CATALINA_TMPDIR $CLASSPATH \
  96. org.apache.catalina.startup.Bootstrap start > $CATALINA_OUT 2>&1 &
  97. echo $! > /tmp/$TOMCAT.pid
  98. echo "$tab==>$TOMCAT starting,Please wait 2s..."
  99. sleep 2
  100. else
  101. cd /usr/local/$TOMCAT
  102. kill -9 `cat /tmp/$TOMCAT.pid`
  103. echo "$TOMCAT stopping,Please wait 2s..."
  104. sleep 2
  105. $JAVA_RUN $JAVA_OPTS $LOGGING_CONFIG $JAVA_ENDORSED_DIRS $CATALINA_BASE $CATALINA_TMPDIR $CLASSPATH \
  106. org.apache.catalina.startup.Bootstrap start > $CATALINA_OUT 2>&1 &
  107. echo $! > /tmp/$TOMCAT.pid
  108. echo "$TOMCAT starting,Please wait 2s..."
  109. sleep 2
  110. fi
  111. done
  112. }
  113. #define status function
  114. tomcatstatus() {
  115. for n in $tomcats
  116. do
  117. TOMCAT="tomcat$n"
  118. run3_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties")
  119. if [ "${run3_status}X" == "X" ];then
  120. echo "$tab==>$TOMCAT is not running..."
  121. else
  122. echo "$tab==>$TOMCAT is running"
  123. fi
  124. done
  125. }
  126. #judge $2
  127. case $2 in
  128. "start")
  129. tomcatstart
  130. ;;
  131. "stop")
  132. tomcatstop
  133. ;;
  134. "restart")
  135. tomcatrestart
  136. ;;
  137. "status")
  138. tomcatstatus
  139. ;;
  140. *)
  141. echo "Usage: $0 $usage"
  142. ;;
  143. esac

 

第一版

脚本内容如下:

  1. #!/bin/bash
  2. # Apache Tomcat daemon
  3. #
  4. # chkconfig: 345 10 10
  5. # description: Apache Tomcat daemon
  6. #
  7. # processname: tomcat
  8. export JAVA_HOME=/usr/local/java
  9. #define variables
  10. tom="/usr/local/tomcat"
  11. startup_bin="bin/startup.sh"
  12. shutdown_bin="bin/shutdown.sh"
  13. usage="{bookfm|edumh|all} {start|stop|restart|status}"
  14. dev="/dev/null"
  15. #judge $1 $2 whether null
  16. if [ "$1" == "" -o "$2" == "" ];then
  17. echo
  18. echo "Usage: $0 $usage"
  19. echo
  20. exit 1
  21. fi
  22. #judge $1
  23. case $1 in
  24. "bookfm")
  25. tomcats="1 2 3"
  26. ;;
  27. "edumh")
  28. tomcats="12"
  29. ;;
  30. "all")
  31. tomcats="1 2 3 12"
  32. ;;
  33. *)
  34. echo "Usage: $0 $usage"
  35. ;;
  36. esac
  37. #define start function
  38. tomcatstart() {
  39. for i in $tomcats
  40. do
  41. tom_home="$tom$i"
  42. run_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom_home}")
  43. if [ "${run_status}X" != "X" ];then
  44. echo "tomcat$i is already running..."
  45. else
  46. ${tom_home}/${startup_bin} &>$dev
  47. echo "tomcat$i starting,Please wait 2s..."
  48. sleep 2
  49. fi
  50. done
  51. }
  52. #define stop function
  53. tomcatstop() {
  54. for j in $tomcats
  55. do
  56. tom1_home="$tom$j"
  57. run1_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom1_home}")
  58. if [ "${run1_status}X" == "X" ];then
  59. echo "tomcat$j is not running..."
  60. else
  61. ${tom1_home}/${shutdown_bin} &>$dev
  62. echo "tomcat$j stopping,Please wait 2s..."
  63. sleep 2
  64. fi
  65. done
  66. }
  67. #define restart function
  68. tomcatrestart() {
  69. for m in $tomcats
  70. do
  71. tom2_home="$tom$m"
  72. run2_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom2_home}")
  73. if [ "${run2_status}X" == "X" ];then
  74. echo "tomcat$m is not running..."
  75. ${tom2_home}/${startup_bin} &>$dev
  76. echo "tomcat$m starting,Please wait 2s..."
  77. sleep 2
  78. else
  79. ${tom2_home}/${shutdown_bin} &>$dev
  80. echo "tomcat$m stopping,Please wait 2s..."
  81. sleep 2
  82. ${tom2_home}/${startup_bin} &>$dev
  83. echo "tomcat$m starting,Please wait 2s..."
  84. sleep 2
  85. fi
  86. done
  87. }
  88. #define status function
  89. tomcatstatus() {
  90. for n in $tomcats
  91. do
  92. tom3_home="$tom$n"
  93. run3_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom3_home}")
  94. if [ "${run3_status}X" == "X" ];then
  95. echo "tomcat$n is not running..."
  96. else
  97. echo "tomcat$n is running"
  98. fi
  99. done
  100. }
  101. #judge $2
  102. case $2 in
  103. "start")
  104. tomcatstart
  105. ;;
  106. "stop")
  107. tomcatstop
  108. ;;
  109. "restart")
  110. tomcatrestart
  111. ;;
  112. "status")
  113. tomcatstatus
  114. ;;
  115. *)
  116. echo "Usage: $0 $usage"
  117. ;;
  118. esac
linux
相关资讯       Tomcat  Tomcat重启脚本  Tomcat重启 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款