1. 程式人生 > >通用jar包啟動腳本

通用jar包啟動腳本

pwd 放置 環境變量 basename turn bash else $? restart

通用jar包啟動腳本
  1. 使用方法:

把本腳本和jar包放置在同一目錄,並保證該目錄下只有一個jar包。
jar包的輸出文件名為“nohup.當前目錄名"

  1. 運行jar包:

腳本名後跟start|stop|restart|status即可

  1. 腳本內容:
#!/bin/bash
#######################
#腳本使用方法:
#本腳本和jar包在同一目錄下,保證當前目錄有且只有一個jar包即可。無需修改任何東西。
# Usage: $0 {start|stop|status|restart}
#######################
#可在此處添加需要加載的環境變量
#######################

CurrentPath=`pwd`
CurrentDirName=`basename $(pwd)`
if ls *.jar &> /dev/null ;then
        if [ `ls *.jar | wc -l` -eq 1 ];then
                APP_NAME=`find $CurrentPath -maxdepth 1 -name "*.jar"`
        else
                echo -e "\033[31m------   Error! Too Much JAR Packages In This Directory !\033[0"
                exit 2
        fi
else
        echo -e "\033[31m------   Error! No JAR Package In Current File !\033[0"
        exit 1
fi

is_exist () {
    pid=`ps aux | grep $APP_NAME | grep -v grep | grep "jar$"| awk ‘{print $2}‘`
    if [ -z "$pid" ];then
        return 1
    else
        return 0
    fi
}
start () {
    is_exist
    if [ $? -eq 0 ];then
        echo "${APP_NAME} is already running. pid=${pid} ."
    else
        nohup java -jar $APP_NAME &> nohup.$CurrentDirName &
    fi
}
stop () {
    is_exist
    if [ $? -eq 0 ];then
        kill  $pid
    else
        echo "${APP_NAME} is not running"
    fi
}
status () {
    is_exist
    if [ $? -eq 0 ];then
        echo "${APP_NAME} is running. Pid is ${pid}"
    else
        echo "${APP_NAME} is not running"
    fi
}
case $1 in
    start)
        start;;
    stop)
        stop;;

    restart)
        stop
        start;;
    status)
        status;;
    *)
        echo "Usage: $0 {start|stop|status|restart} "
esac

通用jar包啟動腳本