while true 死循环判断端口按顺序启动应用

Posted dev0ps

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了while true 死循环判断端口按顺序启动应用相关的知识,希望对你有一定的参考价值。

需求:spring微服务应用启动较慢并且要求一个应用启完才能启第二个应用。

思路:加了个while true 死循环判断端口启动了才启下一个应用。

执行方式:/appupgrade/spring_cloud/pay.sh xxx

#!/bin/bash
export JAVA_HOME=/usr/java/jdk1.8.0_111
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
BUCKUP_DIR=/fs01/backup/spring-cloud/pay/$(date +%Y%m%d%H%M)
PAYPRE_DIR=/fs01/spring-cloud/paypre
APPLY_DIR=/fs01/spring-cloud/pay/acc-apply
SETTLE_DIR=/fs01/spring-cloud/pay/acc-settle
FILE=$1
if [ ! -f ${APPLY_DIR}/${FILE} ];then
echo "/tmp1/${FILE} This directory does not exist.Please check it!"
exit 0     
fi
echo "开始更新应用,请勿操作等待更新完毕!!"
ps aux|grep pay-front|grep -v grep |awk ‘{print $2}‘|xargs kill -9
rm -fr ${PAYPRE_DIR}/app.pid
sleep 10
ps aux|grep acc-apply|grep -v grep |awk ‘{print $2}‘|xargs kill -9
rm -fr ${APPLY_DIR}/app.pid
sleep 3
ps aux|grep acc-settle|grep -v grep |awk ‘{print $2}‘|xargs kill -9
rm -fr ${SETTLE_DIR}/app.pid

/bin/cp /tmp1/$1 ${APPLY_DIR}
[ -z ${BUCKUP_DIR} ] || /bin/mkdir -p ${BUCKUP_DIR} 
cd ${APPLY_DIR}
tar -cf ${BUCKUP_DIR}/apply.tar BOOT-INF/ META-INF/
cd ${SETTLE_DIR}
tar -cf ${BUCKUP_DIR}/settle.tar BOOT-INF/ META-INF/
#更新acc-apply
cd ${APPLY_DIR}/BOOT-INF/classes/ 
/bin/mv bootstrap.yml db.properties  paylink.properties ../
cd ${APPLY_DIR}
/usr/java/jdk1.8.0_111/bin/jar -xf ${APPLY_DIR}/${FILE}
cd ${APPLY_DIR}/BOOT-INF/classes/ 
/bin/mv ../bootstrap.yml ../db.properties ../paylink.properties ./ 
/bin/chown -R tomcat8:tomcatg /fs01/spring-cloud/pay/acc-apply
cd /fs01/spring-cloud/pay/acc-apply/ && ./start.sh
sleep 3
while true
do
	ss -lntp|grep 8761 >>/dev/null
	if [ $? = 0 ];then
	echo "acc-apply更新成功!!"
	break;
	fi
done
#更新acc-settle
/bin/cp ${APPLY_DIR}/${FILE} ${SETTLE_DIR}
cd ${SETTLE_DIR}/BOOT-INF/classes/             
/bin/mv bootstrap.yml db.properties paylink.properties ../
cd ${SETTLE_DIR}
/usr/java/jdk1.8.0_111/bin/jar -xf ${SETTLE_DIR}/${FILE}
cd ${SETTLE_DIR}/BOOT-INF/classes/             
/bin/mv ../bootstrap.yml ../db.properties ../paylink.properties ./
/bin/chown -R tomcat8:tomcatg /fs01/spring-cloud/pay/acc-settle
while true
do
	ss -lntp|grep 8761 >>/dev/null
	if [ $? = 0 ];then
cd /fs01/spring-cloud/pay/acc-settle/ && ./start.sh
	break;
	fi
done
while true
do
	ss -lntp|grep 8762 >>/dev/null
	if [ $? = 0 ];then
	echo "acc-settle更新成功!!"
cd /fs01/spring-cloud/paypre/ && ./start.sh
	break;
	fi
done
while true
do
	ss -lntp|grep 8763 >>/dev/null
	if [ $? = 0 ];then
echo "全部更新完毕,请检查应用日志是否正常!!"
	break;
	fi
done
mv /tmp1/${FILE} /tmp1/${FILE}_$(date +%Y%m%d%H%M)

 

以上是关于while true 死循环判断端口按顺序启动应用的主要内容,如果未能解决你的问题,请参考以下文章

for,while,do while语句区别以及常见死循环格式

Python基础流程控制-while循环

第十节,While循环

关于死循环while(true){}或for(;;){}的总结

关于死循环while(true){}或for(;;){}的总结

While循环语句#yyds干货盘点#