#!bin/bash
#Tomcat部署脚本,提前将apache-tomcat-8.5.30.tar.gz、jdk-8u172-linux-x64.tar.gz上传至服务器的/tmp/目录下
#1.安装JDK
tar -zxf /tmp/jdk-8u172-linux-x64.tar.gz -C /usr/local/
ln -s /usr/local/jdk1.8.0_172/ /usr/local/jdk
#配置java环境
cat >> /etc/profile.d/java.sh <<EOF
export JAVA_HOME=/usr/local/jdk/
export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH
EOF
#注意此处的source /etc/profile,脚本运行完成后,需要退出再重新登录服务器加载/etc/profile。否则java环境失效
source /etc/profile
#2.安装Tomcat
tar -zxf /tmp/apache-tomcat-8.5.30.tar.gz -C /usr/local/
ln -s /usr/local/apache-tomcat-8.5.30/ /usr/local/tomcat
cd /usr/local/tomcat/
mkdir -pv /usr/local/tomcat_dir
mv conf/ logs/ temp/ webapps/ work/ /usr/local/tomcat_dir
#n为建立Tomcat实例的个数
n=7
for((i=1;i<=n;i++))
do
/usr/bin/mkdir -pv /usr/local/tomcat$i
/usr/bin/cp -a /usr/local/tomcat_dir/* /usr/local/tomcat$i
sed -i 's/Server port="8005"/Server port="708'$i'"/' /usr/local/tomcat$i/conf/server.xml
sed -i 's/Connector port="8080"/Connector port="808'$i'"/' /usr/local/tomcat$i/conf/server.xml
sed -i 's/Connector port="8009"/Connector port="908'$i'"/' /usr/local/tomcat$i/conf/server.xml
done
rm -rf /usr/local/tomcat_dir/
mkdir -pv /usr/local/tomcat_sh/
#注意下面的“\”,代表不转译。
cat > /usr/local/tomcat_sh/start_tomcat.sh <<EOF
#!/bin/bash
source /etc/profile
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
export CATALINA_HOME=/usr/local/tomcat
export CATALINA_BASE=\${1%/}
export log=\$CATALINA_BASE/startup.log
# use %/ is for next grep match
echo \$CATALINA_BASE
Tomcat_PID=\$(ps aux | grep "java" | grep -v "grep" | grep "Dcatalina.base=\${CATALINA_BASE}" |awk '{print \$2}')
if [ -n "\$Tomcat_PID" ];then
echo "The tomcat instance \$CATALINA_BASE is running,please checkout the status";
exit 1;
fi
/usr/bin/sh \$CATALINA_HOME/bin/startup.sh > \$log 2>&1
if [ "\$?" = 0 ];then
echo "The tomcat instance \$CATALINA_BASE start succeed!!!";
else
echo "The tomcat instance \$CATALINA_BASE start faild!!!"
tail -f \$log
fi
EOF
cat > /usr/local/tomcat_sh/stop_tomcat.sh <<EOF
#!/bin/bash
source /etc/profile
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
export CATALINA_HOME=/usr/local/tomcat
export CATALINA_BASE=\${1%/}
export log=\$CATALINA_BASE/stop.log
# use %/ is for next grep match
echo \$CATALINA_BASE
Tomcat_PID=\$(ps aux | grep "java" | grep -v "grep" | grep "Dcatalina.base=\${CATALINA_BASE}" |awk '{print \$2}')
if [ -n "\$Tomcat_PID" ];then
/usr/bin/sh \$CATALINA_HOME/bin/shutdown.sh > \$log 2>&1
else
echo "The tomcat instance \$CATALINA_BASE is not running,please checkout the status"
exit 1;
fi
if [ "\$?" = 0 ];then
echo "The tomcat instance \$CATALINA_BASE stop succeed!!!";
else
echo "The tomcat instance \$CATALINA_BASE stop faild!!!"
tail -f \$log
fi
EOF
chmod +x -R /usr/local/tomcat_sh/
原文地址:http://blog.51cto.com/hbgslz/2120748