使用kill -15优雅停止进程

以下脚本生产上可直接使用

#!/bin/bash
source /etc/profile

# 应用存放地址
APP_HOME=/data/app/$1
PORT=$2
# 应用名称jarname:
APP_NAME=$1

# Shell Info
# 使用说明,用来提示输入参数
usage() {
    echo "Usage: sh boot [APP_NAME].jar [PORT] [start|stop|restart|status]"
    exit 1
}

# 检查程序是否在运行
is_exist(){
        # 获取PID
        PID=$(ps -ef |grep ${APP_NAME} |grep ${PORT} | grep -v $0 |grep -v grep |awk '{print $2}')
        if [ -z "${PID}" ]; then
                # 如果进程不存在返回1
                return 1
        else
                # 进程存在返回0
                return 0
        fi
}

# 定义启动程序函数
start(){
        is_exist
        if [ $? -eq "0" ]; then
                echo "${APP_NAME} is already running, PID=${PID}"
        else
                nohup java -javaagent:/data/skywalking-agent/skywalking-agent.jar=agent.service_name=yecai_uat::${APP_NAME}_uat -server -Xms512m -Xmx512m -Xmn128m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=512m -XX:SurvivorRatio=6 -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=80 -XX:+ScavengeBeforeFullGC -XX:+CMSScavengeBeforeRemark -XX:+PrintGCDateStamps -verbose:gc -XX:+PrintGCDetails -Xloggc:gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=dump.hprof  -jar  ${APP_HOME}/${APP_NAME}.jar --server.port=${PORT} --spring.profiles.active=uat > /dev/null 2>&1 &
                PID=$(echo $!)
                echo "${APP_NAME} start success, PID=$!"
        fi
}

# 停止进程函数
stop(){
# 使用kill -15优雅判断进程是否停止
        num=0
        while true
        do
           if [ $num -gt 30 ];then
               sudo kill -9 ${PID}
               echo "${APP_NAME} process stop, PID=${PID}"
               break
           fi
           let num+=1
           is_exist
           if [ $? -eq "0" ]; then
                sudo kill -15 ${PID}
                echo "${APP_NAME} process is stoping, PID=${PID}"
                sleep 1
           else
                echo "There is not the process of ${APP_NAME}"
                break
           fi
       done
}

# 重启进程函数
restart(){
        stop
        start
}

# 查看进程状态
status(){
        is_exist
        if [ $? -eq "0" ]; then
                echo "${APP_NAME} is running, PID=${PID}"
        else
                echo "There is not the process of ${APP_NAME}"
        fi
}

case $3 in
"start")
        start
        ;;
"stop")
        stop
        ;;
"restart")
        restart
        ;;
"status")
       status
        ;;
        *)
        usage
        ;;
esac
exit 0
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值