root@iZuf6heduaqlfxz2wj9zc3Z:~# cat /home/jenkins/vote-api/vote-api-test.sh
#!/bin/bash
# 配置jdk环境
export JAVA_HOME=/usr/local/software/jdk1.8.0_141
export JAR_HOME=$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
# 配置项目名称
APP_NAME=vote-api.jar
#判断进程是否存在 若存在 则kill
is_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
# 删除上一次编译打包后的文件
rm -rf /home/test-vote-api/*.jar
# 将jenkins打包编译之后的文件复制到指定位置
cp -rf /home/jenkins/vote-api/target/*.jar /home/test-vote-api/${APP_NAME}
# 启动项目
nohup java -jar -Dspring.profiles.active=dev /home/test-vote-api/${APP_NAME} -Xms256m -Xmx512m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m --server.port=8082 >/home/test-vote-api/logs/vote-api.log 2>&1 &
echo "程序已启动..."
# 进程是否存在
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. pid is ${pid} "
else
echo "${APP_NAME} is not running."
fi
exit