我这里遇到的这个情况有点不同,首先我的tomcat 是用 tomcat 用户启动的,然后呢,我需要启动的时候防火墙上将 9090 端口和 28036 端口先给封住,不然多人连了,会load 很高,话不多说上脚本,只要你机器装了 expect 就可以运行了,直接用 expect方式让它切root 用户来执行,哈哈
#!/bin/bash
function iptables_reject(){
/bin/expect <<EOF
spawn /bin/su root
expect "*Password:" {send "$paswd\r"}
send "iptables -I INPUT -p tcp --dport 9090 -j REJECT\r"
send "iptables -I INPUT -p tcp --dport 28036 -j REJECT\r"
send "exit\r"
interact
expect eof
EOF
}
function iptables_restart(){
/bin/expect <<EOF
spawn /bin/su root
expect "*Password:" {send "$paswd\r"}
send "systemctl restart iptables\r"
send "exit\r"
interact
expect eof
EOF
}
##############################################################################
. /etc/init.d/functions
source /etc/profile
tomcat_start="/home/tomcat/apache-tomcat8/bin/startup.sh"
paswd=gxbw6666
count=ps -ef | grep tomcat8 | grep -v grep| wc -l
#echo $count
if [ $count == 0 ];then
iptables_reject;
${tomcat_start}
sleep 50s
iptables_restart;
echo "qi dong ok le"
else
echo "tomcat up"
fi