解放双手之liunx的shell编程:(六)利用shell脚本一键启动服务

微服务需要启动的服务很多,当服务器关闭后需要重启,十几个服务逐个启动真的是很烦,浪费我宝贵的时间,因此我编写了一个可以一键启动服务的shell文件来方便启动服务。

初级代码:

#!/bin/bash
cd /mpjava/server/ly.nfrzzl.core.biz/bin
path=`pwd`
if path='/mpjava/server/ly.nfrzzl.core.biz/bin';then
	echo '已经进入/mpjava/server/ly.nfrzzl.core.biz/bin'
	ID=`jps | grep CoreProvider | awk -F" " '{print $1}'`
	if [ -n "$ID" ];then
		echo 'core.biz存在,杀掉进程'
		kill -9 $ID
		echo '杀死进程成功'
		echo '重启服务core.biz'
		rm -rf ./nohub.out
		nohup ./ly.nfrzzl.core.biz &
		ID_new=`jps | grep CoreProvider | awk -F" " '{print $1}'`
		if [ -n "$ID_new" ];then
		echo '启动core.biz成功'
		fi
	else 
		echo "core.biz不存在,直接重启"
		echo "删除日志"
		rm -rf ./nohub.out
		nohup ./ly.nfrzzl.core.biz &
		echo "重启core.biz服务"
		ID_new=`jps | grep CoreProvider | awk -F" " '{print $1}'`
		if [ -n "$ID_new" ];then
		echo '启动core.biz成功'
		fi
	fi
fi
cd /mpjava/server/ly.nfrzzl.core.service/bin	
if path='/mpjava/server/ly.nfrzzl.core.service/bin';then
	echo '已经进入/mpjava/server/ly.nfrzzl.core.service/bin'
	ID=`jps | grep CoreConsumer | awk -F" " '{print $1}'`
	if [ -n "$ID" ];then
		echo 'core.server存在,杀掉进程'
		kill -9 $ID
		echo '杀死进程成功'
		echo '重启服务core.server'
		rm -rf ./nohub.out
		nohup ./ly.nfrzzl.core.service &
		ID_new=`jps | grep CoreConsumer | awk -F" " '{print $1}'`
		if [ -n "$ID_new" ];then
		echo '启动成功'
		fi
	else 
		echo "core.server不存在,直接重启"
		echo "删除日志"
		rm -rf ./nohub.out
		nohup ./ly.nfrzzl.core.service &
		echo "重启core.server服务"
		ID_new=`jps | grep CoreConsumer | awk -F" " '{print $1}'`
		if [ -n "$ID_new" ];then
		echo '启动core.server成功'
		fi
	fi
fi

升级代码: 

#!/bin/bash
fuwu_name=(
BasictoolsProvider
BasictoolsConsumer
BatchcustomerProvider
BatchcustomerConsumer
CreditProvider
CreditConsumer
RzzlGPSBoot
)
fuwu_path=(
/mpjava/server/ly.nfrzzl.basictools.biz/bin
/mpjava/server/ly.nfrzzl.basictools.service/bin
/mpjava/server/ly.nfrzzl.batchcustomer.biz/bin
/mpjava/server/ly.nfrzzl.batchcustomer.service/bin
/mpjava/server/ly.nfrzzl.creditPlat.credit.biz/bin
/mpjava/server/ly.nfrzzl.creditPlat.credit.service/bin
/mpjava/server/ly.nfrzzl.gps.service/bin
)
fuwu_name_now=(
baictools.biz
baictools.server
batchcustomer.biz
batchcustomer.server
creditPlat.credit.biz
creditPlat.credit.server
gps.service
)
qidongmingling=(
ly.nfrzzl.basictools.biz
ly.nfrzzl.basictools.service
ly.nfrzzl.batchcustomer.biz
ly.nfrzzl.batchcustomer.service
ly.nfrzzl.creditPlat.credit.biz
ly.nfrzzl.creditPlat.credit.service
ly.nfrzzl.gps.service
)
for ((i=0;i<${#fuwu_name[@]};i++)) 
do
	cd ${fuwu_path[i]}	
	if path='${fuwu_path[i]}';then
		echo "已经进入${fuwu_path[i]}"
		ID=`jps | grep ${fuwu_name[i]} | awk -F" " '{print $1}'`
			if [ -n "$ID" ];then
			echo "${fuwu_name_now[i]}存在"
			else 
			echo "${fuwu_name_now[i]}不存在,直接重启"
			echo "删除日志"
			rm -rf ./nohub.out
			nohup ./${qidongmingling[i]} &
			echo "重启${fuwu_name_now[i]}服务"
			ID_new=`jps | grep ${fuwu_name[i]} | awk -F" " '{print $1}'`
			if [ -n "$ID_new" ];then
			echo "启动{${fuwu_name_now[i]}}成功"
			fi
		fi
	fi
done

结果:

 

解放双手之liunx的shell编程:(六)利用shell脚本一键启动服务_第1张图片

解放双手之liunx的shell编程:(六)利用shell脚本一键启动服务_第2张图片

你可能感兴趣的:(shell,测试开发,自动化测试)