启动停止多个jar

启动停止多个jar

启动文件设置

多文件同时启动

gollum.sh

#!/bin/sh
#chkconfig: 2345 80 05
#description: gollum

##jar包
export EUREKA=/root/gollum/jar/server-eureka-1.0.jar
export ZUUL=/root/gollum/jar/server-zuul-1.0.jar
export LAB=/root/gollum/jar/service-laboratory-1.0.jar
export EXAM=/root/gollum/jar/service-examination-1.0.jar
export CONSULT=/root/gollum/jar/service-consultation-1.0.jar

##端口
export EUREKA_port=8000
export ZUUL_port=8080
export LAB_port=9106
export EXAM_port=9107
export CONSULT_port=9110

##日志
export EUREKA_log=/root/gollum/jar/log/eureka.out
export ZUUL_log=/root/gollum/jar/log/zuul.out
export LAB_log=/root/gollum/jar/log/laboratory.out
export EXAM_log=/root/gollum/jar/log/examination.out
export CONSULT_log=/root/gollum/log/consultation.out

##配置文件,dev/prod
export spring_config=prod
 
case "$1" in
 
start)
        ## 启动EUREKA
        echo "--------EUREKA 开始启动--------------"
        nohup java -jar $EUREKA --spring.profiles.active=$spring_config >$EUREKA_log 2>&1 &
        EUREKA_pid=`lsof -i:$EUREKA_port|grep "LISTEN"|awk '{print $2}'`
        until [ -n "$EUREKA_pid" ]
            do
              EUREKA_pid=`lsof -i:$EUREKA_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "EUREKA pid is $EUREKA_pid" 
        echo "--------EUREKA 启动成功--------------"
 
        ## 启动ZUUL
        echo "--------开始启动ZUUL---------------"
        nohup java -jar $ZUUL --spring.profiles.active=$spring_config >$ZUUL_log 2>&1 &
        ZUUL_pid=`lsof -i:$ZUUL_port|grep "LISTEN"|awk '{print $2}'` 
        until [ -n "$ZUUL_pid" ]
            do
              ZUUL_pid=`lsof -i:$ZUUL_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "ZUUL pid is $ZUUL_pid"     
        echo "---------ZUUL 启动成功-----------"
		
		## 启动LAB
        echo "--------开始启动LAB---------------"
        nohup java -jar $LAB --spring.profiles.active=$spring_config >$LAB_log 2>&1 &
        LAB_pid=`lsof -i:$LAB_port|grep "LISTEN"|awk '{print $2}'` 
        until [ -n "$LAB_pid" ]
            do
              LAB_pid=`lsof -i:$LAB_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "LAB pid is $LAB_pid"     
        echo "---------LAB 启动成功-----------"
		
		## 启动EXAM
        echo "--------开始启动EXAM---------------"
        nohup java -jar $EXAM --spring.profiles.active=$spring_config >$EXAM_log 2>&1 &
        EXAM_pid=`lsof -i:$EXAM_port|grep "LISTEN"|awk '{print $2}'` 
        until [ -n "$EXAM_pid" ]
            do
              EXAM_pid=`lsof -i:$EXAM_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "EXAM pid is $EXAM_pid"     
        echo "---------EXAM 启动成功-----------"
		
		## 启动CONSULT
        echo "--------开始启动CONSULT---------------"
        nohup java -jar $CONSULT --spring.profiles.active=$spring_config >$CONSULT_log 2>&1 &
        CONSULT_pid=`lsof -i:$CONSULT_port|grep "LISTEN"|awk '{print $2}'` 
        until [ -n "$CONSULT_pid" ]
            do
              CONSULT_pid=`lsof -i:$CONSULT_port|grep "LISTEN"|awk '{print $2}'`  
            done
        echo "CONSULT pid is $CONSULT_pid"     
        echo "---------CONSULT 启动成功-----------"
		
		echo "===startAll success==="
        ;;
 
 stop)
        P_ID=`ps -ef | grep -w $EUREKA | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===EUREKA process not exists or stop success"
        else
            kill -9 $P_ID
            echo "EUREKA killed success"
        fi
		P_ID=`ps -ef | grep -w $ZUUL | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===ZUUL process not exists or stop success"
        else
            kill -9 $P_ID
            echo "ZUUL killed success"
        fi
		P_ID=`ps -ef | grep -w $LAB | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===LAB process not exists or stop success"
        else
            kill -9 $P_ID
            echo "LAB killed success"
        fi
		P_ID=`ps -ef | grep -w $EXAM | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===EXAM process not exists or stop success"
        else
            kill -9 $P_ID
            echo "EXAM killed success"
        fi
		P_ID=`ps -ef | grep -w $CONSULT | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "===CONSULT process not exists or stop success"
        else
            kill -9 $P_ID
            echo "CONSULT killed success"
        fi	
		
        echo "===stopAll success==="
        ;;   
 
restart)
        $0 stop
        sleep 2
        $0 start
        echo "===restartAll success==="
        ;;   
esac	
exit 0

可单文件启动

gullum-one.sh

#!/bin/sh
#chkconfig: 2345 80 06
#description: gollum

export EUREKA=/root/gollum/jar/server-eureka-1.0.jar
export ZUUL=/root/gollum/jar/server-zuul-1.0.jar
export LAB=/root/gollum/jar/service-laboratory-1.0.jar
export EXAM=/root/gollum/jar/service-examination-1.0.jar
export CONSULT=/root/gollum/jar/service-consultation-1.0.jar
 
export EUREKA_port=8000
export ZUUL_port=8080
export LAB_port=9106
export EXAM_port=9107
export CONSULT_port=9110

export EUREKA_log=/root/gollum/jar/log/eureka.out
export ZUUL_log=/root/gollum/jar/log/zuul.out
export LAB_log=/root/gollum/jar/log/laboratory.out
export EXAM_log=/root/gollum/jar/log/examination.out
export CONSULT_log=/root/gollum/log/consultation.out

##配置文件,dev/prod
export spring_config=prod
 
case "$1" in
 
start)
	case "$2" in
		
		eureka|EUREKA)
			## 启动EUREKA
			echo "--------EUREKA 开始启动--------------"
			nohup java -jar $EUREKA --spring.profiles.active=$spring_config >$EUREKA_log 2>&1 &
			EUREKA_pid=`lsof -i:$EUREKA_port|grep "LISTEN"|awk '{print $2}'`
			until [ -n "$EUREKA_pid" ]
				do
				  EUREKA_pid=`lsof -i:$EUREKA_port|grep "LISTEN"|awk '{print $2}'`  
				done
			echo "EUREKA pid is $EUREKA_pid" 
			echo "--------EUREKA 启动成功--------------"
			;;
			
		zuul|ZUUL)
			## 启动ZUUL
			echo "--------开始启动ZUUL---------------"
			nohup java -jar $ZUUL --spring.profiles.active=$spring_config >$ZUUL_log 2>&1 &
			ZUUL_pid=`lsof -i:$ZUUL_port|grep "LISTEN"|awk '{print $2}'` 
			until [ -n "$ZUUL_pid" ]
				do
				  ZUUL_pid=`lsof -i:$ZUUL_port|grep "LISTEN"|awk '{print $2}'`  
				done
			echo "ZUUL pid is $ZUUL_pid"     
			echo "---------ZUUL 启动成功-----------"
			;;
			
		lab|LAB)
			## 启动LAB
			echo "--------开始启动LAB---------------"
			nohup java -jar $LAB --spring.profiles.active=$spring_config >$LAB_log 2>&1 &
			LAB_pid=`lsof -i:$LAB_port|grep "LISTEN"|awk '{print $2}'` 
			until [ -n "$LAB_pid" ]
				do
				  LAB_pid=`lsof -i:$LAB_port|grep "LISTEN"|awk '{print $2}'`  
				done
			echo "LAB pid is $LAB_pid"     
			echo "---------LAB 启动成功-----------"
			;;
		exam|EXAM)
			## 启动EXAM
			echo "--------开始启动EXAM---------------"
			nohup java -jar $EXAM --spring.profiles.active=$spring_config >$EXAM_log 2>&1 &
			EXAM_pid=`lsof -i:$EXAM_port|grep "LISTEN"|awk '{print $2}'` 
			until [ -n "$EXAM_pid" ]
				do
				  EXAM_pid=`lsof -i:$EXAM_port|grep "LISTEN"|awk '{print $2}'`  
				done
			echo "EXAM pid is $EXAM_pid"     
			echo "---------EXAM 启动成功-----------"
			;;
		consult|CONSULT)
			## 启动CONSULT
			echo "--------开始启动CONSULT---------------"
			nohup java -jar $CONSULT --spring.profiles.active=$spring_config >$CONSULT_log 2>&1 &
			CONSULT_pid=`lsof -i:$CONSULT_port|grep "LISTEN"|awk '{print $2}'` 
			until [ -n "$CONSULT_pid" ]
				do
				  CONSULT_pid=`lsof -i:$CONSULT_port|grep "LISTEN"|awk '{print $2}'`  
				done
			echo "CONSULT pid is $CONSULT_pid"     
			echo "---------CONSULT 启动成功-----------"
			;;
			
    esac
	;;
 
 stop)
	case "$2" in
		
		eureka|EUREKA)
			P_ID=`ps -ef | grep -w $EUREKA | grep -v "grep" | awk '{print $2}'`
			if [ "$P_ID" == "" ]; then
				echo "===EUREKA process not exists or stop success"
			else
				kill -9 $P_ID
				echo "EUREKA killed success"
			fi
			;;
			
		zuul|ZUUL)
			P_ID=`ps -ef | grep -w $ZUUL | grep -v "grep" | awk '{print $2}'`
			if [ "$P_ID" == "" ]; then
				echo "===ZUUL process not exists or stop success"
			else
				kill -9 $P_ID
				echo "ZUUL killed success"
			fi
			;;
						
		lab|LAB)
			P_ID=`ps -ef | grep -w $LAB | grep -v "grep" | awk '{print $2}'`
			if [ "$P_ID" == "" ]; then
				echo "===LAB process not exists or stop success"
			else
				kill -9 $P_ID
				echo "LAB killed success"
			fi
			;;
		exam|EXAM)
			P_ID=`ps -ef | grep -w $EXAM | grep -v "grep" | awk '{print $2}'`
			if [ "$P_ID" == "" ]; then
				echo "===EXAM process not exists or stop success"
			else
				kill -9 $P_ID
				echo "EXAM killed success"
			fi
			;;
		consult|CONSULT)
			P_ID=`ps -ef | grep -w $CONSULT | grep -v "grep" | awk '{print $2}'`
			if [ "$P_ID" == "" ]; then
				echo "===CONSULT process not exists or stop success"
			else
				kill -9 $P_ID
				echo "CONSULT killed success"
			fi
			;;
					
	esac
	;;
 
restart)
        $0 stop $2
        sleep 2
        $0 start $2
        echo "===restart $2 success==="
        ;;   
esac	
exit 0

启动命令

#全部jar执行
启动:./gollum.sh start
停止:./gollum.sh stop
重启:./gollum.sh restart

#单个jar执行
启动:./gollum-one.sh start eureka 其他服务类似
停止:./gollum-one.sh stop eureka 其他服务类似
重启:./gollum-one.sh restart eureka  

开机启动

#文件授权:
chmod +x /etc/init.d/gollum.sh
chkconfig --list
#脚本添加到启动服务中: 
chkconfig --add  gollum.sh
#脚本启动服务中移除:
chkconfig --del  gollum.sh
#启动服务: 
chkconfig  gollum.sh on

#最后reboot重启系统即可

service gollum.sh start
service gollum.sh stop

常见问题

/bin/bash^M: 坏的解释器: 没有那个文件或目录

执行shell脚本是报错:/bin/bash^M: 坏的解释器: 没有那个文件或目录是因为该文件在windows系统上打开过,关闭后其中的空格符号和Linux的不同,导致这个报错,我们可以通过sed命令与正则的配合将文件中的空格符号替换成linux的空格

sed -i 's/\r$//' java.sh 

-bash: ./java.sh: 权限不够

chmod +7 java.sh
posted @   地球小星星  阅读(113)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示