博文目录
文章目录
使用方式
./run.sh start
./run.sh stop
./run.sh restart
./run.sh status
#!/bin/bashJAVA_CMD="java"
APP_NAME="bid"
JAR_NAME="bid.jar"
PROFILE="develop"BASE_PATH=`cd $(dirname $0); pwd`
# 应用基础目录下的 env 文件中定义的变量
# source ${BASE_PATH}/envOPTIONS="-XX:MetaspaceSize=256M"
OPTIONS="${OPTIONS} -jar ${BASE_PATH}/${JAR_NAME}"
OPTIONS="${OPTIONS} --spring.profiles.active=${PROFILE}"echoRed() { echo $'\e[0;31m'"$1"$'\e[0m'; }
echoCyan() { echo $'\e[0;36m'"$1"$'\e[0m'; }
echoYellow() { echo $'\e[0;33m'"$1"$'\e[0m'; }[[ -z "$stopWaitTime" ]] && stopWaitTime=10getPid() {pid=`ps ax | grep -i ${JAR_NAME} | grep java | grep -v grep | awk '{print $1}'`
}start() {getPidif [ -z ${pid} ] ; thendoStartelseechoCyan "[${APP_NAME}] 正在运行 ($pid)"exit 1fi
}doStart() {echoCyan "${JAVA_CMD} ${OPTIONS}"# nohup ${JAVA_CMD} ${OPTIONS} > ${BASE_PATH}/start.out/dev/null 2>&1 &nohup ${JAVA_CMD} ${OPTIONS} > /dev/null 2>&1 &# 等待启动startCounter=0 # 记录等待次数while [ -z ${pid} ]dosleep 1startCounter=`expr $startCounter + 1`echoCyan "[${APP_NAME}] 启动中, ${startCounter} 秒 ..."getPidif [ "$startCounter" -ge $stopWaitTime ];thenpid=""fidone# 输出启动结果if [ -z ${pid} ] ; thenechoRed "[${APP_NAME}] 启动失败"elseechoCyan "[${APP_NAME}] 已启动 ($pid)"fi
}restart() {stop && start
}stop() {getPid#是否在运行if [ -z ${pid} ] ; thenechoYellow "[${APP_NAME}] 未在运行"exit 0;fi#停止服务echoCyan "[${APP_NAME}] 正在运行 (${pid})"kill ${pid} #&#等待kill没有效果,去掉#kpid=$!#wait $kpid#等待停止stopCounter=0getPidwhile [ ${pid} ] ; dosleep 1stopCounter=`expr $stopCounter + 1`echoCyan "[${APP_NAME}] 停止中, ${stopCounter} 秒 ..."getPidif [ "$stopCounter" -ge $stopWaitTime ];thenechoCyan "[${APP_NAME}] 强制停止 ..."kill -9 ${pid}fidone#输出结果getPidif [ -z ${pid} ] ; thenechoCyan "[${APP_NAME}] 已停止"elseechoRed "[${APP_NAME}] 停止失败"fi
}status() {getPidif [ -z ${pid} ] ; thenechoYellow "[${APP_NAME}] 未在运行"exit 0elseechoCyan "[${APP_NAME}] 正在运行 ($pid)"exit 0fi
}case "$1" in'start')start;;'stop')stop;;'restart')stopstart;;'status')status;;*)echo "Usage: $0 { start | stop | restart | status }"exit 1;;
esacexit 0