centos启动jar脚本

简介: centos启动jar脚本
#!/bin/bash
# 应用名
APP_NAME=application
# 应用端口
APP_PORT=8081
# 应用健康检查URL
HEALTH_CHECK_URL=http://127.0.0.1:${APP_PORT}/v2/api-docs
# jar所在的根目录
APP_HOME=/home/admin/${APP_NAME} 
# jar的绝对路径
JAR_NAME=${APP_HOME}/api-0.0.1-SNAPSHOT.jar
#应用的启动日志
JAVA_OUT=${APP_HOME}/logs/start.log
PROG_NAME=$0
ACTION=$1
APP_START_TIMEOUT=20    # 等待应用启动的时间
# 创建出相关目录
mkdir -p ${APP_HOME}
mkdir -p ${APP_HOME}/logs
usage() {
    echo "Usage: $PROG_NAME {start|stop|restart}"
    exit 2
}
health_check() {
    exptime=0
    echo "checking ${HEALTH_CHECK_URL}"
    while true
        do
            status_code=`/usr/bin/curl -L -o /dev/null --connect-timeout 5 -s -w %{http_code}  ${HEALTH_CHECK_URL}`
            if [ "$?" != "0" ]; then
               echo -n -e "\rapplication not started"
            else
                echo "code is $status_code"
                if [ "$status_code" == "200" ];then
                    break
                fi
            fi
            sleep 1
            ((exptime++))
            echo -e "\rWait app to pass health check: $exptime..."
            if [ $exptime -gt ${APP_START_TIMEOUT} ]; then
                echo 'app start failed'
               exit 1
            fi
        done
    echo "check ${HEALTH_CHECK_URL} success"
}
start_application() {
    echo "starting java process"
    nohup java -jar ${JAR_NAME} > ${JAVA_OUT} 2>&1 &
    echo "started java process"
}
stop_application() {
   checkjavapid=`ps -ef | grep java | grep ${APP_NAME} | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'`
   if [[ ! $checkjavapid ]];then
      echo -e "\rno java process"
      return
   fi
   echo "stop java process"
   times=60
   for e in $(seq 60)
   do
        sleep 1
        COSTTIME=$(($times - $e ))
        checkjavapid=`ps -ef | grep java | grep ${APP_NAME} | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'`
        if [[ $checkjavapid ]];then
            kill -9 $checkjavapid
            echo -e  "\r        -- stopping java lasts `expr $COSTTIME` seconds."
        else
            echo -e "\rjava process has exited"
            break;
        fi
   done
   echo ""
}
start() {
    start_application
    health_check
}
stop() {
    stop_application
}
case "$ACTION" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        stop
        start
    ;;
    *)
        usage
    ;;
esac
相关文章
|
4月前
|
Linux
CentOS7.9服务器一键脚本部署FRP内网穿透服务端与客户端
CentOS7.9服务器一键脚本部署FRP内网穿透服务端与客户端
245 0
|
5月前
|
Java Linux Docker
centos7 开机自启动自定义脚本
centos7 开机自启动自定义脚本
|
7月前
|
Java Linux 网络安全
【Linux环境】Centos 7启动jar包的详细步骤
【Linux环境】Centos 7启动jar包的详细步骤
222 0
|
8月前
|
缓存 运维 Linux
Linux(CentOS)运维脚本工具集合
Linux(CentOS)运维脚本工具集合
150 2
|
9月前
|
Java Linux Shell
Centos8一键启动多个Springboot jar包
Centos系统一键启动多个Springboot jar包
159 0
|
6天前
|
Java Shell 应用服务中间件
centos7_tomcat开机自启的shell脚本参考
centos7_tomcat开机自启的shell脚本参考
8 0
|
7天前
|
Shell
CentOS6.5自动化安装LAMP脚本
CentOS6.5自动化安装LAMP脚本
|
2月前
|
Java Shell API
通用Shell脚本执行Spring Boot项目Jar包
通用Shell脚本执行Spring Boot项目Jar包
|
3月前
|
Java Linux
Centos8一键启动多个Springboot jar包 改进版
Centos8一键启动多个Springboot jar包 改进版
|
4月前
|
Linux Shell
开源日志平台GrayLog5.1.10 CentOS7一键安装脚本
开源日志平台GrayLog5.1.10 CentOS7一键安装脚本
103 0