#!/bin/bash
#
# author:liusha
# description: JAVA APP start|stop|restart|status.
ENV=dev
APP_HOME=`pwd`
APP_NAME=demoApp
RUNNING_USER=root
LOG_DATE=`date +%Y%m%d%H%M%S`dirname $0|grep "^/" >/dev/null#获取APP路径if [[ $? -eq 0 ]]; thenAPP_HOME=`dirname $0`else
dirname $0|grep "^\." >/dev/null:
temp=$?
if [[ ${temp} -eq 0 ]]; thenAPP_HOME=`dirname $0|sed "s#^.#$APP_HOME#"`elseAPP_HOME=`dirname $0|sed "s#^#$APP_HOME/#"`fi
fi#创建日志路径if [[ ! -d "$APP_HOME/logs" ]]; then
mkdir ${APP_HOME}/logsfi#实时日志
LOG_PATH=${APP_HOME}/logs/${APP_NAME}.out
#GC日志信息
GC_LOG_PATH=${APP_HOME}/logs/gc-${APP_NAME}-${LOG_DATE}.log
#JMX监控参数
JMX="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1091 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"#JVM启动参数
JVM_OPTS="-Dname=$APP_NAME -Djeesuite.configcenter.profile=$ENV -Duser.timezone=Asia/Shanghai -Xms512M -Xmx512M -XX:PermSize=256M -XX:MaxPermSize=512M -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps -Xloggc:$GC_LOG_PATH -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC"APP_FILE=${APP_NAME}.jar
pid=0#获取进程号
getPid() {
pid=`ps -ef |grep ${APP_FILE} |grep -v grep |awk '{print $2}'`
}
#启动服务
start() {
getPidif [[ ! -n "$pid" ]]; thenJAVA_CMD="nohup java -jar $JVM_OPTS $APP_FILE >> $LOG_PATH 2>&1 &"
echo "---------------------------------"
su ${RUNNING_USER} -c "$JAVA_CMD"
echo "启动完成,按CTRL+C退出日志界面即可>>>>>"
echo "---------------------------------"
sleep2stail -f ${LOG_PATH}else
echo "$APP_NAME is running PID: $pid"
fi}
#停止服务
stop() {
getPidif [[ ! -n "$pid" ]]; then
echo "$APP_NAME not running"
else
echo "$APP_NAME stop..."
kill -9${pid}fi}
#重启服务
restart() {
stopsleep1s
start
}
#服务状态
status() {
getPidif [[ ! -n "$pid" ]]; then
echo "$APP_NAME not running"
else
echo "$APP_NAME running PID: $pid"
fi}case $1 instart)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;*)echo "$0: Usage: $0 {start|stop|restart|status}"exit1;;esac