铁雪资源网 Design By www.gsvan.com

废话不多说了,先给大家上代码,具体代码如下所示:

#!/bin/bash
cd `dirname $0`
CUR_SHELL_DIR=`pwd`
CUR_SHELL_NAME=`basename ${BASH_SOURCE}`
#修改这里jar包名即可
JAR_NAME="xxxxxxxxxxxx.jar" 
JAR_PATH=$CUR_SHELL_DIR/$JAR_NAME
#JAVA_MEM_OPTS=" -server -Xms1024m -Xmx1024m -XX:PermSize=128m"
JAVA_MEM_OPTS=""
#SPRING_PROFILES_ACTIV="-Dspring.profiles.active=eureka2"
SPRING_PROFILES_ACTIV=""
LOG_DIR=$CUR_SHELL_DIR/logs
LOG_PATH=$LOG_DIR/${JAR_NAME}.log
echo_help()
{
  echo -e "syntax: sh $CUR_SHELL_NAME start|stop"
}
if [ -z $1 ];then
  echo_help
  exit 1
fi
if [ ! -d "$LOG_DIR" ];then
  mkdir "$LOG_DIR"
fi
if [ ! -f "$LOG_PATH" ];then
  touch "$LOG_DIR"
fi
if [ "$1" == "start" ];then
# check server
  PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
  if [ -n "$PIDS" ]; then
    echo -e "ERROR: The $JAR_NAME already started and the PID is ${PIDS}."
    exit 1
  fi
echo "Starting the $JAR_NAME..."
 
  # start
  nohup java $JAVA_MEM_OPTS -jar $SPRING_PROFILES_ACTIV $JAR_PATH  $LOG_PATH 2>&1 &
 
  COUNT=0
  while [ $COUNT -lt 1 ]; do
    sleep 1
    COUNT=`ps --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}' | wc -l`
    if [ $COUNT -gt 0 ]; then
      break
    fi
  done
  PIDS=`ps --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}'`
  echo "${JAR_NAME} Started and the PID is ${PIDS}."
  echo "You can check the log file in ${LOG_PATH} for details."
 
elif [ "$1" == "stop" ];then
 
  PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
  if [ -z "$PIDS" ]; then
    echo "ERROR:The $JAR_NAME does not started!"
    exit 1
  fi
echo -e "Stopping the $JAR_NAME..."
 
  for PID in $PIDS; do
    kill $PID > /dev/null 2>&1
  done
 
  COUNT=0
  while [ $COUNT -lt 1 ]; do
    sleep 1
    COUNT=1
    for PID in $PIDS ; do
      PID_EXIST=`ps --no-heading -p $PID`
      if [ -n "$PID_EXIST" ]; then
        COUNT=0
        break
      fi
    done
  done
 
  echo -e "${JAR_NAME} Stopped and the PID is ${PIDS}."
else
  echo_help
  exit 1
fi

以上代码只需修改jar包名即可使用

启动jar包:

 ./xxxx.sh start

停止 jar包

./xxxx.sh stop

总结

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

标签:
linux,部署Shell,脚本,linux,springboot,jar

铁雪资源网 Design By www.gsvan.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
铁雪资源网 Design By www.gsvan.com

评论“Linux 启动停止SpringBoot jar 程序部署Shell 脚本的方法”

暂无Linux 启动停止SpringBoot jar 程序部署Shell 脚本的方法的评论...