Created
September 9, 2016 09:43
-
-
Save songpp/3a7db12adfec4a21e5f8ef61e686b23d to your computer and use it in GitHub Desktop.
java app startup script template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# project artifact: ${project.artifact}-${project.version}.jar | |
# startup script of core | |
# 需要一个参数: 启动的配置文件名称,这个配置文件必须在当前classpath下 | |
# 启动示例: | |
# startup.sh 2551.conf 0 0 listening port specified in config file, disable jmx | |
# startup.sh 2551.conf 3551 listening port is 3551 , disable jmx | |
# startup.sh my.conf 5555 8555 listening port is 5555, jmx port is 8555 | |
# startup.sh 3551.conf 0 4444 listening port specified in config file, jmx port is 4444 | |
# startup.sh test.conf 0 listening port specified in config file, disable jmx | |
if [ "$#" -lt 1 ]; then | |
echo "usage: $0 <target conf file under classpath> <listening port> <jmx port>"; | |
echo " port number: local actor system listening port, default is | |
'0', means use the value specified in config file ('akka.remote.netty.tcp.port')"; | |
echo " jmx port number: enable jmx (e.g. jconsole), default is '0', means disable jmx connection" ; | |
exit 1; | |
fi | |
# JAVA_HOME="" | |
## check JAVA_HOME | |
if [ -z ${JAVA_HOME} ] | |
then | |
# 如果没有设置,则默认设置为默认的路径来尝试 | |
JAVA_HOME="/home/work/local/jdk1.8.0_51" | |
fi | |
if [ ! -e ${JAVA_HOME} ] | |
then | |
echo "JAVA_HOME: ${JAVA_HOME} does not exist" >&2; | |
exit 1; | |
fi | |
SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd); | |
CONF_FILE=$1 | |
PORT=${2:-"0"} | |
JMX_PORT=${3:-"0"} | |
if [[ $PORT =~ '^[0-9]+$' ]] ; then | |
echo "Port is not a number" >&2; | |
exit 1; | |
fi | |
if [[ ${JMX_PORT} =~ '^[0-9]+$' ]] ; then | |
echo "JMX Port is not a number" >&2; | |
exit 1; | |
fi | |
HEAP_DUMP_DIR=${SCRIPT_DIR}/heapdump | |
LOG_DIR=${SCRIPT_DIR}/logs | |
mkdir -p ${LOG_DIR} ${HEAP_DUMP_DIR} | |
# jvm8 options here http://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html | |
# -XX:MaxMetaspaceSize 原PermGen,一般都要设置这个值,因为vm默认无限制 | |
# -XX:+UseConcMarkSweepGC CMS (latency first) 连带默认开启`-XX:+UseParNewGC` (另: 不要禁用ParNewGC) | |
# -XX:+ScavengeBeforeFullGC FullGC之前先回收年轻带,vm默认开启 | |
# -XX:+CMSScavengeBeforeRemark Remark阶段之前尝试回收一次年轻带 | |
# -Dsun.net.inetaddr.ttl=<TTL in seconds> dns cache ttl, jvm default is forever, usually a few minutes in practice | |
# -Djava.rmi.server.hostname=<external IP> rmi stuff, set this if there's multiple ip addresses | |
# -XX:HeapDumpPath 可以指定目录或者文件名,建议指定目录,指定目录时自动生成的文件名里带有pid值 | |
JAVA_OPTS=" -server | |
-Xms1024m -Xmx2048m -Xmn800m -XX:MaxMetaspaceSize=256m | |
-XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled | |
-XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=75 | |
-XX:+ScavengeBeforeFullGC -XX:+CMSScavengeBeforeRemark | |
-XX:+PrintGCDateStamps -verbose:gc -XX:+PrintGCDetails -Xloggc:${LOG_DIR}/gc.${CONF_FILE}.log | |
-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M | |
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${HEAP_DUMP_DIR} " | |
## jmx for jconsole and jvisual | |
JMX_OPTS=" -Dcom.sun.management.jmxremote | |
-Dcom.sun.management.jmxremote.port=${JMX_PORT} | |
-Dcom.sun.management.jmxremote.authenticate=false | |
-Dcom.sun.management.jmxremote.ssl=false " | |
if [[ "${PORT}" != "0" ]] ; then | |
JAVA_OPTS=" ${JAVA_OPTS} -Dakka.remote.netty.tcp.port=${PORT} "; | |
fi | |
if [[ "${JMX_PORT}" != "0" ]] ; then | |
JAVA_OPTS=" ${JAVA_OPTS} ${JMX_OPTS} "; | |
fi | |
echo "use JAVA_HOME: ${JAVA_HOME}\n CONFIG FILE: ${CONF_FILE}" >&2 | |
nohup ${JAVA_HOME}/bin/java \ | |
${JAVA_OPTS} -DAPP_ID=${CONF_FILE} -DLOGBACK_OUT_REF=FILE \ | |
-cp .:${project.artifactId}-${project.version}.jar \ | |
cluster.ClusterStartup -f ${CONF_FILE} > ${LOG_DIR}/core-${CONF_FILE}-nohup.log 2>&1 & | |
echo $! > ${SCRIPT_DIR}/run-${CONF_FILE}.pid | |
echo "write pid to ${SCRIPT_DIR}/run-${CONF_FILE}.pid" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment