java程序启动脚本


#!/bin/bash

source /etc/profile

project=classroom-api
##############################
mkdir -p "/var/run/$project"
mkdir -p "/data0/logs/$project"

pid_file="/var/run/$project/run.pid"
jar_file="${project}.jar"
run_log="/data0/logs/$project/stdout.log"

if [[ "$DADA_ENV" == "production" ]]; then
    java_opts="-Xms4g -Xmx4g"
else
    java_opts="-Xms512m -Xmx512m"
fi

cd /data0/webroot/${project}/
##############################

echo "work dir: "`pwd`
echo "pid file: $pid_file"
echo "jar file: $jar_file"
echo "run log: $run_log"
echo

check_runing()
{
    pid=`cat ${pid_file}` 
    ps aux | grep ${pid} | grep -q ${jar_file}
    echo $?
}


start()
{
    if [ -f ${pid_file} ];then
        res=`check_runing`
        if [ $res -eq 0 ];then
            echo "already running"
            exit 0
        else
            rm -f ${pid_file}
        fi
    fi

    echo "DADA_ENV: ${DADA_ENV}"
    if [[ "$DADA_ENV" == "test" ]]; then
       localIP=0.0.0.0
       nohup java ${java_opts} -javaagent:/jacoco/lib/jacocoagent.jar=includes=com.dadaabc.*,output=tcpserver,port=40023,address=$localIP -jar ${jar_file} >> ${run_log} 2>&1 & echo $! > ${pid_file}
    else
       nohup java ${java_opts} -jar ${jar_file} >> ${run_log} 2>&1 & echo $! > ${pid_file}
    fi

    sleep 3
    res=`check_runing`
    if [ $res -eq 0 ];then
        echo "start OK"
    else
        echo "start Failed"
        rm -f ${pid_file}
    fi
}


stop()
{
    if [ -f ${pid_file} ];then
        res=`check_runing`
        if [ $res -eq 0 ];then
            pid=`cat ${pid_file}` 
            kill -TERM ${pid}
            sleep 3
            res=`check_runing`
            if [ $res -ne 0 ];then
                rm -f ${pid_file}
                echo "stop OK"
            else
                kill -9 ${pid}
                rm -f ${pid_file}
                echo "force stop OK"
            fi
        else
            echo "${pid_file} exists, but ${jar_file} not running"
            rm -f ${pid_file}
        fi
    else
        echo "${jar_file} not running"
    fi

}


usage()
{
    echo "$0 stop"
    echo "$0 start"
    echo "$0 restart"
}

if [ $# -ne 1 ];then
    usage
    exit 1
fi

case $1 in 
    start)
        start;;
    stop)
        stop;;
    restart)
        stop && sleep 1 && start;;
    *)
        usage && exit 1
esac

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM