Skip to content

Instantly share code, notes, and snippets.

@ChinaXing
Last active August 29, 2015 14:06
Show Gist options
  • Save ChinaXing/6db9c4faa95d4092aa38 to your computer and use it in GitHub Desktop.
Save ChinaXing/6db9c4faa95d4092aa38 to your computer and use it in GitHub Desktop.
reusable app control shell scripts - start/stop app
#!/bin/bash
function getWorkDir
{
dir=$(dirname $0)
cd $dir || {
echo "$dir not eixt."
exit 1
}
cd ..
}
function getLogDir
{
LOG_DIR=logs/
[ -d $LOG_DIR ] || mkdir -p $LOG_DIR
}
function prepareJVMOption
{
HEAP_SIZE="$((4*1024))m"
ENDEN_SIZE="$((2*1024))m"
PERM_SIZE="$((320))m"
JAVA_OPT="$JAVA_OPT -server -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCApplicationConcurrentTime"
JAVA_OPT="$JAVA_OPT -verbose:gc -Xloggc:$LOG_DIR/gc.log -XX:-UseGCLogFileRotation -XX:+DisableExplicitGC" #-XX:PrintHeapAtGC
JAVA_OPT="$JAVA_OPT -Xnoclassgc -Xms$HEAP_SIZE -Xmx$HEAP_SIZE -Xmn$ENDEN_SIZE"
JAVA_OPT="$JAVA_OPT -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:CMSInitiatingOccupancyFraction=70 -XX:+CMSParallelRemarkEnabled -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+CMSClassUnloadingEnabled -XX:SurvivorRatio=8"
JAVA_OPT="$JAVA_OPT -XX:PermSize=$PERM_SIZE -XX:MaxPermSize=$PERM_SIZE"
JAVA_OPT="$JAVA_OPT -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$LOG_DIR/oom.hprof -XX:ErrorFile=$LOG_DIR/hs_error%p.log"
JAVA_OPT="$JAVA_OPT -XX:+UseBiasedLocking -XX:+UseCompressedOops"
}
function enableJmxRemote
{
local port=${1:-9999}
JAVA_OPT="$JAVA_OPT -Dcom.sun.management.jmxremote.port=$port"
JAVA_OPT="$JAVA_OPT -Dcom.sun.management.jmxremote.authenticate=false"
JAVA_OPT="$JAVA_OPT -Dcom.sun.management.jmxremote.ssl=false"
}
function enableDebug
{
local port=${1:-8000}
JAVA_OPT="$JAVA_OPT -agentlib:jdwp=transport=dt_socket,address=*:$port"
}
function usage
{
echo "$0 start|stop"
exit 1
}
function getJava
{
if [ -n "$JAVA_BIN" ]
then
return
fi
if [ -n "$JAVA_HOME" ]
then
JAVA_BIN=$JAVA_HOME/bin/java
return
fi
JAVA_BIN=java
}
function doStart
{
if [ -f $PID_FILE ] && [ -s $PID_FILE ]
then
echo "already started PID = $(cat $PID_FILE)"
exit 0
fi
getJava
if [ "x$1" = "xdebug" ]
then
shift
enableDebug "$@"
elif [ "x$1" = "xjmxremote" ]
then
shift
enableJmxRemote "$@"
fi
nohup $JAVA_BIN $JAVA_OPT -DAPP_HOME=. -jar $JAR_FILE 1>$LOG_DIR/stdout.log 2>$LOG_DIR/stderr.log &
RT=$? PID=$!
if [ $RT -eq 0 -a -n "$!" ]
then
echo "$PID" > $PID_FILE
sleep 1
if ! [ -d /proc/$PID ]
then
echo "Start failure !, see log for more detail."
rm -f $PID_FILE
return
fi
echo "Start succeed, PID = $PID"
fi
}
function stopByPid
{
local PID=$1
echo "Stop with PID = $PID"
for i in `seq 5`
do
echo -e -n "try to stop by kill SIGTERM .. $i\r"
tryStop $PID
done
echo "Stop failured after retry 5 times "
}
function tryStop
{
local PID=$1
if ! [ -d /proc/$PID ]
then
[ -e $PID_FILE ] && rm -f $PID_FILE
echo -e "\nStopped!"
exit 0
fi
kill $PID
sleep 5
if ! [ -d /proc/$PID ]
then
[ -e $PID_FILE ] && rm -f $PID_FILE
echo -e "\nStopped!"
exit 0
fi
}
function doStop
{
if [ -f $PID_FILE ] && [ -s $PID_FILE ]
then
PID=$(cat $PID_FILE)
else
PID=$(ps ax | grep java | grep -v grep | grep $JAR_FILE | awk '{print $1}' | head -1)
fi
if [ -z $PID ]
then
echo "already stopped !"
exit 0
fi
stopByPid $PID
}
function init
{
PID_FILE="bin/run.pid"
JAR_FILE="lib/db-mq-1.0-SNAPSHOT.jar"
getWorkDir
getLogDir
prepareJVMOption
}
[ $# -ge 1 ] || usage
init
if [ $1 = "start" ]
then
shift
doStart "$@"
exit
fi
if [ $1 = "stop" ]
then
doStop
exit
fi
usage
#### END ###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment