jar包启动脚本

hcwei 2020年09月10日 361次浏览
#!/bin/bash

# java路径
JAVA_BIN="/mnt/installer/jdk1.8.0_131/bin/java"

# 获取动作
FUNCTION=$1

# 获取包名(stop时只需输入包名;start时需要输入包全路径)
APP_NAME=$2

# JVM调优参数
JVM_OPS=$3

# 启动参数
JAR_OPS=$4

is_exist(){
    PID=`ps -ef|grep ${APP_NAME}|grep -v grep|grep -v "java_manage.sh"|awk '{print$2}'`
    #如果不存在返回1,存在返回0
    if [ -z "${PID}" ]
    then
        return 1
    else
        return 0
    fi
}

start(){
    is_exist
    if [ $? -eq "0" ]; then
        echo "${APP_NAME} is already running. pid=${PID} ."
    else
        nohup ${JAVA_BIN} -jar ${JVM_OPS} ${APP_NAME} ${JAR_OPS} >/dev/null 2>&1 &
        sleep 3
        is_exist
        if [ $? -eq "0" ]; then
            echo "${APP_NAME} start success"
        else
            echo "${APP_NAME} start failed !!!"
            exit 2
        fi
    fi
}

stop(){
    is_exist
    if [ $? -eq "0" ]; then
        kill -9 ${PID}
        echo "${APP_NAME} is stoped"
    else
        echo "${APP_NAME} is not running"
    fi
}

case "${FUNCTION}" in
start)
  start
  ;;
stop)
  stop
  ;;
restart)
  stop
  start
  ;;
*)
  printf "Explain: JAR_URL:jar包绝对路径; JVM_OPS:jar包前参数可以为空(JVM参数,启动环境等); JAR_OPS:jar包后参数可以为空(启动密钥信息等)\n"
  printf "Usage: `basename $0` %s {start|stop|restart} JAR_URL JVM_OPS JAR_OPS\n"
  printf "Example: ./java_manage.sh start /tmp/xxx.jar '-Xms64m -Xmx512m' '--jasypt.encryptor.password=xxx'\n"
  exit 1
  ;;
esac