1 #!/bin/bash 2 3 #项目jar名称 4 JAR_FILE=$2 5 APP_NAME=${JAR_FILE%.*} 6 7 #JDK指定 8 #JAVA_HOME=/app/java/jdk8 9 10 #关闭debug模式则设置为空 11 APP_DEBUGE= 12 #APP_DEBUGE="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=18099" 13 14 #JVM参数 -Xms程序启动时占用内存大小 -Xmx程序运行期间最大可占用的内存大小 -Xss设定每个线程的堆栈大小 -Xmn 年轻代大小 15 #JVM_OPTS= 16 JVM_OPTS='-Xms1024m -Xmx1024m -Xss256k -XX:MaxPermSize=512m -XX:NewRatio=4 -XX:+UseParallelGC -XX:ParallelGCThreads=8 -XX:+UseAdaptiveSizePolicy -verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -XX:-HeapDumpOnOutOfMemoryError -Xloggc:verbose-gc-sp.txt' 17 18 19 # 如果不指定jdk则使用默认 20 if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then 21 javaexe="$JAVA_HOME/bin/java" 22 elif type -p java > /dev/null 2>&1; then 23 javaexe=$(type -p java) 24 elif [[ -x "/usr/bin/java" ]]; then 25 javaexe="/usr/bin/java" 26 else 27 echo "Unable to find Java" 28 exit 1 29 fi 30 31 # 项目配置文件路径 32 #APP_CONF="$APP_HOME/conf/application.properties" 33 34 # 日志文件 35 LOG_FILE= 36 #LOG_FILE="--logging.config=$APP_HOME/conf/logback-spring.xml" 37 38 # 校验进程 39 APP_PID=$APP_NAME.pid 40 41 #APP_CONF="--spring.config.location=file:$APP_CONF " 42 APP_CONF= 43 44 start(){ 45 checkpid 46 if [ $? -eq 0 ]; then 47 echo JDK路径: $javaexe 48 echo 项目名称: $JAR_FILE 49 echo 配置文件: $APP_CONF 50 echo JVM参数: $JVM_OPTS 51 52 if [ ! -n "$APP_DEBUGE" ] ;then 53 echo "关闭debug模式!" 54 else 55 echo "开启debug模式!" 56 fi 57 58 /bin/sh -c "$javaexe -jar $APP_DEBUGE $JVM_OPTS $JAR_FILE $APP_CONF $LOG_FILE > /dev/null 2>&1 & echo \$!" > "$APP_PID" 59 echo "---------------------------------" 60 pid=$(cat "$APP_PID") 61 echo "启动完成,pid:$pid" 62 echo "---------------------------------" 63 sleep 2s 64 else 65 echo "$APP_NAME is runing PID: $pid" 66 fi 67 68 } 69 70 status(){ 71 checkpid 72 if [ $? -eq 0 ]; then 73 echo "$APP_NAME not runing" 74 else 75 echo "$APP_NAME runing PID: $pid" 76 fi 77 } 78 79 checkpid(){ 80 if [[ -f "$APP_PID" ]]; then 81 pid=$(cat "$APP_PID") 82 checkPidNum 83 if [ $? -eq 0 ];then 84 return 0; 85 else 86 return 1; 87 fi 88 else 89 checkPidNum 90 if [ $? -eq 0 ];then 91 echo "${?}" 92 return 0; 93 else 94 return 1; 95 fi 96 fi 97 } 98 99 checkPidNum(){ 100 PIDNUM=`ps -ef|grep $JAR_FILE|grep -v grep|grep -v bootmanager|awk '{print $2}'` 101 if [ -z "${PIDNUM}" ]; then 102 return 0 103 else 104 pid=${PIDNUM} 105 return 1 106 fi 107 } 108 109 stop(){ 110 checkpid 111 if [ $? -eq 0 ]; then 112 echo "$APP_NAME not runing" 113 else 114 echo "$APP_NAME stop..." 115 kill -9 $pid 116 fi 117 } 118 119 restart(){ 120 stop 121 sleep 1s 122 start 123 } 124 125 case $1 in 126 start) start;; 127 stop) stop;; 128 restart) restart;; 129 status) status;; 130 *) echo "require start|stop|restart|status" ;; 131 esac