一個Spring-Boot的通用啓動腳本,支持啓動/中止/重啓/查看狀態/Dump進程相關數據(JVM/OS)java
使用方式:ios
基本使用spring
$ 腳本名 [start|stop|restart|status|dump]
DEBUG模式啓動bash
$ 腳本名 [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=test JAR_NAME=$APP_NAME\.jar PID=$APP_NAME\.pid APP_HOME=`pwd` LOG_PATH=$APP_HOME/logs GC_LOG_PATH=$LOG_PATH/gc DEBUG_FLAG=$2 if [ ! -d $LOG_PATH ]; then mkdir $LOG_PATH fi if [ ! -d $GC_LOG_PATH ]; then mkdir $GC_LOG_PATH fi # DUMP父目錄 DUMP_DIR=$LOG_PATH/dump if [ ! -d $DUMP_DIR ]; then mkdir $DUMP_DIR fi # DUMP目錄前綴 DUMP_DATE=`date +%Y%m%d%H%M%S` # DUMP目錄 DATE_DIR=$DUMP_DIR/$DUMP_DATE if [ ! -d $DATE_DIR ]; then mkdir $DATE_DIR fi # 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" jmap -dump:format=b,file=jmap-dump-$pid.bin $pid 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 ;; esac exit 0