当前位置: 首页 > 工具软件 > jail-shell > 使用案例 >

Shell-启动jar包(java程序)脚本

笪成周
2023-12-01
#!/bin/sh
###################################
#环境变量及程序执行参数
#需要根据实际环境以及Java程序名称来修改这些参数
###################################
#JDK所在路径
JAVA_HOME=/home/showyang/projects/jdk1.8.0_151
LICENSE_FILE=$JSIFS_3/agent
export LD_LIBRARY_PATH=$LICENSE_FILE 
 
#Java程序所在的目录
APP_HOME=$(cd `dirname $0`; pwd -P)
 
#需要启动的Java主程序(main方法类)
APP_MAINCLASS=showyang-center-task.jar
APP_NAME=$APP_HOME/$APP_MAINCLASS
NACOS="
--nacos.url=10.10.111.6:8848
--nacos.namespace=showyang
--nacos.username=showyang
--nacos.password=123456
"
OPENFIRE_RESOURCE="--openfire.resource=master"
CONFIG_LOG="--logging.config=$APP_HOME/logback-spring.xml"
 
###################################
LICENSE_FILE=/home/showyang/projects/project/springboot/agent
export LD_LIBRARY_PATH=$LICENSE_FILE

usage() {
    echo "Usage: sh manage.sh [start|stop|status|restart]"
    exit 1
}

kills(){
    tpid=`ps -ef|grep $APP_MAINCLASS|grep -v grep|grep -v kill|awk '{print $2}'`
    if [[ $tpid ]]; then
        echo 'Kill Process!'
        kill -9 $tpid
    fi
}

start(){
    #nohup java -jar myapp.jar > /dev/null 2>&1 &
    tpid=`ps -ef|grep $APP_MAINCLASS|grep -v grep|grep -v kill|awk '{print $2}'`
    if [ ${tpid} ]; then
        echo '程序运行中,请先停止程序再启动!'
        exit 1
    fi
    nohup $JAVA_HOME/bin/java -agentlib:agent=$LICENSE_FILE -agentpath:$APP_HOME/libdecrypt.so -Xms512m -Xmx512m -jar $APP_NAME $NACOS $OPENFIRE_RESOURCE $CONFIG_LOG > /dev/null 2>&1 &
    sleep 3
    tpid=`ps -ef|grep $APP_MAINCLASS|grep -v grep|grep -v kill|awk '{print $2}'`
    if [ ${tpid} ]; then
        echo '启动成功,进程ID:'${tpid}
    else
        echo '启动失败,请检查原因!'
    fi
}

stop(){
    tpid=`ps -ef|grep $APP_MAINCLASS|grep -v grep|grep -v kill|awk '{print $2}'`
    if [ ${tpid} ]; then
        echo '开始停止程序!'
        kill -15 $tpid
    fi
    sleep 5
    tpid=`ps -ef|grep $APP_MAINCLASS|grep -v grep|grep -v kill|awk '{print $2}'`
    if [ ${tpid} ]; then
        echo '开始强制kill进程!'
        kill -9 $tpid
    else
        echo '程序停止成功!'
    fi

}

status(){
    tpid=`ps -ef|grep $APP_MAINCLASS|grep -v grep|grep -v kill|awk '{print $2}'`
    if [ ${tpid} ]; then
        echo 'App is running.'
    else
        echo 'App is NOT running.'
    fi
}

#重启
restart(){
  stop
  sleep 2
  start
}


case "$1" in
    "start")
        start
        ;;
    "stop")
        stop
        ;;
    "kill")
        kills
        ;;
    "status")
        status
        ;;
    "restart")
        restart
        ;;
     *)
        usage
        ;;
esac
 类似资料: