当工具用习惯了。也就懒的去关心底层的东西了。项目部署的时候,用习惯了jenkins也就不关心运维写的脚本了。但是当工具出问题,那就要从最基础的脚本来部署项目。脚本我也不太懂,在这里只是与大家一起分享启动和关闭的两个脚本,大神请绕路。
注:springboot项目,jar包部署,main类启动项目。脚本有很多,只要懂的原理,都能写出来,仅供参考,没有注释,相关标签知识自己补吧。
1.启动脚本
#!/bin/shJAVA_OPTS=""CLASSPATH=""CATALINA_OPTS=""PRG="$0"RUN_USER="root"PID_FILE="/data/sbc/项目名/tomcat.pid"current_user=`whoami`if [ "$current_user" != "$RUN_USER" ];thenecho "please run $0 as user $RUN_USER" 1>&2exit 1fi# resolve links - $0 may be a softlinkwhile [ -h "$PRG" ]; dols=`ls -ld "$PRG"`link=`expr "$ls" : '.*-> \(.*\)$'`if expr "$link" : '/.*' > /dev/null; thenPRG="$link"elsePRG=`dirname "$PRG"`/"$link"fidone# Get standard environment variablesPRGDIR=`dirname "$PRG"`APP_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`SPRINGBOOT_OUT="$APP_HOME"/log/springboot.out# set JAVA_OPTSENV_CONF="$APP_HOME"/conf/env.confif [ -f $ENV_CONF ];thenJAVA_OPTS=`sed 's/"//g' $ENV_CONF | awk '/^[^#]/' | tr "\n" ' '`fi# set CLASSPATHif [ -d "$APP_HOME"/lib ];thenfor lib_jar in `ls "$APP_HOME/lib"`;doCLASSPATH="$APP_HOME"/lib/"$lib_jar":"$CLASSPATH"donefi# User can set environment in setenv.shif [ -r "$APP_HOME/bin/setenv.sh" ]; then. "$APP_HOME/bin/setenv.sh"fiecho "Using JAVA_HOME: $JAVA_HOME"echo "Using APP_HOME: $APP_HOME"if [ ! -z "$CLASSPATH" ];thenecho "Using CLASSPATH: $CLASSPATH"fiif [ ! -z "$PID_FILE" ]; thenecho "Using PID_FILE: $PID_FILE"fiif [ ! -z "$PID_FILE" ]; thenif [ -f "$PID_FILE" ]; thenif [ -s "$PID_FILE" ]; thenecho "Existing PID file found during start."if [ -r "$PID_FILE" ]; thenPID=`cat "$PID_FILE"`ps -p $PID >/dev/null 2>&1if [ $? -eq 0 ] ; thenecho "Server appears to still be running with PID $PID. Start aborted."exit 0elseecho "Removing/clearing stale PID file."rm -rf "$PID_FILE" >/dev/null 2>&1if [ $? != 0 ]; thenif [ -w "$PID_FILE" ]; thencat /dev/null > "$PID_FILE"elseecho "Unable to remove or clear stale PID file. Start aborted."exit 1fififielseecho "Unable to read PID file. Start aborted."exit 1fielserm -rf "$PID_FILE" >/dev/null 2>&1if [ $? != 0 ]; thenif [ ! -w "$PID_FILE" ]; thenecho "Unable to remove or write to empty PID file. Start aborted."exit 1fififififi# Add server.jar to classpath, if CLASSPATH is not blank, it must be endswith ":"CLASSPATH="$CLASSPATH""$APP_HOME"/bin/server.jarcat /dev/null > ${SPRINGBOOT_OUT}${JAVA_HOME}/bin/java $JAVA_OPTS -classpath $CLASSPATH org.springframework.boot.loader.JarLauncher >> $SPRINGBOOT_OUT 2>&1 &if [ ! -z "$PID_FILE" ]; thenecho $! > "$PID_FILE"fiecho "springboot server started."
2.关闭脚本
#/bin/shRUN_USER="root"PID_FILE="/data/sbc/项目名/tomcat.pid"SHUTDOWN_PORT="8411"current_user=`whoami`if [ "$current_user" != "$RUN_USER" ];thenecho "please run $0 as user $RUN_USER"exit 1fiif [ ! -f "$PID_FILE" ];thenecho "pid file $PID_FILE not exist, please check if server running, stop aborted."exit 1finetstat -an | grep LISTEN | grep -w $SHUTDOWN_PORTif [[ $? -eq 0 ]]; thenshut_info=`curl --connect-timeout 10 -m 30 -XPOST http://127.0.0.1:"$SHUTDOWN_PORT"/shutdown.json`echo "shutdown info: $shut_info ,result code: $?"fipid=`cat $PID_FILE`for num in 1 2 3 4 5 6 7 8 9 10doecho "stop times: $num"still_alive=`ps -ef | awk '$2 ~ /^'$pid'$/{print $2}'`if [ ! -z "$still_alive" ];thenkill $pidsleep 1elsebreakfidonestill_alive=`ps -ef | awk '$2 ~ /^'$pid'$/{print $2}'`if [ ! -z "$still_alive" ];thenkill -9 $pidfirm -rf $PID_FILEexit 0
再补充一种nohup java 方式的简单启动脚本
#!/bin/bash#这里可替换为你自己的执行程序,其他代码无需更改APP_NAME=myApplication.jar#使用说明,用来提示输入参数usage() {echo "Usage: sh start.sh [start|stop|restart|status]"exit 1}#检查程序是否在运行is_exist() { pid=`ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}' `#如果不存在返回1,存在返回0if [ -z "${pid}" ]; thenreturn 1elsereturn 0fi}#启动方法start() {is_existif [ $? -eq "0" ]; thenecho "${APP_NAME} is already running. pid=${pid} ."elsenohup java -jar $APP_NAME --server.port=83> /dev/null 2>&1 &fi}#停止方法stop() {is_existif [ $? -eq "0" ]; thenkill -9 $pidelseecho "${APP_NAME} is not running"fi}#输出运行状态status() {is_existif [ $? -eq "0" ]; thenecho "${APP_NAME} is running. Pid is ${pid}"elseecho "${APP_NAME} is not running."fi}#重启restart() {stopstart}#根据输入参数,选择执行对应方法,不输入则执行使用说明case "$1" in"start")start;;"stop")stop;;"status")status;;"restart")restart;;*)usage;;esac
如果觉得《springboot项目jar包启动脚本》对你有帮助,请点赞、收藏,并留下你的观点哦!