shell

jar包启动脚本

admin · 11月26日 · 2020年
#作者:刘 2020.11月
#!/bin/bash
export JAVA_HOME=/software/java
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
TIME=$(date "+%Y-%m-%d %H:%M:%S")
STEP=10
JAR_DIR_PATH=$(cd $(dirname $0); pwd)
echo "-------${TIME}-------需要启动并监测服务的路径:"${JAR_DIR_PATH}

for JAR in `ls -l ${JAR_DIR_PATH}/*.jar | awk '{print $9}'`
do
  JAR_PATH=$JAR
  JAR_NAME=$(basename $JAR)
  
  echo "--------正在检测jar包目录:"${JAR_DIR_PATH}
  echo "jar包路径:"${JAR_PATH}
  echo "jar包名称:"${JAR_NAME}
  PID=`ps -ef |grep $(echo $JAR | awk -F/ '{print $NF}') | grep -v grep | grep -v "$0" | awk '{print $2}'`
  if [ -n "$PID" ];then
     if [ X$1 == 'X' ];then
        echo "$JAR_NAME:正在运行,PID=$PID"
     elif [ $1 == "${JAR_NAME}" ];then
        echo "$JAR_NAME:正在重启,重启前PID=${PID}"
        kill -9 ${PID}
        exec nohup java -Xms512m -Xmx1g -jar -Duser.timezone=GMT+8 $JAR >/dev/null 2>&1 &
        sleep 2
        PID_NEW=`ps -ef |grep $(echo $JAR | awk -F/ '{print $NF}') | grep -v grep | grep -v "$0" | awk '{print $2}'`
        echo "$JAR_NAME:重启完成,重启后PID=${PID_NEW}"
     elif [ $1 == 'all' ];then
        echo "$JAR_NAME:正在重启,重启前PID=${PID}"
        kill -9 ${PID}
        exec nohup java -Xms512m -Xmx1g -jar -Duser.timezone=GMT+8 $JAR >/dev/null 2>&1 &
        sleep $STEP
        PID_NEW=`ps -ef |grep $(echo $JAR | awk -F/ '{print $NF}') | grep -v grep | grep -v "$0" | awk '{print $2}'`
        echo "$JAR_NAME:重启完成,重启后PID=${PID_NEW}"
     elif [ $1 == 'stopall' ];then
        echo "$JAR_NAME:正在停止"
        kill -9 ${PID}
        sleep $STEP
     else
        echo "$JAR_NAME:正在运行,PID=$PID"
     fi
  else
    exec nohup java -Xms512m -Xmx1g -jar -Duser.timezone=GMT+8 $JAR >/dev/null 2>&1 &
    sleep $STEP
    PID=`ps -ef |grep $(echo $JAR | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
    echo "启动时PID:${PID}"
    COUNT=0
    while [ -z "$PID" ]
    do
      if (($COUNT == 30));then
        echo "$JAR_NAME:$(expr $count \* 10)秒内未启动,请检查!"
        break
      fi
      count=$(($count+1))
      echo "$JAR_NAME 启动中.................."
      sleep $STEP
      PID=`ps -ef |grep $(echo $JAR | awk -F/ '{print $NF}') | grep -v grep | awk '{print $2}'`
      echo "启动后循环PID:${PID}"
  done
  echo "$JAR_NAME:已经启动成功,PID=$PID"
  fi
  echo ""
done