jar 启动脚本

前段时间用springboot做项目后,每次重新发布都好麻烦, 所以写了个脚本来配合jenkins 发布;

#!/bin/bash


APP_NAME=application.jar

function check {
	local PID=0
	if [ -e tpid ] ;then
		PID=$(cat tpid)
		# while read TMPID; do PID=$TMPID ;done < tpid
	fi
	# echo "PID:$PID"
	if ps -p $PID > /dev/null ;then
		echo $PID
    else
    	echo 0
	fi
}
function isRun {
	local PID=$(check)
	if [ $PID -ne 0 ]; then
		echo "application is running..."
	else
		echo "application is not running..."
	fi
}
 
function start {
	if [ ! -e $APP_NAME ]; then 
		echo "$APP_NAME is not found!"
		return 0
	fi

	local PID=$(check)
	if [ $PID -ne 0 ]; then
		echo "$PID 	$APP_NAME was running..."
		return 0
	fi

	local argumet=""
	if [ -e application.properties ] ;then
		argumet="$argumet --spring.config.location=application.properties"
	fi
	nohup java -jar $APP_NAME $argumet > log.out 2>&1 &
	echo $! > tpid
	PID=$(cat tpid)
	echo $PID Start Success!
}

function stop {
	local PID=$(check)
	if [ $PID -ne 0 ]; then
		echo -n "Stop Process..."
		kill -15 $PID
		num=0
		while [[ num -le 5 ]]; do
			# echo $num
			echo -n "." 
			sleep 1
			PID=$(check)
			if [ $PID -eq 0 ]; then
				echo "Success!"
				return 0
			fi
			num=$[ $num + 1 ]
		done
		PID=$(check)
		if [ $PID -ne 0 ]; then
			kill -9 $PID
			echo ""
			echo "Kill Process!"
		else
			echo "Success!"
		fi
		return 0
	fi
	return 1
}

function restart {
	stop
	start
}

function deploy {
	fd=$(date +"%Y%m%d%H%M%S")
	fileCount=`ls ./target/ | grep .jar$ | wc -l`
	if [ $fileCount -eq 1 ]; then  
		fileName=`ls ./target/ | grep .jar$`
		newName="$fileName.$fd"
		mv ./target/$fileName ./target/$newName
		if [ -e ./$APP_NAME ]; then
			rm -rf ./$APP_NAME
		fi
		ln -s ./target/$newName ./$APP_NAME
		echo "deploy success..."
		restart
	else
		echo "Can not be deploy, jar file $fileCount"
		return 1
	fi
}


case $1 in
	"start" ) start;;
	"stop" ) stop;;
	"check" ) isRun;;
	"restart" ) restart;;
	"deploy" ) deploy;;
	* ) echo "$0 start"
		echo "$0 stop"
		echo "$0 check"
		echo "$0 restart"
		echo "$0 deploy"
esac

exit 0

  

原文地址:https://www.cnblogs.com/xdsfoo/p/7026510.html