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包名主体不能改变,防止脚本无效。目前正在公司生产环境使用中,希望您纠正,提出建议。

总结

以上所述是小编给大家介绍的Linux下shell通用脚本启动jar,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对呐喊教程网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。