一個Spring-Boot的通用啟動指令碼,支援啟動/停止/重啟/檢視狀態/Dump程序相關資料(JVM/OS)
使用方式:
基本使用
$ 指令碼名 [start|stop|restart|status|dump]
DEBUG模式啟動
$ 指令碼名 [start|restart] debug#!/bin/bash# Spring-Boot 常規啟動指令碼,基於HotSpot Java8# 使用方式:xx.sh [start|stop|restart|status|dump]# 將Spring-Boot Jar包和此指令碼放在同一目錄下,之後配置APP_NAME/PROFILE即可cd `dirname $0`# 應用名(boot jar包名)APP_NAME=scheduler# Spring-Boot環境名(profiles)PROFILE=testJAR_NAME=$APP_NAME\\.jarPID=$APP_NAME\\.pidAPP_HOME=`pwd`LOG_PATH=$APP_HOME/logsGC_LOG_PATH=$LOG_PATH/gcDEBUG_FLAG=$2if [ ! -d $LOG_PATH ]; then mkdir $LOG_PATHfiif [ ! -d $GC_LOG_PATH ]; then mkdir $GC_LOG_PATHfi# DUMP父目錄DUMP_DIR=$LOG_PATH/dumpif [ ! -d $DUMP_DIR ]; then mkdir $DUMP_DIRfi# DUMP目錄字首DUMP_DATE=`date +%Y%m%d%H%M%S`# DUMP目錄DATE_DIR=$DUMP_DIR/$DUMP_DATEif [ ! -d $DATE_DIR ]; then mkdir $DATE_DIRfi# GC日誌引數GC_LOG_OPTS="-XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:$GC_LOG_PATH/gc-%t.log"# OOM Dump記憶體引數DUMP_OPTS="-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$LOG_PATH"# JVM DEBUG引數,用於除錯,預設不開啟# ClassLoader和Method Compile日誌,用於除錯COMPILE_LOADER_OPTS="-XX:+TraceClassLoading -XX:+TraceClassUnloading -XX:-PrintCompilation"# 遠端除錯引數REMOTE_DEBUG_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"# DEBUG引數DEBUG_OPTS="$COMPILE_LOADER_OPTS $REMOTE_DEBUG_OPTS"# 至於Garbage Collector,雖然Java8已經支援G1了,但是不一定必須用,CMS在預設場景下也是一個優秀的回收器GC_OPTS="-XX:+UseConcMarkSweepGC"OTHER_OPTS="-Djava.security.egd=file:/dev/./urandom"# JVM 啟動引數,如無特殊需求,推薦只配置堆+元空間JVM_OPTIONS="-server -Xms2g -Xmx2g -XX:MetaspaceSize=256m $GC_OPTS $GC_LOG_OPTS $DUMP_OPTS $OTHER_OPTS"#使用說明,用來提示輸入引數usage() { echo "Usage: sh [run_script].sh [start|stop|restart|status|dump]" exit 1}#檢查程式是否在執行is_exist(){ pid=`ps -ef|grep $APP_HOME/$JAR_NAME|grep -v grep|awk '{print $2}' ` #如果不存在返回1,存在返回0 if [ -z "${pid}" ]; then return 1 else return 0 fi}#啟動方法start(){ is_exist if [ $? -eq "0" ]; then echo "--- ${JAR_NAME} is already running PID=${pid} ---" else if [ "$DEBUG_FLAG" = "debug" ]; then JVM_OPTIONS="$JVM_OPTIONS $DEBUG_OPTS" echo -e "\\033[33m Warning: currently running in debug mode! This mode enables remote debugging, printing, compiling, and other information \\033[0m" fi echo "JVM_OPTIONS : " echo "$JVM_OPTIONS" nohup java -jar $JVM_OPTIONS -Dspring.profiles.active=$PROFILE $APP_HOME/$JAR_NAME >/dev/null 2>&1 & echo $! > $PID echo "--- start $JAR_NAME successed PID=$! ---" fi }#停止方法stop(){ #is_exist pidf=$(cat $PID) #echo "$pidf" echo "--- app PID = $pidf begin kill $pidf ---" kill $pidf rm -rf $PID sleep 2 is_exist if [ $? -eq "0" ]; then echo "--- app 2 PID = $pid begin kill -9 $pid ---" kill -9 $pid sleep 2 echo "--- $JAR_NAME process stopped ---" else echo "--- ${JAR_NAME} is not running ---" fi }#輸出執行狀態status(){ is_exist if [ $? -eq "0" ]; then echo "--- ${JAR_NAME} is running PID is ${pid} ---" else echo "--- ${JAR_NAME} is not running ---" fi}dump(){ is_exist if [ $? -eq "0" ]; then echo -e "Dumping the $JAR_NAME ...\\c" do_dump else echo "--- ${JAR_NAME} is not running ---" fi }#重啟restart(){ stop start}do_dump(){ jstack $pid > $DATE_DIR/jstack-$pid.dump 2>&1 echo -e ".\\c" jinfo $pid > $DATE_DIR/jinfo-$pid.dump 2>&1 echo -e ".\\c" jstat -gcutil $pid > $DATE_DIR/jstat-gcutil-$pid.dump 2>&1 echo -e ".\\c" jstat -gccapacity $pid > $DATE_DIR/jstat-gccapacity-$pid.dump 2>&1 echo -e ".\\c" jmap $pid > $DATE_DIR/jmap-$pid.dump 2>&1 echo -e ".\\c" jmap -heap $pid > $DATE_DIR/jmap-heap-$pid.dump 2>&1 echo -e ".\\c" jmap -histo $pid > $DATE_DIR/jmap-histo-$pid.dump 2>&1 echo -e ".\\c" if [ -r /usr/sbin/lsof ]; then /usr/sbin/lsof -p $pid > $DATE_DIR/lsof-$pid.dump echo -e ".\\c" fi if [ -r /bin/netstat ]; then /bin/netstat -an > $DATE_DIR/netstat.dump 2>&1 echo -e ".\\c" fi if [ -r /usr/bin/iostat ]; then /usr/bin/iostat > $DATE_DIR/iostat.dump 2>&1 echo -e ".\\c" fi if [ -r /usr/bin/mpstat ]; then /usr/bin/mpstat > $DATE_DIR/mpstat.dump 2>&1 echo -e ".\\c" fi if [ -r /usr/bin/vmstat ]; then /usr/bin/vmstat > $DATE_DIR/vmstat.dump 2>&1 echo -e ".\\c" fi if [ -r /usr/bin/free ]; then /usr/bin/free -t > $DATE_DIR/free.dump 2>&1 echo -e ".\\c" fi if [ -r /usr/bin/sar ]; then /usr/bin/sar > $DATE_DIR/sar.dump 2>&1 echo -e ".\\c" fi if [ -r /usr/bin/uptime ]; then /usr/bin/uptime > $DATE_DIR/uptime.dump 2>&1 echo -e ".\\c" fi echo "OK!" echo "DUMP: $DATE_DIR"}#根據輸入引數,選擇執行對應方法,不輸入則執行使用說明case "$1" in "start") start ;; "stop") stop ;; "status") status ;; "restart") restart ;; "dump") dump ;; *) usage ;;esacexit 0
關注我,私信回覆“資料”獲取面試寶典《Java核心知識點整理.pdf》“,覆蓋了JVM、鎖、高併發、反射、Spring原理等多種面試資料!!