zoukankan      html  css  js  c++  java
  • linux服务创建及jq配置服务列表查看

    1.应用背景

          随着业务需求,后台处理服务不断增多,对于这些服务或后台程序的查看、更新操作越来越凌乱,所以我们首先需要一个服务列表查看工具,方便查看各
    服务的端口、运行状态、jar包路径等等。

    2.创建服务方式

      2.1创建service,通过简单命令start、stop、restart、status管理
          demo:
          创建服务文件:ihr-resumemessdeal 并copy至:/etc/rc.d/init.d

     1 #!/bin/sh
     2 #chkconfig:2345 80 05
     3 #description:ihr-resumemessdeal.jar
     4 description_txt="*******服务"
     5 path_txt="/service_resumemessdeal_jar/ihr-resumemessdeal.jar"
     6 app_command="nohup java -jar /usr/local/ihr-services/service_resumemessdeal_jar/ihr-resumemessdeal.jar > /dev/null 2>&1 &"
     7 app_pidname="ihr-resumemessdeal.jar"
     8 pidlist=""
     9 checkpid(){
    10    pidlist=`ps -ef|grep $app_pidname|grep -v "grep"|awk '{print $2}'`
    11 }
    12 start(){
    13   echo "$app_pidname 服务准备启动"
    14   checkpid
    15   if [ "$pidlist" = "" ]
    16      then
    17        su - root -c "$app_command"
    18        checkpid
    19        if [ "$pidlist" = "" ]
    20          then
    21            echo "$app_pidname 服务启动失败"
    22          else
    23            echo "$app_pidname 服务启动成功"
    24        fi
    25      else
    26        echo "$app_pidname 已存在并运行中"
    27   fi
    28 }
    29 stop(){
    30    checkpid
    31    if [ "$pidlist" = "" ]
    32       then
    33        echo "$app_pidname 服务不存在,或已停止运行"
    34       else 
    35         kill -9 $pidlist
    36         checkpid
    37         if [ "$pidlist" = "" ]
    38            then 
    39              echo "$app_pidname 服务停止成功"
    40            else
    41              echo "$app_pidname 服务停止失败,请重新操作"
    42         fi
    43    fi
    44 }
    45 restart(){
    46    stop
    47    start
    48 }
    49 status(){
    50    checkpid
    51    if [ "$pidlist" = "" ]
    52       then 
    53          echo "已停止"
    54       else
    55         echo "运行中"
    56    fi
    57 }
    58 description(){
    59    echo "$description_txt"
    60 }
    61 path(){
    62   echo "$path_txt"
    63 }
    64 pidnum(){
    65     checkpid
    66   if [ "$pidlist" = "" ]
    67       then
    68        echo ""
    69       else 
    70        echo "$pidlist"
    71    fi
    72 }
    73 
    74 case $1 in
    75 start)
    76     start;;
    77 stop)
    78     stop;;
    79 restart)
    80     restart;;
    81 status)
    82     status;;
    83 description)
    84     description;;
    85 path)
    86     path;;
    87 pidnum)
    88     pidnum;;
    89 *);;
    90 esac

          运行:
          chmod u+x ihr-resumemessdeal
          chkconfig --add ihr-resumemessdeal
          service ihr-resumemessdeal start

          测试命令:

          

      2.2直接运行为linux后台程序:(大部分运维都这样做)
           demo:
           nohup java -jar /usr/local/ihr-services/service_resumemessdeal_jar/ihr-resumemessdeal.jar > /dev/null 2>&1 &


    3.服务列表管理

       3.1针对2.1创建服务列表脚本

    uthor:zefeng.guo
    servicelist=`chkconfig --list |grep '^ihr-' |awk '{print $1}'`
    printf "33[33m%-26s %-9s %-10s  %-30s %-20s33[0m
    " 服务名  状态  进程ID 描述  服务路径
    for sl in $servicelist
      do
       sta=`service $sl status`
       des=`service $sl description`
       pat=`service $sl path`
       pidn=`service $sl pidnum`
       if [ "$sta" = "空命令" ]
         then
           printf "33[44m%-23s %-10s %-10s %-30s %-20s33[0m
    " $sl $sta $pidn $des $pat
         elif [ "$pidn" = "" ]
         then
          printf "33[45m%-23s %-10s %-10s %-30s %-20s33[0m
    " $sl $sta $pidn $des $pat
         else
           printf "33[32m%-23s %-10s %-10s %-30s %-20s33[0m
    " $sl ${sta} $pidn $des $pat 
       fi 
     done

           运行如下:

           

        3.2针对2.2 创建服务列表脚本

            对于运行于后台得jar包程序查看,我们可以通过jq(linux读取json配置文件)配置获取程序基本信息:

            3.2.1 创建服务配置test.json

     1 [
     2     {
     3         "index": 0,
     4         "name": "ihr-rtfeedbackdeal.jar",
     5         "desc": "简历转发",
     6         "dir": "/service_rtfeedbackdeal_jar/ihr-rtfeedbackdeal.jar"
     7     },
     8     {
     9         "index": 1,
    10         "name": "ihr-resu3333333333.jar",
    11         "desc": "简历填写",
    12         "dir": "service_resum3333_jar/ihr-resumem33333.jar"
    13     },
    14     {
    15         "index": 2,
    16         "name": "ihr-resumemessdeal.jar",
    17         "desc": "简历反馈",
    18         "dir": "service_resumemessdeal_jar/ihr-resumemessdeal.jar"
    19     },
    20     {
    21         "index": 3,
    22         "name": "ihr-aaaaaaaaaa.jar",
    23         "desc": "简历修改",
    24         "dir": "service_resumeme11111_jar/ihr-resu111111111.jar"
    25     },
    26     {
    27         "index": 4,
    28         "name": "ihr-aaaarrrr.jar",
    29         "desc": "简历回收",
    30         "dir": "service_resumeme11111_jar/ihr-resu13333.jar"
    31     }
    32 ]

            3.2.1 读取配置文件并检索程序运行状态:

    cd /home/gzf/tools/jq
    jqpath="/jq"
    testpath="/home/gzf/tools/jq/test.json"
    servicenames=`cat $testpath | .$jqpath .[] | .$jqpath .index`
    printf "33[33m%-26s %-9s %-10s  %-30s %-20s33[0m
    " 服务名  状态  进程ID 描述  服务路径
    sl="ihr-"
    for sc in $servicenames
      do
         sl=`cat $testpath | .$jqpath .[$sc] | .$jqpath .name`
         sl=${sl//"/}
         pidn=`ps -ef | grep $sl | grep -v "grep"| awk '{print $2}'`
         des=`cat $testpath | .$jqpath .[$sc] | .$jqpath .desc`
         pat=`cat $testpath | .$jqpath .[$sc] | .$jqpath .dir`
              sta="运行"     
          if [ "$pidn" = "空命令" ]
         then
           printf "33[44m%-23s %-10s %-10s %-30s %-20s33[0m
    " $sl $sta $pidn $des $pat
         elif [ "$pidn" = "" ]
         then
          sta="停止"
          pidn=""
          printf "33[45m%-23s %-10s %-10s %-30s %-20s33[0m
    " $sl $sta $pidn $des $pat
         else
           printf "33[32m%-23s %-10s %-10s %-30s %-20s33[0m
    " $sl ${sta} $pidn $des $pat 
       fi 
     done

          运行如下:

    3.备注:

       知识点:

        3.1 nohup命令的用法
        3.2 linux服务的创建
        3.3 jq(linux读取json配置)的用法
        3.4 printf命令的用法

  • 相关阅读:
    C# 实现简单打印(二)打印一个文本文档,打印的内容是多行的
    用户管理:登录窗体通过ShowDialog()方法实现切换
    SQL 定义与使用数据库及表 实例_(学生,课程表,选修表)
    temp0305
    计算机硬件通用功能类:硬件信息控制器(主机名,cpu编号,网卡地址,MAC地址,主硬盘编号,ip地址,获取最大线程数,验证服务IP)
    socket编程:简单的TCP服务器
    从输入的邮箱地址中提取用户名
    C#基础:helloWord book 实例小集合
    怎么样datatable表中增加一行合计行?
    C#基础:多态:基类可以定义并实现虚(virtual)方法,派生类可以重写(override)这些方法
  • 原文地址:https://www.cnblogs.com/guozefeng/p/5748926.html
Copyright © 2011-2022 走看看