SpringBoot项目启动停止脚本

小熊博客
2021-05-21 / 0 评论 / 313 阅读
温馨提示:
本文最后更新于2021-05-21,若内容或图片失效,请留言反馈。
#!/bin/bash
appName=`ls|grep .jar$`
if [ -z $appName ]
then
    echo "同级目录下未查询到jar"
    exit 1
fi
killForceFlag=$2
function start()
{
    count=`ps -ef |grep java|grep $appName|wc -l`
    if [ $count != 0 ];then
        echo " $appName 正在运行中,请先停止"
    else
        echo "$appName 启动成功"
        nohup java -XX:+UseG1GC -XX:+HeapDumpOnOutOfMemoryError -Xms512M -Xmx4G -jar $appName > /dev/null 2>&1 &
    fi
}
function stop()
{
    appId=`ps -ef |grep java|grep $appName|awk '{print $2}'`
    if [ -z $appId ]
    then
        echo "$appName 没有启动,请检查"
    else
        echo -n "$appName 正在停止"
        if [ "$killForceFlag" == "-f" ]
        then
            echo "强制停止"
            kill -9 $appId
        else
            echo
            kill $appId
        fi
    fi
}
function status()
{
    appId=`ps -ef |grep java|grep $appName|awk '{print $2}'`
    if [ -z $appId ]
    then
        echo -e "\033[31m Not running \033[0m"
    else
        echo -e "\033[32m Running [$appId] \033[0m"
    fi
}
function restart()
{
    stop
    for i in {3..1}
    do
        echo -n "$i "
        sleep 1
    done
    echo 0
    start
}
function usage()
{
    echo "Usage: $0 {start|stop|restart|status|stop -f}"
    echo "Example: $0 start"
    exit 1
}
case $1 in
    start)
    start;;
    stop)
    stop;;
    restart)
    restart;;
    status)
    status;;
    *)
    usage;;
esac

使用方法

命令作用
./app.sh start启动
./app.sh stop停止
./app.sh restart重启
./app.sh stop -f强制杀死进程