Skip to content

Instantly share code, notes, and snippets.

@crfilho
Created September 19, 2016 16:30
Show Gist options
  • Save crfilho/99d9bf9bf402903333c3f670a45e5a53 to your computer and use it in GitHub Desktop.
Save crfilho/99d9bf9bf402903333c3f670a45e5a53 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Startup script for java jar applications
##########################################
### SET GC PARAMETERS #####
##########################################
#Specify the minimum java version
MIN_JAVA_VERSION = 17
#Specify the initial heap size of the application
JAVA_HOME=${JAVA_HOME:-/usr/jdk/latest/}
#Specify the initial heap size of the application
HEAPSIZE=2048
#Specify the heap for young objects
YOUNGSIZE=256
#Specify the platform type ( 32 or 64 bit)
PLATFORM=64
# Default size of memory allocated to store the classes metadata (metaspace for JDK 8 and above, permgen for older JDK version)
CLASSESMETADATASIZE=256
# Special native memory space (not GC managed) used to store compiled code from the JIT
CODECACHESIZE=128
# Jar file
JAR_FILE=myapp.jar
# Properties File
PROPFILE=myapp.properties
# Process Name
SERVICE_NAME=MYAPP
# Main class name
MAIN_CLASS=com.myapp.Main
# Find where this script is located
WHEREAMI=`dirname $0`
# Find dirs we need
HOME=${_HOME:-$WHEREAMI/..}
# Put all third party jars in /home/lib
LIB=${_LIB:-$HOME/lib}
#
# Makes sure that the file passed in argument are available in the $BIN directory.
# Exits on failure.
#
checkFile()
{
for i in $1;
do
if [ ! -f $HOME/$i ];
then
printf "*** Error: Unable to find configuration file \$HOME/%s\n*** Exiting\n" $i
exit 1;
fi
done
}
#
# Init the main jar file and goes through all files within $LIB directory and appends into JARFILE var
#
initJarLibs()
{
JARFILES=$LIB/$JAR_FILE
for jar in $LIB/*.jar;
do
if [ ! -z "$VERBOSE" ];
then
printf "\t\$LIB/%s\n" `basename $jar`
fi
if [ $jar != $LIB/$JAR_FILE ];
then
if [ -z "$JARFILES" ] ;
then
JARFILES=$jar
else
JARFILES=$JARFILES:$jar
fi
fi
done
}
# Run the Service
# --------------------------------------
if [ ! -z "$VERBOSE" ];
then
printf "\n=== Running Application ===\n\n"
fi
# Look for jar file
checkFile JAR_FILE
# Look for properties file
checkFile PROPFILE
# Init all jar files available
initJarLibs
######### START DATE TIME #####
year=`date '+%y'`
month=`date '+%m'`
day=`date '+%d'`
hour=`date '+%H'`
minute=`date '+%M'`
time=$hour$minute
date=$year$month$day
###############################
##### GET JAVA VERSION ########
JAVA_VERSION=$(java -version 2>&1 | sed 's/java version "\(.*\)\.\(.*\)\..*"/\1\2/; 1q')
echo =============================================
echo === Running SERVICE ===
echo =============================================
##################################################
################# JVM OPTIONS ##################
##################################################
JVM_OPTIONS="-XX:CompileThreshold=1000 -XX:-UseCounterDecay"
# Specific parameters depending on java version
if [ $MIN_JAVA_VERSION -gt $JAVA_VERSION ]
then
printf "*** Error: MINIMUM REQUIRED JAVA VERSION: $MIN_JAVA_VERSION ****"
exit 1;
elif [ $JAVA_VERSION -ge 18 ] # JAVA VERSION 1.8.0 or newer
then
CLASSESMETADATASIZE_OPTION="-XX:MetaspaceSize"
CLASSESMETADATAMAXSIZE_OPTION="-XX:MaxMetaspaceSize"
HTTPSERVER_READTIMEOUT_OPTION="-Dsun.net.httpserver.maxReqTime"
USEPARNEWGC_OPTION=""
TIERED_COMPILATION_SWITCH="-XX:-TieredCompilation"
elif [ $JAVA_VERSION -ge 17 ] # JAVA VERSION 1.7.0 or newer
then
CLASSESMETADATASIZE_OPTION="-XX:PermSize"
CLASSESMETADATAMAXSIZE_OPTION="-XX:MaxPermSize"
HTTPSERVER_READTIMEOUT_OPTION="-Dsun.net.httpserver.maxReqTime"
USEPARNEWGC_OPTION="-XX:+UseParNewGC"
TIERED_COMPILATION_SWITCH=""
fi
PLATFORMOPTION=""
DATAMODEL=""
if [ $PLATFORM -eq 64 ];
then
PLATFORMOPTION=-XX:+UseCompressedOops
DATAMODEL=-d64
fi
$JAVA_HOME/bin/java ${DATAMODEL} \
-D${SERVICE_NAME} \
${PLATFORMOPTION} \
-server \
-Xms${HEAPSIZE}M \
-Xmx${HEAPSIZE}M \
${CLASSESMETADATASIZE_OPTION}=${CLASSESMETADATASIZE}m \
${CLASSESMETADATAMAXSIZE_OPTION}=${CLASSESMETADATASIZE}m \
-XX:NewSize=${YOUNGSIZE}m \
-XX:MaxNewSize=${YOUNGSIZE}m \
-XX:InitialCodeCacheSize=${CODECACHESIZE}m \
-XX:ReservedCodeCacheSize=${CODECACHESIZE}m \
-XX:-UseBiasedLocking\
${TIERED_COMPILATION_SWITCH} \
$JVM_OPTIONS \
${MAIN_CLASS} &
set +x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment