JVM部署脚本

为了方便jenkins部署,笔者总结了部署脚本如下(该脚本附有动态计算机器内核以及线程数对应相关物理内存匹配JVM较为优化的参数,使用G1垃圾收集器,生产GC日志收集,DUMP出OOM日志when OOM 等,此时已不具有年老代,需要注意。另外部署监控端口冲突策略,友好TAB提示窗口,或者定制化端口重用策略等)

##!/bin/bash
## Copyright for xxxxxx coporation 
## Time  2019-6-18
## Auther  jack.lei 
#
#
#
#
#
#
#|=| Check whether the port is duplicated here,
#|=| avoiding repeated startup and saving resources. 
#|=| The port is passed in through the second parameter.

if [ -z $2  ]

then 

  echo "Port char is not empty ..."
	exit 1
fi



PENDING_KILL_PORT=`netstat -plnt | awk '/'$2'/{print $7}' | awk -F "/" '{print $1}'`

echo The scanned PID is  $PENDING_KILL_PORT

if [ -n "$PENDING_KILL_PORT" ];then
    
     if [ -z $3 ];then
          
        dialog --msgbox "The current port is already occupied. Please check the port usage." 20 50
        exit 1
    else
        dialog --msgbox "Now it will forcefully close the port that is already occupied and start the new service." 20 50
        kill -9 $PENDING_KILL_PORT

    fi
else

  echo  "No port duplication, normal startup . . ." 

fi




#if [[ -z $PEND_KILL_PORT && -z $3 ]]

#  then
#            dialog --msgbox "The current port is already occupied. Please check the port usage." 20 50
#            exit 1             
#  else 
		
#            dialog --msgbox "Now it will forcefully close the port that is already occupied and start the new service." 20 50
#            kill -9 $PEND_KILL_PORT
#fi
#
#
#
#
#
#
#
#
# Gather Current Date
CURRENT=`date "+%Y-%m-%d"` 
#
# Splicing the current DUMP file path 
JVM_PATH=$HOME/jvm/dump/
DUMP_PATH=$JVM_PATH
#
#Create a dump file
if [[ -f $DUMP_PATH &&  -e $DUMP_PATH ]]
then  
   echo System overflow frequency is too high

else 
   mkdir -p $DUMP_PATH
fi 
#
#
#      
#
#Get the number of CPUs corresponding to the current server kernel
kernel_cpu=`cat /proc/cpuinfo| grep "processor"| wc -l`
echo  "The current number of server cores is $kernel_cpu"
#
#
#Caculate current server maximum memory RAM
local_memory=`free | sed -n '2p' | awk  '{print $2}'`
base_memory=`expr $local_memory / 1024 / 1024`
echo " now sys memory is $base_memory G"
#
MEMORY_SINK=`expr $base_memory / 1024 / 4` 
MEMORY_XMN=`expr $MEMORY_SINK / 3`
kernel=`expr $kernel_cpu \* 4`
#
#
#Start the JVM script
#
#
#
#
#Input parameter check here
if [ -z  $1  ]
 then  
    echo "Please pass in the name of the jar file to be executed."
    exit 1
    

fi
#
#
java -jar   target/$1  
-Xmx$MEMORY_SINKm 
-Xms$MEMORY_SINKm
-Xmn$MEMORY_XMNm
-XX:SurvivorRatio=10
#
#
#Use the latest garbage collector here
-XX:+UseG1GC(开启G1)
-XX:MaxTenuringThreshold=10
-XX:ParallelGCThreads=$kernel
-XX:ConcGCThreads=$kernel
-XX:+DisableExplicitGC
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=$DUMP_PATH
-XX:+PrintGCDetails
-XX:+PrintGCTimeStamps
-XX:+PrintHeapAtGC
-XX:+TraceClassLoading
-XX:+PrintClassHistogram
#查看服务端启动-XX详情
-XX:+UnlockExperimentalVMOptions
-XX:+UnlockDiagnosticVMOptions
-XX:+PrintFlagsFinal
#(JIT编译详情)
-XX:+PrintCompilation

简单使用方法如下:

start_config_jvm.sh这个脚本接受三个参数

解释如下:

第一个参数为你编译以后的JAR包的名字,不需要绝对路经直接名字就好(*)

第二个参数为你当前服务需要启动的端口,与SERVER POINT一致(*)

第三个参数是如果有值(现阶段传什么都行,建议传容易区别的数值例如1),脚本在检测有端口占用的时候会直接提示强制关闭冲突端口重启服务避免服务底层异常节约资源。反之如果不传,那么脚本默认认为端口冲突,会停止运行,并基于提示.

第三个参数友好的提供给外部入口是否强制控制端口冲突。

 我的额外使用脚本如下:


if [ -e 'error.txt' ]
 then  
      rm -rf  error.txt  
fi


sh start_config_jvm.sh  spark_xxx-0.0.1-SNAPSHOT.jar 8080 1  2> error.txt

 

你可能感兴趣的:(shell,脚本)