linux java启动脚本

1.(1)新建txt文件复制下面内容,按下面数字顺序修改。修改完改成server.sh文件。
(2) 将server.sh文件服务Linux中,并在server.sh文件目录执行 ./server.sh后,按照提示执行即可

#!/bin/bash
#
# cc: 2345 99 01

source /etc/profile
#1.修改jar包所在目录
jar_home=/home/changfeng/java/app
jar_name=""

option(){
	echo "********************************************************"
	echo "option: sh server.sh [start|stop|restart]"
	echo " *1, start * "
	echo " *2, stop * "
	echo " *3, restart * "
	echo "********************************************************"
	read -p "please input option:" option
	case "$option" in
	"1")
        choose
		jar $?
		if [[ $? -eq "0" ]]; then
		    #2.修改数字3,有多少个jar就写多少
		    for (( i = 1; i <= 3; i++ )); do
		        jar $i
		        start
		        sleep 1
	        done
        else
	        start
        fi
	;;
	"2")
		choose
		jar $?
		 #3.修改数字3,有多少个jar就写多少
		if [[ $? -eq "0" ]]; then
		    for (( i = 1; i <= 3; i++ )); do
		        jar $i
		        stop
		        sleep 1
	        done
        else
	        stop
        fi
	;;
	"3")
		choose
		jar $?
		if [[ $? -eq "0" ]]; then
		 #4.修改数字3,有多少个jar就写多少
		for (( i = 1; i <= 3; i++ )); do
		
		jar $i
		restart
		sleep 1
	    done

        else
	        restart
        fi
        ;;
	*)
		option
	;;
	esac

}

choose(){
	echo "********************************************************"
	echo "option: choose jar"
	echo " *0, all * "
	#5.修改成所有服务名
	echo " *1, eureka * "
	echo " *2, zuul * "
	echo " *3, user * "
	echo "********************************************************"
	read -p "please input option:" option
	return $option;
}

jar(){
	case "$1" in
	"0")
		return 0
	;;
	"1")
	    #6.修改成对应jar名称
		jar_name="eureka-server-1.0.0.jar"
		return 1
	;;
	"2")
	    #7.修改成对应jar名称
		jar_name="zuul-1.0.0.jar"
		return 2
	;;
	"3")
	    #8.修改成对应jar名称
		jar_name="user-1.0.0.jar"
		return 3
	;;
	*)
		jar
	;;
	esac

}

if_exist(){
	pid=`ps -ef | grep $jar_name | grep -v grep | awk '{print $2}' `
	if [[ -z $pid ]]; then
		return 1
	else
		return 0
	fi
}

start() {
	cd $jar_home
	nohup java -jar $jar_name > /dev/null 2>&1 &
	echo "The $jar_name in running... "
}

stop() {
	if_exist
	if [[ $? -eq "0" ]]; then
	    #9.修改数字3,有多少个jar就写多少
		for (( j = 0; j <= 3; j++ )); do
		if_exist
		if [[ $? -eq "0" ]]; then
			kill -9 $pid
		else
			break
		fi
	done
	echo "The $jar_name in stop... "
    else
	    echo "$jar_name  not running..."
    fi
}

restart() {
stop
sleep 2
start
}

#启动入口
option $1

你可能感兴趣的:(linux,java,运维)