欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Linux下shell通用脚本启动jar(微服务)

程序员文章站 2022-04-21 18:05:46
Linux下shell通用脚本启动jar(微服务) vim app_jar.sh 使用方式: app_jar.sh start app_jar.sh stop app_jar.sh restart 注:该脚本只需要放在jar包当前目录下即可,脚本当前目录只允许存在一个jar包,如果多个会同时启动或重 ......

linux下shell通用脚本启动jar(微服务)

 

vim app_jar.sh

#!/bin/bash

#source /etc/profile
# auth:liucx
# please change these parameters according to your real env.
# set java home: remember that dolphin only supports jdk8!
java_home=/usr/java/jdk1.8.0_162

# application directory
cd `dirname $0`
app_home=`pwd`

app_name="`cd ${app_home} && find -name '*.jar' `"
jar_home=`pwd |awk -f"/" '{print $nf}'`
# java jvm lunch parameters
if [ x"$java_mem_opts" == x ];then
    java_mem_opts="-server -xms512m -xmx512m -xmn128m -xss256k -xx:+disableexplicitgc -xx:+useconcmarksweepgc -xx:+cmsparallelremarkenabled -xx:largepagesizeinbytes=128m -xx:+usefastaccessormethods -xx:+usecmsinitiatingoccupancyonly -xx:cmsinitiatingoccupancyfraction=70 "
fi


# waiting timeout for starting, in seconds
start_wait_timeout=30

psid=0
 
checkpid() {
   javaps=`$java_home/bin/jps -l | grep "$jar_home"`
   if [ -n "$javaps" ]; then
      psid=`echo $javaps | awk '{print $1}'`
   else
      psid=0
   fi
}



start() {
   checkpid

   if [ $psid -ne 0 ]; then
      echo "================================"
      echo "warn: $app_name already started! (pid=$psid)"
      echo "================================"
   else
      echo -n "starting $app_name ..."
      #-dspring.profiles.active=dev 指定环境
      nohup $java_home/bin/java -jar -dspring.profiles.active=dev -dconfig_enable=true $java_mem_opts  ${app_name} >${jar_home}.log 2>&1 &   
      checkpid
      if [ $psid -ne 0 ]; then
         echo "(pid=$psid) [ok]"
      else
         echo "[failed]"
      fi
   fi
}

stop() {
    
   sleep 2
   checkpid

   if [ $psid -ne 0 ]; then
      echo -n "stopping $app_name ...(pid=$psid) "
      #kill -9 $psid
      kill -9 $psid
      if [ $? -eq 0 ]; then
         echo "[ok]"
      else
         echo "[failed]"
      fi

#      checkpid
#      if [ $psid -ne 0 ]; then
#         stop
#      fi
   else
      echo "================================"
      echo "warn: $app_name is not running"
      echo "================================"
   fi
}



status() {
   checkpid

   if [ $psid -ne 0 ];  then
      echo "$app_name is running! (pid=$psid)"
   else
      echo "$app_name is not running"
   fi
}


info() {
   echo "system information:"
   echo "****************************"
   echo `head -n 1 /etc/issue`
   echo `uname -a`
   echo
   echo "java_home=$java_home"
   echo `$java_home/bin/java -version`
   echo
   echo "app_home=$app_home"
   echo "app_name=$app_name"
   echo "****************************"
}

case "$1" in
   'start')
      start
      ;;
   'stop')
     stop
     ;;
   'restart')
     stop
     echo "you restart the application ..."
     start #
     ;;
   'status')
     status
     ;;
   'info')
     info
     ;;
  *)
     echo "usage: $0 {start|stop|restart|status|info}"
     exit 1
esac

 

使用方式:

app_jar.sh  start

app_jar.sh  stop

app_jar.sh  restart

 

注:该脚本只需要放在jar包当前目录下即可,脚本当前目录只允许存在一个jar包,如果多个会同时启动或重启

还需提醒下:启动的jar包名主体不能改变,防止脚本无效。目前正在公司生产环境使用中,希望您纠正,提出建议。