shell-jar包运行相关脚本

shell

Posted by lwj108 on June 13, 2019
cd /opt/micro-service/

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

#检查程序是否在运行
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包
  echo "Please Input jar Name:(servcie.jar)"
  read jarName

  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
    #启动
    java -jar $jarName &
  fi
}

#启动
start(){
  #输入要操作的jar包
  echo "Please Input jar Name:(servcie.jar)"
  read jarName
  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 config.properties BOOT-INF/classes/config/config.properties
    #压缩回jar内
    jar uvf $jarName BOOT-INF/classes/config/config.properties
    sleep 2
    #启动
    java -jar $jarName &
  fi
}

#停止
stop(){
  #输入要操作的jar包
  echo "Please Input jar Name:(servcie.jar)"
  read jarName
  is_exist
  if [ $? -eq "0" ]; then
    kill -9 $pid
    ps -ef|grep java
  else
    echo "${jarName} is not running"
  fi 
}

#状态
status(){
  #输入要操作的jar包
  echo "Please Input jar Name:(servcie.jar)"
  read jarName
  is_exist
  if [ $? -eq "0" ]; then
    echo "${jarName} is running. Pid is ${pid}"
  else
    echo "${jarName} is NOT running."
  fi
}

#重启
restart(){
  #输入要操作的jar包
  echo "Please Input jar Name:(servcie.jar)"
  read jarName
  stop
  sleep 5
  start
}

#全部启动
startAll(){
  for FILENAME in $(ls *jar);
  do 
    pid=`ps -ef|grep $FILENAME|grep -v grep|awk '{print $2}'`
    java -jar $pid &  
  done
}

#全部停止
stopAll(){
  for FILENAME in $(ls *jar);
  do 
    pkill -9 $FILENAME
  done
}

#全部重启
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

具体操作如下图(以upstart修改微服务下redis.yaml配置文件启动为例)。 jar-shell启动脚本实例.jpg 或者点击这里查看