通用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