jar包启动脚本

无需参数脚本
start.sh

#!/bin/bash
serverhome=/app/xxx/admin
ps ax | grep 'xxx-admin-biz' | grep -v grep | awk '{print $1}' | xargs kill -9  &> /dev/null
sleep 1
nohup
java -server \
-Xmx2g \
-Xms2g \
-Xverify:none \
-XX:+PrintGCDetails \
-XX:+PrintGCDateStamps \
-XX:+PrintTenuringDistribution \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:+AggressiveOpts \
-XX:+UseConcMarkSweepGC \
-XX:+DisableExplicitGC \
-XX:CMSInitiatingOccupancyFraction=60 \
-jar  gdisp-screen-biz.jar --spring.config.location=/app/xxx/admin/resources/bootstrap.yml> /dev/null 2>&1 &

ps ax | grep 'xxx-admin-biz' | grep -v grep | awk '{print $1}' | xargs echo "starting server executor, pid:"

参数脚本

#!/bin/sh  
# desc 程序启动暂停脚本 

APP_HOME=/app/java/admin

#启动的程序名称
APP_NAME=xxx-admin-biz.jar

# 配置文件环境后缀
PROFILE=dev
# 配置文件nacos环境域
NAMESPACE=public

#日志
LOG_PATH=/app/java/logs
LOG_FILE=patch

#java虚拟机启动参数
JAVA_OPTS="-Xmx1048m -Xms1024m"

#**************************
#(函数)判断程序是否已启动
#初始化psid变量
#****************************
psid=0

checkpid() {
   JPID=$(ps -ef | grep $APP_NAME | grep -v grep |  awk '{ print $2 }')
   if [ -z "$JPID" ]
   then
      psid=0
   else
      psid=$JPID
   fi
}  

#**************************
#启动程序
#****************************
start() {
   checkpid

   if [ $psid -ne 0 ]; then
      echo "================================"
      echo "warn: $APP_NAME already started! (pid=$psid)"
      echo "================================"
   else
      echo -n "Starting $APP_NAME ..."

      nohup java -jar $APP_HOME/$APP_NAME $JAVA_OPTS --spring.profiles.active=$PROFILE --spring.cloud.nacos.discovery.namespace=$NAMESPACE --spring.cloud.nacos.config.namespace=$NAMESPACE >/dev/null 2>&1 &
      checkpid
      if [ $psid -ne 0 ]; then
         echo "(pid=$psid) [OK]"
      else
         echo "[Failed]"
      fi
   fi
}

#*****************************
#停止程序
#*****************************
stop() {
        checkpid
   
   if [ $psid -ne 0 ]; then
      echo -n "Stopping $APP_NAME ...(pid=$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  
}  

#********************************
#读取脚本的第一个参数($1),进行判断  
#参数取值示例:{start|stop|restart|status}  
###################################  
case "$1" in
   'start')
      start
      ;;
   'stop')
     stop
     ;;
   'restart')
     stop
     start
     ;;
   'status')
     status
     ;;
  *)
     echo "Usage: $0 {start|stop|restart|status}"
     exit 1
esac
exit 0

./start.sh stop/start/restart/status


版权声明:本文为weixin_43606738原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。