跳至主要內容
Linux常用命令&脚本&操作

常用shell脚本

jar包启动/停止/重启

脚本名称:server.sh

用途:用于执行springboot打包生成的可执行文件helloworld.jar ,jar包存放在/home/test/目录下。

脚本内容:

cd /home/test/
if [ -z $appName ];then
    appName=`ls -t |grep .jar$ |head -n1`
fi

if [ -n "$appName" ];then
	if [ -z $APP_NAME ];then
       APP_NAME=${appName%.*}
	fi
	if [ -z $JVM ];then
	JVM="-XX:+UseG1GC -XX:+HeapDumpOnOutOfMemoryError -Xms512M -Xmx2G"
	fi

fi

function getAppId()
{
	appId=`ps -ef |grep java|grep $appName|awk '{print $2}'`
}

function start()
{
	getAppId
	if [ -z $appId ];then
		echo "The $appName is starting..."
		nohup java $JVM -jar ./$appName --server.port=8090 --server.servlet.context-path=/appName  > /dev/null 2>&1 &
	else
		echo "$appName is still running, please check it..."
		exit 1
	fi  
}

function stop()
{
	getAppId
	if [ -z $appId ];then
	  echo "The $appName not running"
	else
        echo "The $appName is stopping..."
        kill $appId
		for i in {1..15}
		do
			echo *****wait 1s to check server[$appId]  status*****
			sleep 1
			getAppId
			if [ -z $appId ];then
				echo "The $appName is stopped..."
				return
			fi
		done
		echo *****force kill server[$appId]*****
  		kill -9 $appId
	fi
}

function restart()
{
    stop
    start
}

function status()
{
    getAppId
	if [ -z $appId ]; then
	    echo -e "$appName not running" 
	else
		echo -e "$appName is running [$appId], waiting to check status" 
		SYS_URL="http://localhost:$SERVER_PORT/appname/sys/status/check"
		echo "check $SYS_URL ......" 
        RST=`curl $SYS_URL -s`
		if [ "$RST" == "On" ]; then
			echo "running"
		else
			echo "failure"
		fi
	fi
}


function usage()
{
    echo "Usage: $0 {start|stop|restart|status|stop -f}"
    echo "Example: $0 start"
    exit 1
}

case $1 in
	start)
	start;;

	stop)
	stop;;
	
	restart)
	restart;;
	
	status)
	status;;
	
	*)
	usage;;
esac

代码小郭...大约 14 分钟SOPlinux