1. 程式人生 > >Java 應用程式啟動停止暫停啟動指令碼

Java 應用程式啟動停止暫停啟動指令碼

#!/bin/sh  
# Author Pine Chown
# date 2018-12-06
# desc 程式啟動暫停指令碼 

APP_HOME=/usr/local/java/application

#啟動的程式名稱
APP_NAME=charging

# 配置檔案生產環境
PROFILE=pro

LOG_PATH=/usr/local/java/logs
LOG_FILE=patch

#java虛擬機器啟動引數
JAVA_OPTS="-Xmx1048m -Xms1024m"

#**************************
#(函式)判斷程式是否已啟動
#初始化psid變數
#****************************
psid=0

checkpid() {
   JPID=$(ps -ef | grep java.*$APP_NAME.*.war | 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*.war $JAVA_OPTS --spring.profiles.active=$PROFILE >/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

編寫完成!!!