【jar包服务操作】【配置 web 界面监控服务运行】【shell脚本】

42 篇文章 11 订阅

1、先看下最终实现效果

在这里插入图片描述

2、 安装 supervisor (web 界面监控)

  • 安装
yum install -y supervisor
systemctl start supervisord
systemctl enable supervisord
  • 修改配置 /etc/supervisord.conf
cat <<EOF >> /etc/supervisord.conf

[inet_http_server]        
port=*:9001 
username=admin             
password=cloudansys[supervisor]           
EOF
  • 添加配置文件 default.base 和 java.ini(空文件)
touch /etc/supervisord.d/java.ini
cat <<EOF >> /etc/supervisord.d/default.base
[program:lzgl-server]                           ; web 界面显示的服务名称
directory=/root/projects/lzgl-server            ; 执行命令的目录
command=java -jar lzgl-server-1.0-SNAPSHOT.jar  ; 执行的命令
priority=1                                      ; 数字越高,优先级越高
numprocs=1                                      ; 启动几个进程
autostart=true                                  ; 随着supervisord的启动而启动
autorestart=true                                ; 自动重启
startretries=10                                 ; 启动失败时的最多重试次数
exitcodes=0                                     ; 正常退出代码
stopsignal=KILL                                 ; 用来杀死进程的信号
stopwaitsecs=10                                 ; 发送SIGKILL前的等待时间
redirect_stderr=true                            ; 重定向stderr到stdout
;
;
;         
EOF
  • 重启 supervisor
systemctl restart supervisord

3、脚本 jar-operator.sh,这个脚本需要依赖 supervisor

#!/bin/bash

:<<!
【脚本说明】
1、此脚本适用操作jar包服务;
2、建议把脚本放在 /usr/wlf 目录下;
3、哪个项目使用只需要在jar所在目录执行【ln -s /usr/wlf/jar-operator.sh operate.sh】;
4、在启动或重启时会判断是否配置了 web 监控,如果没配置则自动配置。
!

# 服务操作参数
operate=$1
# 指定运行内存大小
memsize=$2

# 服务基本信息
homedir=$(cd $(dirname $0);pwd)
appfile=$(ls $homedir/*.jar)
app=${appfile##*/}

# 默认2G内存
if [[ -z $memsize ]]; then
    memsize=2048
fi

# jvm 调优参数
jarp="java -Xms${memsize}m -Xmx${memsize}m -jar "

# 获取项目名【去掉后缀-1.0-SNAPSHOT.jar或-1.0.0.jar的部分】
function get_project(){
    # 先获取横杠-的索引
    declare -a seplist
    j=0
    for i in `seq ${#app}`
    do
        char=${app:$i-1:1}
        if [[ $char = '-' ]]; then
            seplist[$j]=$i
            j=$j+1
        fi
    done
    # 判断是否包含字符串SNAPSHOT
    if [[ $app =~ "SNAPSHOT" || $app =~ "RELEASE" ]]; then
        # 后缀为-1.0-SNAPSHOT.jar,取出倒数第二个"-"的索引
        size=${#seplist[*]}
        index=$(expr $size - 1)
        idx=$(expr ${seplist[$index]} - 1)
        # 获取项目名
        project=${app:0:${idx}}
        echo $project
    else
        # 后缀为-1.0.0.jar,取出倒数第一个"-"的索引
        size=${#seplist[*]}
        index=$(expr $size - 1)
        idx=$(expr ${seplist[$index]} - 1)
        # 获取项目名
        project=${app:0:${idx}}
        echo $project
    fi
}

# 项目名称
project=$(get_project)

# 进程
pid=`ps -ef | egrep "$app" | egrep -v grep | awk '{print $2}'`

# 定制化shell输出
function custom_print(){
    echo -e "\033[5;34m ***** \033[0m"
    echo -e "\033[32m $@ ! \033[0m"
    echo -e "\033[5;34m ***** \033[0m"
}

# 判断该项目是否已配置 web
function file_has_str(){
    # 首先判断该项目是否为空
    if [[ ! $project ]]; then
        echo "true"
    else
        # 再判断 web 配置中是否已配置该项目,精确匹配
        grep -lrq $project]$ /etc/supervisord.d/java.ini && echo "true" || echo "false"
    fi
}

# 提示信息
msg='Please input the param 【<run [memsize]|kil|res [memsize]|sta|log>】'

# web 界面配置 【该功能需要安装 supervisor】
function config_web(){
    # 判断是否安装了 supervisor
    if [[ -d /etc/supervisord.d/ ]]; then
        # 再判断是否已配置了该项目
        if [[ $(file_has_str) = "false" ]]; then
            conf_def=/etc/supervisord.d/default.base
            conf_use=/etc/supervisord.d/java.ini
            title="[program:$project]"
            dir="directory=$homedir"
            cmd="command=$jarp $app"
            sed -i "1c $title" $conf_def
            sed -i "2c $dir" $conf_def
            sed -i "3c $cmd" $conf_def
            cat $conf_def >> $conf_use
            /usr/bin/supervisorctl update
        fi
    fi
}

# 判断输入参数
if [[ -z $operate || $operate = "help" || $operate = "-h" ]]; then
    custom_print $msg

# 启动服务
elif [[ $operate = "run" || $operate = "start" ]]; then
    config_web
    if [[ $pid ]]; then
        # 服务正在运行中
        msg='The service is already running'
        custom_print $msg
    else
        if [[ -d /etc/supervisord.d/ ]]; then
            /usr/bin/supervisorctl start $project > /dev/null
        else
            nohup $jarp $app > /dev/null 2>&1 &
        fi
        msg='Start success'
        custom_print $msg
    fi

# 重启服务
elif [[ $operate = "restart" || $operate = "res" ]]; then
    config_web
    if [[ -d /etc/supervisord.d/ ]]; then
        /usr/bin/supervisorctl restart $project > /dev/null
    else
        if [[ $pid ]]; then
            kill -9 $pid
        fi
        nohup $jarp $app > /dev/null 2>&1 &
    fi
    msg='Restart success'
    custom_print $msg

# 停止服务
elif [[ $operate = "stop" || $operate = "kil" ]]; then
    if [[ $pid ]]; then
        if [[ -d /etc/supervisord.d/ ]]; then
            /usr/bin/supervisorctl stop $project > /dev/null
        else
            kill -9 $pid
        fi
        msg='Stopped success'
        custom_print $msg
    else
        # 服务早已停止或未启动
        msg='The service is already down'
        custom_print $msg
    fi

# 查看服务运行状态
elif [[ $operate = "status" || $operate = "sta" ]]; then
    if [[ $pid ]]; then
        # 黄底蓝字
        msg='RUNNING'
        custom_print $msg
    else
        # 蓝底黑字
        echo -e "\033[5;34m ***** \033[0m"
        echo -e "\033[31m STOPPED ! \033[0m"
        echo -e "\033[5;34m ***** \033[0m"
    fi

# 查看服务运行日志
elif [[ $operate = "log" ]]; then
    # 日志
    logfile=$(ls $homedir/logs/*.log)
    if [[ -e $logfile ]]; then
        tail -f $logfile
    else
        # 尚未生成日志
        msg='No log has been generated yet'
        custom_print $msg
    fi

# 参数输入有误
else
    msg='Please input the correct param 【<run [memsize]|kil|res [memsize]|sta|log>】'
    custom_print $msg
fi

4、脚本使用方法

在这里插入图片描述

在这里插入图片描述

5、脚本jar-operator-without-supervisor.sh,这个脚本无依赖

#!/bin/bash

:<<!
【脚本说明】
1、此脚本适用操作jar包服务;
2、建议把脚本放在 /usr/wlf 目录下;
3、哪个项目使用只需要在jar所在目录执行【ln -s /usr/wlf/jar-operator-without-supervisor.sh operate.sh】;
4、该脚本未配置web监控服务。
!

# 服务操作参数
operate=$1
# 指定运行内存大小
memsize=$2

# 服务基本信息
homedir=$(cd $(dirname $0);pwd)
appfile=$(ls $homedir/*.jar)
app=${appfile##*/}

# 默认2G内存
if [[ -z $memsize ]]; then
    memsize=2048
fi

# jvm 调优参数
jarp="java -Xms${memsize}m -Xmx${memsize}m -jar "

# 获取项目名【去掉后缀-1.0-SNAPSHOT.jar或-1.0.0.jar的部分】
function get_project(){
    # 先获取横杠-的索引
    declare -a seplist
    j=0
    for i in `seq ${#app}`
    do
        char=${app:$i-1:1}
        if [[ $char = '-' ]]; then
            seplist[$j]=$i
            j=$j+1
        fi
    done
    # 判断是否包含字符串SNAPSHOT
    if [[ $app =~ "SNAPSHOT" || $app =~ "RELEASE" ]]; then
        # 后缀为-1.0-SNAPSHOT.jar,取出倒数第二个"-"的索引
        size=${#seplist[*]}
        index=$(expr $size - 1)
        idx=$(expr ${seplist[$index]} - 1)
        # 获取项目名
        project=${app:0:${idx}}
        echo $project
    else
        # 后缀为-1.0.0.jar,取出倒数第一个"-"的索引
        size=${#seplist[*]}
        index=$(expr $size - 1)
        idx=$(expr ${seplist[$index]} - 1)
        # 获取项目名
        project=${app:0:${idx}}
        echo $project
    fi
}

# 项目名称
project=$(get_project)

# 进程
pid=`ps -ef | egrep "$app" | egrep -v grep | awk '{print $2}'`

# 定制化shell输出
function custom_print(){
    echo -e "\033[5;34m ***** \033[0m"
    echo -e "\033[32m $@ ! \033[0m"
    echo -e "\033[5;34m ***** \033[0m"
}

# 提示信息
msg='Please input the param 【<run [memsize]|kil|res [memsize]|sta|log>】'

# 判断输入参数
if [[ -z $operate || $operate = "help" || $operate = "-h" ]]; then
    custom_print $msg

# 启动服务
elif [[ $operate = "run" || $operate = "start" ]]; then
    if [[ $pid ]]; then
        # 服务正在运行中
        msg='The service is already running'
        custom_print $msg
    else
        nohup $jarp $app > /dev/null 2>&1 &
        msg='Start success'
        custom_print $msg
    fi

# 重启服务
elif [[ $operate = "restart" || $operate = "res" ]]; then
    if [[ $pid ]]; then
        kill -9 $pid
    fi
    nohup $jarp $app > /dev/null 2>&1 &
    msg='Restart success'
    custom_print $msg

# 停止服务
elif [[ $operate = "stop" || $operate = "kil" ]]; then
    if [[ $pid ]]; then
        kill -9 $pid
        msg='Stopped success'
        custom_print $msg
    else
        # 服务早已停止或未启动
        msg='The service is already down'
        custom_print $msg
    fi

# 查看服务运行状态
elif [[ $operate = "status" || $operate = "sta" ]]; then
    if [[ $pid ]]; then
        # 黄底蓝字
        msg='RUNNING'
        custom_print $msg
    else
        # 蓝底黑字
        echo -e "\033[5;34m ***** \033[0m"
        echo -e "\033[31m STOPPED ! \033[0m"
        echo -e "\033[5;34m ***** \033[0m"
    fi

# 查看服务运行日志
elif [[ $operate = "log" ]]; then
    # 日志
    logfile=$(ls $homedir/logs/*.log)
    if [[ -e $logfile ]]; then
        tail -f $logfile
    else
        # 尚未生成日志
        msg='No log has been generated yet'
        custom_print $msg
    fi

# 参数输入有误
else
    msg='Please input the correct param 【<run [memsize]|kil|res [memsize]|sta|log>】'
    custom_print $msg
fi

参考

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WaiSaa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值