微服务启动脚本优化版本

方便启动各类微服务jar文件

Posted by lwj108 on August 1, 2019
cd /opt/micro-service/

#输入要进行的操作,启动jar
echo "you need todo?(start/stop/status/restart/startAll/stopAll/restartAll/upstart)"

i=0;
array=(start stop status restart startAll stopAll restartAll upstart)
for(( i=0;i<${#array[@]};i++)) 
do
let j=i+1;
echo $j":${array[i]}";
done;
echo -n "Please Input your choose number: "
read todoNum
todo=${array[$todoNum-1]}

#检查程序是否在运行
is_exist(){
  pid=`ps -ef|grep $jarName|grep -v grep|awk '{print $2}'`
  #如果不存在返回1,存在返回0     
  if [ -z "${pid}" ]; then
   return 1
  else
    return 0
  fi
}

#修改启动
upstart(){
  #输入要操作的jar包
  i=0;
  jar_array=()
  for FILENAME in $(ls *jar);
  do 
    let i=i+1;
    echo $i": ${FILENAME}";
    let j=i-1;
    jar_array[$j]=${FILENAME};
  done
  echo -n "Please Input your choose number: "
  let n=index-1
  jarName=${jar_array[$n]}
  is_exist
  if [ $? -eq 0 ]; then
    echo "${jarName} is already running. please stop"
  else
    echo "Enter the file name you want to replace"
    read fileName
    jar tvf $jarName |grep $fileName
    #选择正确的文件路径进行修改
    echo "Query out as follows,Please select the copy you want"
    read oldFilePath
    #解压jar内文件
    jar xvf $jarName $oldFilePath
    sleep 1
    #输入jar外替换文件路径
    echo "Please Input replace file path:(like:config/config.properties)"
    read newFilePath
    #替换文件
    cp $newFilePath $oldFilePath
    echo "Replace success!"
    #压缩回jar内
    jar uvf $jarName $oldFilePath
    sleep 2
    #启动
    #判断日志文件夹是否存在,不存在则创建
    if [ ! -d ${jarName%%-0*}  ];then
      mkdir ${jarName%%-0*}
      nohup java -jar $jarName > ${jarName%%-0*}/log.out &
    else
      nohup java -jar $jarName > ${jarName%%-0*}/log.out &
    fi
    tail -100f ${jarName%%-0*}/log.out
    echo "${jarName} be running"
  fi
}

#启动
start(){
  #输入要操作的jar包
  i=0;
  jar_array=()
  for FILENAME in $(ls *jar);
  do 
    let i=i+1;
    echo $i": ${FILENAME}";
    let j=i-1;
    jar_array[$j]=${FILENAME};
  done
  echo -n "Please Input your choose number: "
  read index
  let n=index-1
  jarName=${jar_array[$n]}
  echo "${jarName} starting..."
  is_exist
  if [ $? -eq 0 ]; then
    echo "${jarName} is already running. pid=${pid}"
  else
    #解压jar内文件
    jar xvf $jarName BOOT-INF/classes/config/config.properties
    sleep 1
    #替换文件
    cp -rf config/config.properties BOOT-INF/classes/config/config.properties
    #压缩回jar内
    jar uvf $jarName BOOT-INF/classes/config/config.properties
    sleep 2
    #启动
    #判断日志文件夹是否存在,不存在则创建
    if [ ! -d ${jarName%%-0*}  ];then
      mkdir ${jarName%%-0*}
      nohup java -jar $jarName > ${jarName%%-0*}/log.out &
    else
      nohup java -jar $jarName > ${jarName%%-0*}/log.out &
    fi
    tail -100f ${jarName%%-0*}/log.out
    echo "${jarName} be running"
  fi
}

#停止
stop(){
  #输入要操作的jar包
  i=0;
  jar_array=()
  for FILENAME in $(ls *jar);
  do 
    let i=i+1;
    echo $i": ${FILENAME}";
    let j=i-1;
    jar_array[$j]=${FILENAME};
  done
  echo -n "Please Input your choose number: "
  read index
  let n=index-1
  jarName=${jar_array[$n]}
  echo "${jarName} stopping..."
  is_exist
  if [ $? -eq "0" ]; then
    kill -9 $pid
    echo "${jarName} be stoped"
  else
    echo "${jarName} is not running"
  fi 
}

#状态
status(){
  #输入要操作的jar包
  i=0;
  jar_array=()
  for FILENAME in $(ls *jar);
  do 
    let i=i+1;
    echo $i": ${FILENAME}";
    let j=i-1;
    jar_array[$j]=${FILENAME};
  done
  echo -n "Please Input your choose number: "
  read index
  let n=index-1
  jarName=${jar_array[$n]}
  is_exist
  if [ $? -eq "0" ]; then
    echo "${jarName} is running. Pid is ${pid}"
  else
    echo "${jarName} is NOT running."
  fi
}

#重启
restart(){
  #输入要操作的jar包
  i=0;
  jar_array=()
  for FILENAME in $(ls *jar);
  do 
    let i=i+1;
    echo $i": ${FILENAME}";
    let j=i-1;
    jar_array[$j]=${FILENAME};
  done
  echo -n "Please Input your choose number: "
  read index
  let n=index-1
  jarName=${jar_array[$n]}
  is_exist
  if [ $? -eq "0" ]; then
    kill -9 $pid
    echo "${jarName} be stoped"
  else
    echo "${jarName} is not running"
  fi 
  sleep 2
  #解压jar内文件
  jar xvf $jarName BOOT-INF/classes/config/config.properties
  sleep 1
  #替换文件
  cp -rf config/config.properties BOOT-INF/classes/config/config.properties
  #压缩回jar内
  jar uvf $jarName BOOT-INF/classes/config/config.properties
  sleep 2
  #启动
  #判断日志文件夹是否存在,不存在则创建
  if [ ! -d ${jarName%%-0*}  ];then
    mkdir ${jarName%%-0*}
    nohup java -jar $jarName > ${jarName%%-0*}/log.out &
  else
    nohup java -jar $jarName > ${jarName%%-0*}/log.out &
  fi
  tail -100f ${jarName%%-0*}/log.out
  echo "restart success!"
}

#全部启动
startAll(){
  for FILENAME in $(ls *jar);
  do 
    nohup java -jar $FILENAME & 
    sleep 10 
  done
  echo "startAll success!"
}

#全部停止
stopAll(){
  for FILENAME in $(ls *jar);
  do 
    pid=`ps -ef|grep $FILENAME|grep -v grep|awk '{print $2}'`
    kill -9 $pid
  done
  echo "stopAll success!"
}

#全部重启
restartAll(){
  stopAll
  sleep 10
  startAll
}

case $todo in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status
    ;;
  upstart)
    upstart
    ;;
  restart)
    restart
    ;;
  startAll)
    startAll
    ;;
  stopAll)
    stopAll
    ;;
  restartAll)
    restartAll
    ;;
esac