java后台服務器啟動腳本


最近由於經常在項目上線或者調試中啟動服務,由於要設置環境變量這些,所以為了方便寫了個啟動腳本,希望能夠幫助大家,也算是給自己做個小筆記:

example_project_start.sh:

# /bin/bash
# Author
# Date: 2019-04-15
# Usage: ./example_project.sh $1 [$ENV]

Usage() {
  echo "command format: \"$0\" start|restart|stop [debug]"
}

if [ $# -lt 1 ]; then
  Usage
  exit 1
fi

prj_path=`cd $(dirname $0)/../; pwd`
cd $prj_path

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$prj_path/lib

export LD_LIBRARY_PATH

ENV=$2
[[ -z $ENV ]] && ENV="test" 

binary="example_project"
basedir="/export/servers/example_project"
identification="/export/servers/example_project/target"

jar_name="$basedir/target/example_project.jar"
cfg="$basedir/config"

function GetProcNum () {
  proc_num=`ps -ef|grep "${identification}"|grep "${basedir}"|grep -v " grep "|grep -v " vi "|grep -v " vim "|grep -v " tail "|wc -l`
}

function Release() {

  nohup java -Dfile.encoding=utf-8 -Dcfg=$cfg -jar $jar_name $ENV 1>out 2>&1 &

}

function Stop () {
  GetProcNum
  if [ $proc_num == 0 ]; then
    echo "${binary} stop successfully."
  else
    echo "${binary} is stopping ..."
    `ps -ef|grep "${identification}"|grep "${basedir}"|grep -v " grep "|grep -v " vi "|grep -v " vim "|grep -v " tail " > $basedir/stop.tmp`

    while read line
    do
      proc_id=`echo $line|awk -F' ' '{print $2}'`
      kill  -15 ${proc_id}
      sleep 5
    done < $basedir/stop.tmp
    while [ 1 -eq 1 ]; do
      sleep 5
      GetProcNum
      if [ $proc_num == 0 ]; then
        echo "${binary} stop successfully."
        break
      else
        echo "${binary} stop stopping!"
      fi
    done
  fi
}

function Start() {
  GetProcNum
  if [ $proc_num == 0 ]; then
    echo "${binary} is starting ..."
    
    Release $ENV

    sleep 2
    GetProcNum

    if [ $proc_num == 0 ]; then
      echo "${binary} start Failed!"
      exit 1
    else
      echo "${binary} start OK!"
    fi

  else
    echo "${binary} is already running now."
  fi
}

if [ "$1" == 'start' ]; then
  Start "$2"
elif [ "$1" == 'restart' ]; then
  Stop
  Start "$2"
elif [ "$1" == 'stop' ]; then
  Stop
else
  Usage
fi

 

轉載請注明出處:https://www.cnblogs.com/fnlingnzb-learner/p/10713590.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM