#!/bin/bash
#引入环境变量
export   YBS_SCIENCE_HOME=/home/data/jar
#运行目录
rundir=""
case $1 in
ybs-science)
        rundir=science
        ;;
*)
        echo "输入参数$1有误，请重试！！"
        exit
        ;;
esac

#当前运行jar包绝对路径
runPath=$YBS_SCIENCE_HOME/$rundir/run
#移动
sudo mv ~/$1.jar $YBS_SCIENCE_HOME/$rundir/pub/
#备份
bakJarName=`date +%Y%m%d%H:%M:%S`
sudo mkdir $YBS_SCIENCE_HOME/$rundir/bak/$bakJarName
sudo mv $runPath/* $YBS_SCIENCE_HOME/$rundir/bak/$bakJarName/
#放包
sudo mv $YBS_SCIENCE_HOME/$rundir/pub/*  $runPath/

runjar=`ls $runPath`
if [[ -z $runjar ]]; then
echo "找不到"$1"的jar包，请检查jar是否存在. shell脚本退出"
exit
fi

#得到服务pid
server=`sudo ps -ef | grep java | grep $1 |grep nohup | grep -v grep | awk '{print $2}'`
if [[ $server -gt 0 ]]; then
  echo "运行服务列表中存在"$1"服务,正在关停..."
  sudo ps -ef | grep java | grep $1 |grep -v grep | awk '{print $2}' |xargs sudo kill -9
  sleep 3
  echo "关停成功"
fi

cd $runPath
sudo nohup java -jar $runjar > /dev/null 2>&1 &

sleep 3s
echo "启动成功,请查看控制台日志确认"