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

原文地址:https://www.cnblogs.com/fnlingnzb-learner/p/10713590.html