java启动jar包的shell脚本,常见的配置jvm参数方案

针对普通的可执行的jar文件,提供启动,停止,重启等功能
配置JVM参数: 打印GC日志,当内存溢出OOM时导出一份heapdump

假设脚本名字叫app.sh
那么:

  1. 给app.sh添加可执行权限: chmod +x app.sh
  2. 启动项目: ./app.sh start
  3. 停止项目: ./app.sh stop
  4. 重启项目: ./app.sh restart
#!/bin/bash
# 针对普通的可执行的jar文件,提供启动,停止,重启等功能
# 需要根据jar包名称更改APP_JAR
# ms 和 mx 也需要根据情况调整

SHELL_FOLDER=$(cd "$(dirname "$0")";pwd)
APP_JAR="xxl-job-admin-2.2.0.jar"
LOG_PATH=$SHELL_FOLDER/logs
JAVA_OPTS="
-server
-Xms1g 
-Xmx1g 
-XX:+AlwaysPreTouch 
-XX:+PrintGCDetails 
-XX:+PrintGCTimeStamps 
-XX:+PrintGCCause 
-Xloggc:$LOG_PATH/gc.log 
-XX:+HeapDumpOnOutOfMemoryError 
-XX:HeapDumpPath=$LOG_PATH/heapdump
-Dfile.encoding=utf-8"

start(){
  if [ ! -d "$LOG_PATH"  ];then
    mkdir "$LOG_PATH"
  fi
  nohup java $JAVA_OPTS -jar $SHELL_FOLDER/$APP_JAR >$LOG_PATH/console.log 2>&1 & 
  echo "app $SHELL_FOLDER/$APP_JAR started."
  echo "JAVA_OPTS: $JAVA_OPTS"
}

stop(){
    ps -ef|grep $SHELL_FOLDER/$APP_JAR|grep -v grep|awk '{print $2}'|xargs kill -9
    echo "app $SHELL_FOLDER/$APP_JAR is killed."
}

restart(){
	stop
	start
}

echo "APP_HOME:$SHELL_FOLDER"
case $1 in
	"start")
		start
		;;
	"stop")
		stop
		;;
	"restart")
		restart
		;;
	*)
		echo "Usage: app.sh ( commands ... )"
		echo "commands:"
		echo "  start             Start java app"
		echo "  stop              Stop java app"
		echo "  restart           Restart java app"
		;;
esac

你可能感兴趣的:(码农的日记,java,shell,jvm.gc)