10分钟学会编写 springboot jar包 启动脚本


10分钟系列之—— 10分钟学会编写 高颜值 Springboot jar包 start | stop | restart | show status 脚本


1.第一个Shell脚本
在Linux目录下,如 /home/app/sample
输入命令

vi test.sh

输入文件内容:

#!/bin/bash
echo "Hello world!"

然后输入:wq 并 回车,即可保存文件内容。
如此则创建了一个名为 text.sh 的文件。
接下来,赋予脚本文件执行权限,然后执行:

chmod +x ./test.sh  #使脚本具有执行权限
./test.sh  #执行脚本

就会看到 “Hello world!” 输出。

2.定义一个变量,使用条件判断语句,循环语句以及分支判断语句:

#!/bin/bash
echo "Hello world!"
java_option="-server -Xms4G -Xmx4G -Xmn1800m"
echo ${java_option}
if [ $"$java_option" ]
then
 echo "has java options config"
else
 echo "no config"
fi

3.结合Linux命令的逻辑:

PID=$(ps aux | grep ${_JAR_KEYWORDS} | grep -v grep | awk '{print $2}' )

上述脚本的意思是:

○ 按照进程关键字查找指定进程,显示所有的进程显示全部的列
○ 去除包含进程关键字的进程行(grep -v 是反向查找的意思,比如 grep -v grep 就是查找不含有 grep 字段的行)
○ 每行按空格分割,输出文本中的第二项。

如下示例,一目了然:

[root@01001 sample]$ ps aux|grep java |grep -v grep |awk '{print $2}'
34867
35642
[root@01001 sample]$ ps aux|grep java |grep -v grep
root       34867  0.2  5.9 4795292 483136 ?      Sl   09:33   0:28 java -jar dubboDemo-service-1.0.0-SNAPSHOT.jar
root       35642  0.4  7.2 4852108 594724 ?      Sl   09:34   0:46 java -jar dubboConsumer-service-1.0.0-SNAPSHOT.jar
[root@01001 sample]$ ps aux|grep java 
root       34867  0.2  5.9 4795292 483136 ?      Sl   09:33   0:28 java -jar dubboDemo-service-1.0.0-SNAPSHOT.jar
root       35642  0.4  7.2 4852108 594724 ?      Sl   09:34   0:46 java -jar dubboConsumer-service-1.0.0-SNAPSHOT.jar
root      115153  0.0  0.0 112664   972 pts/0    S+   12:31   0:00 grep --color=auto java

Linux命令返回值 $?

  1. 通过 $? 获取上一次命令执行的结果,0表示成功,非0表示失败。
  2. 获取上一个命令的退出状态
  3. 获取函数的返回值

示例1

[root@dev2] # ls
[root@dev2] # echo $?
0

示例2
编写下面的代码,并保存为 test.sh:

#!/bin/bash
if["$1" == 100]
then
exit 0  # 参数正确,退出状态为0
else
exit 1  # 参数错误,退出状态1
fi

exit表示退出当前 Shell 进程,我们必须在新进程中运行 test.sh,
否则当前 Shell 会话(终端窗口)会被关闭,我们就无法取得它的退出状态了。

[root@dev2 ~]# cd demo
[root@dev2 demo]# bash ./test.sh 100  #作为一个新进程运行
[root@dev2 demo]# echo $?
0

再如,运行 test.sh 时传递参数 89:

[root@dev2 demo]# ./test.sh 89  #作为一个新进程运行
[oot@dev2 demo]# echo $?
1

示例3
编写下面的代码,并保存为 test.sh:

#!/bin/bash
function add() {
  return `expr $1 + $2`
}
add 16 50
echo $?

运行结果:66

OK,有了上述基础,我们来看下springboot 的jar包执行脚本
先看效果:
10分钟学会编写 springboot jar包 启动脚本_第1张图片
./run.sh status > 输出当前服务状态
输入:./run.sh start > 启动服务

./run.sh status    #输出当前服务状态
./run.sh start     #启动服务
./run.sh stop      #停止服务
./run.sh restart   #重启服务

下面是完整脚本

 #!/bin/bash
 #ja
JAVA_OPTIONS="-server -Xms4G -Xmx4G -Xmn1800m -Xss256k -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m -XX:+UseG1GC -Dspring.profiles.active=dev"
APP_JAR=dubboDemo-1.0.0-SNAPSHOT.jar
APP_NAME=dubboDemo
STDOUT_FILE=run.log

PID=$(ps aux | grep ${APP_JAR} | grep -v grep | awk '{print $2}' )

function check_if_process_is_running() {
 if [ "$PID" = "" ]; then
   return 1
 fi
 ps -p $PID | grep "java"
 return $?
}

function status_cmd(){
   if check_if_process_is_running
    then
      isrunning
    else
      notrunning
    fi
}

function isrunning(){
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}
  echo -e "[$(date +%F-%T)] \033[32m ${APP_NAME} is running ! (PID=${PID}) \033[0m" 2>&1 | tee -a ${STDOUT_FILE}
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}
}
function notrunning(){
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}
  echo -e "[$(date +%F-%T)] \033[32m ${APP_NAME} not running ! (PID=${PID}) \033[0m" 2>&1 | tee -a ${STDOUT_FILE}
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}
}

function stop_cmd(){
  if ! check_if_process_is_running
    then
      stoped
      exit 0
    fi
    kill -9 $PID
    waitstop
    NOT_KILLED=1
    for i in {1..20}; do
      if check_if_process_is_running
      then
        echo -ne "\033[32m Stopping \033[0m" 
        for i in {1..5}; do
          echo -ne "\033[32m.\033[0m"
          sleep 1
        done
      else
        NOT_KILLED=0
      fi
    done
    echo
    if [ $NOT_KILLED = 1 ]
    then
      cannotkill
      exit 1
    fi
    alreadystop
}

function stoped(){
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}
  echo -e "[$(date +%F-%T)] \033[32m ${APP_NAME} already stopped ! (PID=${PID}) \033[0m" 2>&1 | tee -a ${STDOUT_FILE}
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}
}
function waitstop(){
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}
  echo -e "[$(date +%F-%T)] \033[32m ${APP_NAME} Waiting for process to stop ! (PID=${PID}) \033[0m" 2>&1 | tee -a ${STDOUT_FILE}
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}
}
function cannotkill(){
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}
  echo -e "[$(date +%F-%T)] \033[32m ${APP_NAME} Cannot kill process ! (PID=${PID}) \033[0m" 2>&1 | tee -a ${STDOUT_FILE}
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}  
}
function alreadystop(){
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}
  echo -e "[$(date +%F-%T)] \033[32m ${APP_NAME} already stopped ! (PID=${PID}) \033[0m" 2>&1 | tee -a ${STDOUT_FILE}
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}    
}


function start_cmd(){
  if [ "$PID" != "" ] && check_if_process_is_running
    then
      alreadyrunning
      exit 1
    fi
   nohup java $JAVA_OPTIONS -jar $APP_JAR > /dev/null 2>&1 & 
   echo -ne "\033[32m Starting \033[0m" 
    for i in {1..20}; do
        echo -ne "\033[32m.\033[0m"
        sleep 1
    done
    echo
    if check_if_process_is_running 
     then
       fail
    else
       started
    fi
}

function alreadyrunning(){
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}
  echo -e "[$(date +%F-%T)] \033[32m ${APP_NAME} already running ! (PID=${PID}) \033[0m" 2>&1 | tee -a ${STDOUT_FILE}
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}
}
function fail(){
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}
  echo -e "[$(date +%F-%T)] \033[32m ${APP_NAME} fail ! (PID=${PID}) \033[0m" 2>&1 | tee -a ${STDOUT_FILE}
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}  
}
function started(){  
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}
  echo -e "[$(date +%F-%T)] \033[32m ${APP_NAME} started ! (PID=${PID}) \033[0m" 2>&1 | tee -a ${STDOUT_FILE}
  echo "[$(date +%F-%T)] ================================================================================" 2>&1 | tee -a ${STDOUT_FILE}    
}

case "$1" in
  status)
    status_cmd
    ;;
  stop)
    stop_cmd
    ;;
  start)
    start_cmd
    ;;
  restart)
    $0 stop
    if [ $? = 1 ]
    then
      exit 1
    fi
    $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
esac

exit 0

关于Linux 中 echo 字体颜色的说明:

echo命令打印带有颜色的字符
命令参数格式:
echo -e “\033[字背景颜色;字体颜色m字符串\033[0m”
举例:如下命令含义为:(黑色背景 红色字体)

echo -e "\033[40;31m 开始安装 \033[0m"

echo -e "\033[40;31m 开始安装 \033[0m"
echo -e "\033[40;32m 安装成功 \033[0m"

在这里插入图片描述

常用颜色对应数字如下:
字颜色: 30-----------37
30:黑 31:红 32:绿 33:黄 34:蓝色 35:紫色 36:深绿 37:白色
背景颜色范围: 40----47
40:黑 41:深红 42:绿 43:黄色 44:蓝色 45:紫色 46:深绿 47:白色

你可能感兴趣的:(10分钟学会系列,linux,spring,boot,bash)