之前在windows上面写了一篇windows下一个tomcat版本创建多个实例,又在linux上面试了下。
首先必须配置java环境变量以及tomcat的环境变量CATALINA_HOME=/usr/local/tomcat
配置环境变量的方法这里就省去了,网上很多
配置方法是
vi /etc/profile
在最下面加入如下
export JAVA_HOME=/usr/local/java/jdk1.7.0_51 export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME PATH CLASSPATH export CATALINA_HOME=/usr/local/tomcat
使配置立即生效
source /etc/profile
我的tomcat路径为:/usr/local/tomcat
10个tomcat路径为:/usr/local/tomcatInstance
复制tomcat下的conf和logs文件夹既可,你要是不用单个webapps就需要把webapps复制过去就可以,当然一般就是需要负载均衡用的,直接复制就可以了
修改每个实例文件夹下的/usr/local/tomcatInstance/tomcat1/conf/server.xml的三个端口号
<Server port="8005" shutdown="SHUTDOWN"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
每个10个数据就行例如第一个
<Server port="8015" shutdown="SHUTDOWN"> <Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8019" protocol="AJP/1.3" redirectPort="8443" />
每个都配置好以后下面来写启动脚本都放在实例的根目录tomcat[n],[n]为数字1-10
启动脚本为startup.sh
#! /bin/sh CATALINA_BASE=`pwd` sh "$CATALINA_HOME"/bin/startup.sh
关闭脚本为shutdown.sh
#! /bin/sh CATALINA_BASE=`pwd` sh "$CATALINA_HOME"/bin/shutdown.sh
当然关闭脚本可以这样写kill.sh
#! /bin/sh tomcatInstance_path=`pwd` pid=`ps -ef|grep java|grep $tomcatInstance_path |awk '{print $2}'` echo "pid=[$pid]" while [ -n "$pid" ]; do echo "kill $pid" kill -9 $pid sleep 1s pid=`ps -ef|grep java|grep $tomcatInstance_path |awk '{print $2}'` done
下面在来写restart.sh脚本,这里关闭是直接调用的kill.sh,这样可以完全确定关闭
#! /bin/sh tomcatInstance_path=`pwd` #sh kill.sh or shutdown.sh sh kill.sh #删除所有日志文件,和工作文件 rm -rf logs/* rm -rf work/* sh startup.sh ps -ef|grep java|grep $tomcatInstance_path
将这四个文件放在tomcat的十个实例文件夹根目录中。
由于脚本中使用了pwd,所以必须cd到当前目录,而不能直接从其他目录去执行
然后再在/usr/local/tomcatInstance下面创建四个脚本分别为
启动所有实例startupall.sh
tomcatInstance_path=`pwd` cd $tomcatInstance_path for dir in `ls` do if [ -d $dir ] then cd "$tomcatInstance_path/$dir" echo "$tomcatInstance_path/$dir" sh startup.sh cd "$tomcatInstance_path" fi done
停止所有实例shutdownall.sh
tomcatInstance_path=`pwd` cd $tomcatInstance_path for dir in `ls` do if [ -d $dir ] then cd "$tomcatInstance_path/$dir" echo "$tomcatInstance_path/$dir" sh shutdown.sh cd "$tomcatInstance_path" fi done
杀死所有实例killall.sh
tomcatInstance_path=`pwd` cd $tomcatInstance_path for dir in `ls` do if [ -d $dir ] then cd "$tomcatInstance_path/$dir" echo "$tomcatInstance_path/$dir" sh kill.sh cd "$tomcatInstance_path" fi done
重启所有实例restartall.sh
#! /bin/sh tomcatInstance_path=`pwd` cd $tomcatInstance_path for dir in `ls` do if [ -d $dir ] then cd "$tomcatInstance_path/$dir" echo "$tomcatInstance_path/$dir" sh restart.sh cd "$tomcatInstance_path" fi done
本来想加服务的,明天晚上再加上开机启动这块。服务这块不太熟悉,明天研究下,晚了,睡觉了。