#!/bin/bash
SpringBoot=$2
#启动参数
START_OPTS=$3
#JVM参数
JVM_OPTS="-Dname=$SpringBoot -Duser.timezone=Asia/Shanghai -Xms512M -Xmx512M -XX:PermSize=256M -XX:MaxPermSize=512M -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC"
APP_HOME=`pwd`
LOG_PATH=$APP_HOME/logs/$SpringBoot.log
ACTIVE="spring.profiles.active=dev" #启动环境
if [ "$1" = "" ];
then
echo -e "\033[0;31m 未输入操作名 \033[0m \033[0;34m {start|stop|restart|status} \033[0m"
exit 1
fi
if [ "$SpringBoot" = "" ];
then
echo -e "\033[0;31m 未输入应用名 \033[0m"
exit 1
fi
function start()
{
count=`jps -ml|grep $SpringBoot|grep -w $ACTIVE|awk '{print $1}'|wc -l`
if [ $count != 0 ];then
echo "$SpringBoot is running..."
else
if ["$START_OPTS" = ""];then
nohup java -jar $JVM_OPTS $SpringBoot --$ACTIVE $START_OPTS > /dev/null 2>&1 &
else
nohup java -jar $JVM_OPTS $SpringBoot --$ACTIVE $START_OPTS > /dev/null 2>&1 &
fi
sleep 10
echo "Start $SpringBoot success..."
fi
}
function stop()
{
echo "Stop $SpringBoot"
boot_id=`jps -ml|grep $SpringBoot|grep -w $ACTIVE|awk '{print $1}'`
count=`jps -ml|grep $SpringBoot|grep -w $ACTIVE|awk '{print $1}'|wc -l`
if [ $count != 0 ];then
#kill $boot_id
count=`jps -ml|grep $SpringBoot|grep -w $ACTIVE|awk '{print $1}'|wc -l`
boot_id=`jps -ml|grep $SpringBoot|grep -w $ACTIVE|awk '{print $1}'`
Model=${SpringBoot%-1*} #微服务名称
Model=${Model//_/-} #微服务名称 ,下划线 替换 横杠
echo "微服务名称:"$Model
BigModel=`echo $Model | tr 'a-z' 'A-Z'}` #替换为大写
echo "微服务名称大写:"$BigModel
Ip=`ifconfig eth0 |grep "inet addr"| cut -f 2 -d ":"|cut -f 1 -d " "` #ip地址
echo "Ip:"$Ip
Pid=`jps -ml|grep $SpringBoot|grep -w $ACTIVE|awk '{print $1}'`
echo "Pid:"$Pid
Port=`netstat -nlp | awk '{if($6=="LISTEN" && $7=="'"$Pid/java"'")print $4}' | awk -F ":" '{print $4}'`
echo "Port:"$Port
curl -X DELETE http://admin:admin@localhost:8081/eureka/apps/$BigModel/$Ip:$Model:$Port #通知eureka下线
kill -9 $boot_id
fi
}
function restart()
{
stop
sleep 2
start
}
function status()
{
count=`jps -ml|grep $SpringBoot|grep -w $ACTIVE|awk '{print $1}'|wc -l`
if [ $count != 0 ];then
echo "$SpringBoot is running..."
else
echo "$SpringBoot is not running..."
fi
}
case $1 in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
status;;
*)
echo -e "\033[0;31m Usage: \033[0m \033[0;34m sh $0 {start|stop|restart|status} {SpringBootJarName} \033[0m
\033[0;31m Example: \033[0m
\033[0;33m sh $0 start esmart-test.jar \033[0m"
esac