#!/bin/bash
SpringBoot=$2
environment=$3
directory=$4
debug=$5
export JAVA_HOME=/home/java/jdk1.8.0_171
export PATH=$PATH:$JAVA_HOME/bin
if [ "$1" = "" ];
then
echo -e "\033[0;31m 未输入操作名 \033[0m \033[0;34m {start|stop|restart|status|copy} \033[0m"
exit 1
fi
if [ "$SpringBoot" = "" ];
then
echo -e "\033[0;31m 未输入应用名 \033[0m"
exit 1
fi
if [ "$environment" = "" ];
then
echo -e "\033[0;31m 未输入环境类型 \033[0m"
exit 1
fi
function start()
{
echo "准备启动 $SpringBoot"
count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
if [ $count != 0 ];then
echo "$正在停止应用程序..."
restart
else
if [ "$debug" != "debug" ];
then
echo "正在启动 $SpringBoot"
BUILD_ID=dontKillMe
nohup java -jar $SpringBoot --spring.profiles.active=$environment > /dev/null 2>&1 &
echo "$SpringBoot 启动完成....."
else
echo "正在以DEBUG启动 $SpringBoot"
BUILD_ID=dontKillMe
nohup java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=5678,suspend=n -jar $SpringBoot --spring.profiles.active=$environment > /dev/null 2>&1 &
echo "$SpringBoot 以DEBUG启动完成....."
fi
fi
}
function stop()
{
echo "正在停止 $SpringBoot"
boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
echo "结束进程ID $boot_id,$SpringBoot 有 $count 个存在"
if [ $count > 0 ];then
kill $boot_id
echo "杀死"
count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
echo "再次统计 $count"
boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
kill -9 $boot_id
fi
}
function restart()
{
stop
sleep 2
start
}
function copy(){
#jar_path=/var/lib/jenkins/workspace/$directory/default/syh/${SpringBoot%.jar*}/target
jar_path=/var/lib/jenkins/workspace/$directory/${SpringBoot%.jar*}/target
echo "编译目录 $jar_path"
target_path=/root/work/syh-project
cd $jar_path
cp $jar_path/$SpringBoot $target_path
echo "成功复制到运行目录 $target_path"
cd $target_path
restart
}
function status()
{
count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
if [ $count != 0 ];then
echo "$SpringBoot 已经在运行"
else
echo "$SpringBoot 没有运行"
fi
}
case $1 in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
status;;
copy)
copy;;
*)
echo -e "\033[0;31m Usage: \033[0m \033[0;34m sh $0 {start|stop|restart|status|copy} {SpringBootJarName} \033[0m
\033[0;31m Example: \033[0m
\033[0;33m sh $0 start test.jar \033[0m"
esac