转载 [https://my.oschina.net/galenzhang/blog/5088541]

java -verbose:gc -Xloggc:/mnt/gc/spring-demo.jar/gc.20210608_220432.log -XX:+UseG1GC -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/mnt/gc/spring-demo.jar/ -jar spring-demo.jar

#-verbose:gc //输出GC情况
#-Xloggc:filepath  //将GC日志输出到指定文件中
#-XX:+PrintGCDetails  //输出详细的GC情况
#-XX:+HeapDumpOnOutOfMemoryError //表示当JVM发生OOM时,自动生成DUMP文件
#-XX:HeapDumpPath=filepath // 生成DUMP文件的路径

全部脚本

#!/bin/bash
SpringBoot=$2
heapDumpPath="/mnt/gc/$SpringBoot/"
if [ $1 = "" ]
then
    echo  "not enter start-stop-restart-status"
    exit 1
fi

if [ "$SpringBoot" = "" ]
then
    echo -e "no app name"
    exit 1
fi

function start()
{
    count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
    if [ $count != 0 ];then
        echo "$SpringBoot is running..."
    else
         echo "-xx:HeapDumpPath = $heapDumpPath"
         if [ ! -d "$heapDumpPath" ];then
		mkdir -p "$heapDumpPath"
	 fi
         yearMonthDay=`date "+%Y%m%d_%H%M%S"`
         echo "gc log name ==== $yearMonthDay"
         if [ ! -d "${heapDumpPath}gc.${yearMonthDay}.log" ];then
             touch "${heapDumpPath}gc.${yearMonthDay}.log"
         fi
        echo "Start $SpringBoot success..."
         `ls -t | grep $SpringBoot | tail -n +10 | xargs rm -rf`
         BUILD_ID=dontKillMe  nohup java -verbose:gc -Xloggc:"${heapDumpPath}gc.${yearMonthDay}.log" -XX:+UseG1GC -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -XX:+PrintGCDateStamps  -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$heapDumpPath -jar -Dsnow.flake.work.id=1 -Dsnow.flake.datacenter.id=1 $SpringBoot > ./app.log 2>&1 &
    fi
}

function stop()
{
    echo "Stop $SpringBoot"
    boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
    count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`

    if [ $count != 0 ];then
        kill $boot_id
        count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`

        boot_id=`ps -ef |grep java|grep $SpringBoot|grep -v grep|awk '{print $2}'`
        kill -9 $boot_id
    fi
}

function restart()
{
    stop
    sleep 2
    start
}

function status()
{
    count=`ps -ef |grep java|grep $SpringBoot|grep -v grep|wc -l`
    if [ $count != 0 ];then
        echo "$SpringBoot is running..."
    else
        echo "$SpringBoot is not running..."
    fi
}

case $1 in
    start)
    start
	;;
    stop)
    stop
	;;
    restart)
    restart
	;;
    status)
    status
	;;
    *)

    echo -e "command not exist"
esac
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐