jar脚本练习

时间:2022-05-16 17:48:55   收藏:0   阅读:8

javaServer.sh

#!/bin/bash
export JAVA_HOME=/u01/java_home/jdk1.8.0_131
export APP_HOME=/u01/app
export APP_NAME=test.jar
export PROG=test
function getPid {
PID=`ps -ef | grep $APP_HOME/$APP_NAME | grep -v grep | awk {print $2}|awk NR==1`
}
if test ! -d "logs"
then
mkdir "logs"
fi
getPid
case $1 in
    start)
        if [  ! $PID  ];then
        nohup $JAVA_HOME/bin/java -jar $APP_HOME/$APP_NAME >> $APP_HOME/logs/catalina.`date +%Y-%m-%d`.out 2>&1 &
        echo "starting"
        sleep 1
        getPid
        echo -e "$PROG is \033[32mActive\033[0m, PID = $PID"
        else
        echo "$PROG is exist, PID = $PID"
        fi
        ;;
    stop)
        if [  $PID ];then
        kill -15 $PID
        echo "stoping..."
        sleep 1
        getPid
        echo -e "$PROG is \033[31mDead\033[0m"
        else
        echo "$PROG is not exist"
        fi
        ;;
    restart)
        echo "restarting..."
        if [  $PID ];then
        kill -15 $PID
        sleep 3
        nohup $JAVA_HOME/bin/java -jar $APP_HOME/$APP_NAME >> $APP_HOME/logs/catalina.`date +%Y-%m-%d`.out 2>&1 &
        getPid
        echo -e "$PROG is \033[32mRestart\033[0m, PID = $PID"
        else
        nohup $JAVA_HOME/bin/java -jar $APP_HOME/$APP_NAME >> $APP_HOME/logs/catalina.`date +%Y-%m-%d`.out 2>&1 &
        sleep 1
        getPid
        echo -e "$PROG is \033[32mRestart\033[0m, PID = $PID"
        fi
        ;;
    init)
        echo "init $(basename $0) scripts..."
        SET_APP_HOME=`pwd`
        echo "set APP_HOME=`pwd`"
        read -p "change jar APP home (yes|no)?" INPUT
        while true ;do
        case $INPUT in
        y|yes|‘‘)
        sed -i "s#^export APP_HOME=$APP_HOME#export APP_HOME=$SET_APP_HOME#g" $0
        break
        ;;
        n|no)
        echo "APP_HOME=$APP_HOME"
        break
        ;;
        *)
        echo -n "Input ERROR,please input yes or no? "
        read INPUT
        ;;
        esac
        done

        JAR_NUM=$(find $(pwd) -name "*.jar"|wc -l)
        if test $JAR_NUM -eq 1;
        then
        SET_APP_NAME=$(basename $(find $(pwd) -name "*.jar"))
        echo "set APP_NAME=$SET_APP_NAME"
        read -p "change jar APP name (yes|no)?" INPUT
        while true ;do
        case $INPUT in
        y|yes|‘‘)
        sed -i "s#^export APP_NAME=$APP_NAME#export APP_NAME=$SET_APP_NAME#g" $0
        break
        ;;
        n|no)
        echo "APP_NAME=$APP_NAME"
        break
        ;;
        *)
        echo -n "Input ERROR,please input yes or no? "
        read INPUT
        ;;
        esac
        done
        else
        echo "jar app numer=$JAR_NUM is ERROR! please check."
        fi
        ;;
    *)
    echo "Usage:{$0 start|stop|restart}"
    ;;
esac
exit 0

 

原文:https://www.cnblogs.com/wxp100/p/15333175.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!