现在的位置: 首页 > 综合 > 正文

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

2012年03月18日 综合 ⁄ 共 2837字 ⁄ 字号 评论 1 条

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

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

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

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

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

把脚本复制到/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

#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

目前有 1 条留言    访客:1 条, 博主:0 条

  1. Marion 2012年03月27日 上午 6:19  @回复  Δ1楼 回复

    Where exactly is the facebook like link ?

给我留言

留言无头像?