|
Linux下tomcat服务启动/停止/重启脚本
适用情况:有多个tomcat同时使用,监听不同的端口,如在/usr/local下有多个tomcat目录名字依次是tomcat1、tomcat2、tomcat3....tomcat12
使用service tomcat 的方式启动停止tomcat服务,使用标签代表一个或几个tomcat服务,如:
boofm 代表 tomcat1、tomcat2、tomcat3,这样启动的时候就可以这么启动
- service tomcat bookfm start
all 代表所有的tomcat,这样启动的时候就可以这么启动
把脚本复制到/etc/init.d/下,命名为tomcat,就可以实现service 的方式来启动tomcat服务
效果如下:
使用方式:service tomcat xxx start|stop|restart|status
- [root@server1 ~]# service tomcat bookfm start
- tomcat1 starting,Please wait 2s...
- tomcat2 starting,Please wait 2s...
- tomcat3 starting,Please wait 2s...
- [root@server1 ~]#
- [root@server1 ~]# service tomcat bookfm stop
- tomcat1 stopping,Please wait 2s...
- tomcat2 stopping,Please wait 2s...
- tomcat3 stopping,Please wait 2s...
- [root@server1 ~]#
- [root@server1 ~]# service tomcat bookfm restart
- tomcat1 is not running...
- tomcat1 starting,Please wait 2s...
- tomcat2 is not running...
- tomcat2 starting,Please wait 2s...
- tomcat3 is not running...
- tomcat3 starting,Please wait 2s...
- [root@server1 ~]# service tomcat bookfm status
- tomcat1 is running
- tomcat2 is running
- tomcat3 is running
- [root@server1 ~]#
第二版
- #!/bin/bash
-
- # Apache Tomcat daemon
- #
- # chkconfig: 345 10 10
- # description: Apache Tomcat daemon
- #
- # processname: tomcat
-
- export JAVA_HOME=/usr/local/java
-
- JAVA_RUN="/usr/local/java/bin/java"
-
- usage="{bookfm|edumh|all} {start|stop|restart|status}"
-
- JAVA_OPTS=""
- #JAVA_OPTS="-server -Xms512m -Xmx512m \
- # -XX:PermSize=16m -XX:MaxPermSize=64m -XX:MaxNewSize=64m \
- # -XX:-UseGCOverheadLimit -XX:+UseConcMarkSweepGC"
-
- #judge $1 $2 whether null
- if [ "$1" == "" -o "$2" == "" ];then
- echo
- echo "Usage: $0 $usage"
- echo
- exit 1
- fi
-
- #judge $1
- case $1 in
- "bookfm")
- tomcats="1 2 3"
- ;;
- "edumh")
- tomcats="12"
- ;;
- "all")
- tomcats="1 2 3 12"
- ;;
- *)
- echo "Usage: $0 $usage"
- ;;
- esac
-
- tab=$1
-
- #define start function
- tomcatstart() {
- for i in $tomcats
- do
- TOMCAT="tomcat$i"
- run_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties")
- LOGGING_CONFIG="-Djava.util.logging.config.file=/usr/local/$TOMCAT/conf/logging.properties \
- -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
- JAVA_ENDORSED_DIRS="-Djava.endorsed.dirs=/usr/local/$TOMCAT/common/endorsed"
- CLASSPATH="-classpath :/usr/local/$TOMCAT/bin/bootstrap.jar:/usr/local/$TOMCAT/bin/commons-logging-api.jar"
- CATALINA_BASE="-Dcatalina.base=/usr/local/$TOMCAT \
- -Dcatalina.home=/usr/local/$TOMCAT"
- CATALINA_TMPDIR="-Djava.io.tmpdir=/usr/local/$TOMCAT/temp"
- CATALINA_OUT="/usr/local/$TOMCAT/logs/catalina.out"
- cd /usr/local/$TOMCAT
- if [ "${run_status}X" != "X" ];then
- echo "$tab==>$TOMCAT is already running..."
- else
- $JAVA_RUN $JAVA_OPTS $LOGGING_CONFIG $JAVA_ENDORSED_DIRS $CATALINA_BASE $CATALINA_TMPDIR $CLASSPATH \
- org.apache.catalina.startup.Bootstrap start > $CATALINA_OUT 2>&1 &
- echo $! > /tmp/$TOMCAT.pid
- echo "$tab==>$TOMCAT starting,Please wait 2s..."
- sleep 2
- fi
- done
- }
-
- #define stop function
- tomcatstop() {
- for j in $tomcats
- do
- TOMCAT="tomcat$j"
- run1_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties")
- if [ "${run1_status}X" == "X" ];then
- echo "$TOMCAT is not running..."
- else
- kill -9 `cat /tmp/$TOMCAT.pid`
- echo "$TOMCAT stopping,Please wait 2s..."
- sleep 2
- fi
- done
- }
-
- #define restart function
- tomcatrestart() {
-
- for m in $tomcats
- do
- TOMCAT="tomcat$m"
- run2_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties")
- LOGGING_CONFIG="-Djava.util.logging.config.file=/usr/local/$TOMCAT/conf/logging.properties \
- -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
- JAVA_ENDORSED_DIRS="-Djava.endorsed.dirs=/usr/local/$TOMCAT/common/endorsed"
- CLASSPATH="-classpath :/usr/local/$TOMCAT/bin/bootstrap.jar:/usr/local/$TOMCAT/bin/commons-logging-api.jar"
- CATALINA_BASE="-Dcatalina.base=/usr/local/$TOMCAT \
- -Dcatalina.home=/usr/local/$TOMCAT"
- CATALINA_TMPDIR="-Djava.io.tmpdir=/usr/local/$TOMCAT/temp"
- CATALINA_OUT="/usr/local/$TOMCAT/logs/catalina.out"
-
-
- if [ "${run2_status}X" == "X" ];then
- echo "$tab==>$TOMCAT is not running..."
- $JAVA_RUN $JAVA_OPTS $LOGGING_CONFIG $JAVA_ENDORSED_DIRS $CATALINA_BASE $CATALINA_TMPDIR $CLASSPATH \
- org.apache.catalina.startup.Bootstrap start > $CATALINA_OUT 2>&1 &
- echo $! > /tmp/$TOMCAT.pid
- echo "$tab==>$TOMCAT starting,Please wait 2s..."
- sleep 2
- else
- cd /usr/local/$TOMCAT
- kill -9 `cat /tmp/$TOMCAT.pid`
- echo "$TOMCAT stopping,Please wait 2s..."
- sleep 2
- $JAVA_RUN $JAVA_OPTS $LOGGING_CONFIG $JAVA_ENDORSED_DIRS $CATALINA_BASE $CATALINA_TMPDIR $CLASSPATH \
- org.apache.catalina.startup.Bootstrap start > $CATALINA_OUT 2>&1 &
- echo $! > /tmp/$TOMCAT.pid
- echo "$TOMCAT starting,Please wait 2s..."
- sleep 2
- fi
- done
- }
-
- #define status function
- tomcatstatus() {
- for n in $tomcats
- do
- TOMCAT="tomcat$n"
- run3_status=$(ps -ef | grep -v 'grep' | egrep "$TOMCAT/conf/logging.properties")
- if [ "${run3_status}X" == "X" ];then
- echo "$tab==>$TOMCAT is not running..."
- else
- echo "$tab==>$TOMCAT is running"
- fi
- done
- }
-
- #judge $2
- case $2 in
- "start")
- tomcatstart
- ;;
- "stop")
- tomcatstop
- ;;
- "restart")
- tomcatrestart
- ;;
- "status")
- tomcatstatus
- ;;
- *)
- echo "Usage: $0 $usage"
- ;;
- esac
第一版
脚本内容如下:
- #!/bin/bash
-
- # Apache Tomcat daemon
- #
- # chkconfig: 345 10 10
- # description: Apache Tomcat daemon
- #
- # processname: tomcat
-
- export JAVA_HOME=/usr/local/java
-
- #define variables
- tom="/usr/local/tomcat"
- startup_bin="bin/startup.sh"
- shutdown_bin="bin/shutdown.sh"
- usage="{bookfm|edumh|all} {start|stop|restart|status}"
- dev="/dev/null"
-
- #judge $1 $2 whether null
- if [ "$1" == "" -o "$2" == "" ];then
- echo
- echo "Usage: $0 $usage"
- echo
- exit 1
- fi
-
- #judge $1
- case $1 in
- "bookfm")
- tomcats="1 2 3"
- ;;
- "edumh")
- tomcats="12"
- ;;
- "all")
- tomcats="1 2 3 12"
- ;;
- *)
- echo "Usage: $0 $usage"
- ;;
- esac
-
- #define start function
- tomcatstart() {
- for i in $tomcats
- do
- tom_home="$tom$i"
- run_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom_home}")
- if [ "${run_status}X" != "X" ];then
- echo "tomcat$i is already running..."
- else
- ${tom_home}/${startup_bin} &>$dev
- echo "tomcat$i starting,Please wait 2s..."
- sleep 2
- fi
- done
- }
-
- #define stop function
- tomcatstop() {
- for j in $tomcats
- do
- tom1_home="$tom$j"
- run1_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom1_home}")
- if [ "${run1_status}X" == "X" ];then
- echo "tomcat$j is not running..."
- else
- ${tom1_home}/${shutdown_bin} &>$dev
- echo "tomcat$j stopping,Please wait 2s..."
- sleep 2
- fi
- done
- }
-
- #define restart function
- tomcatrestart() {
-
- for m in $tomcats
- do
- tom2_home="$tom$m"
- run2_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom2_home}")
- if [ "${run2_status}X" == "X" ];then
- echo "tomcat$m is not running..."
- ${tom2_home}/${startup_bin} &>$dev
- echo "tomcat$m starting,Please wait 2s..."
- sleep 2
- else
- ${tom2_home}/${shutdown_bin} &>$dev
- echo "tomcat$m stopping,Please wait 2s..."
- sleep 2
- ${tom2_home}/${startup_bin} &>$dev
- echo "tomcat$m starting,Please wait 2s..."
- sleep 2
- fi
- done
- }
-
- #define status function
- tomcatstatus() {
- for n in $tomcats
- do
- tom3_home="$tom$n"
- run3_status=$(ps -ef | grep -v 'grep' | egrep "java.*=${tom3_home}")
- if [ "${run3_status}X" == "X" ];then
- echo "tomcat$n is not running..."
- else
- echo "tomcat$n is running"
- fi
- done
- }
-
- #judge $2
- case $2 in
- "start")
- tomcatstart
- ;;
- "stop")
- tomcatstop
- ;;
- "restart")
- tomcatrestart
- ;;
- "status")
- tomcatstatus
- ;;
- *)
- echo "Usage: $0 $usage"
- ;;
- esac
|
|
|