# java env
export JAVA_HOME=/opt/jdk1.8.0_11
 
# App Info
# 应用存放地址
APP_HOME=/cjbdi/hubeiDataAnalysisBackend
# 应用名称
APP_NAME=$1
TIME=$(date +%Y-%m-%d)
 
# Shell Info
 
# 使用说明,用来提示输入参数
usage() {
    # echo "Usage: sh boot [APP_NAME] [start|stop|restart|status]"
    echo "Usage: bootjar.sh [APP_NAME] [start|stop|restart|status]"
    exit 1
}
 
 
# 检查程序是否在运行
is_exist() {
    # 获取PID
    PID=$(ps -ef | grep ${APP_NAME} | grep -v $0 | grep -v grep | awk '{print $2}')
    # -Z "${pid}"判断pid是否存在,如果不存在返回1,存在返回0
    if [ -z "${PID}" ]; then
        # 如果进厂不存在返回1
        return 1
     else
        # 进程存在返回0
        return 0
    fi
}
 
 
#定义启动程序函数
start(){
    is_exist
    if [ $? -eq "0" ]; then
        echo "${APP_NAME} is already running, PID=${PID}"
    else
        nohup ${JAVA_HOME}/bin/java -jar ${APP_HOME}/${APP_NAME} >> logs/${APP_NAME%.*}-$TIME.log 2>&1 &
        PID=(echo $!)
        echo "${APP_HOME} start success, PID=$!"
    fi
}
 
 
#停止进程函数
stop(){
    is_exist
    if [ $? -eq "0" ]; then
        kill -9 ${PID}
        echo "${APP_HOME} process stop, PID=${PID}"
    else
        echo "There is no the process of ${APP_NAME}"
    fi
 
}
 
# 重启进程函数
restart(){
    stop
    start
}
 
 
# 查看进程状态
status(){
    is_exist
    if [ $? -eq "0" ]; then
        echo "${APP_NAME} is running, PID=${PID}"
    else
        echo "There is no the process of ${APP_NAME}"
    fi
}
 
 
case $2 in
"start")
        start
        ;;
"stop")
        stop
        ;;
"restart")
        restart
        ;;
"status")
       status
        ;;
    *)
    usage
    ;;
esac
exit 0