linux服务创建及jq配置服务列表查看

1.应用背景

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

2.创建服务方式

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

 #!/bin/sh
#chkconfig:2345 80 05
#description:ihr-resumemessdeal.jar
description_txt="*******服务"
path_txt="/service_resumemessdeal_jar/ihr-resumemessdeal.jar"
app_command="nohup java -jar /usr/local/ihr-services/service_resumemessdeal_jar/ihr-resumemessdeal.jar > /dev/null 2>&1 &"
app_pidname="ihr-resumemessdeal.jar"
pidlist=""
checkpid(){
pidlist=`ps -ef|grep $app_pidname|grep -v "grep"|awk '{print $2}'`
}
start(){
echo "$app_pidname 服务准备启动"
checkpid
if [ "$pidlist" = "" ]
then
su - root -c "$app_command"
checkpid
if [ "$pidlist" = "" ]
then
echo "$app_pidname 服务启动失败"
else
echo "$app_pidname 服务启动成功"
fi
else
echo "$app_pidname 已存在并运行中"
fi
}
stop(){
checkpid
if [ "$pidlist" = "" ]
then
echo "$app_pidname 服务不存在,或已停止运行"
else
kill -9 $pidlist
checkpid
if [ "$pidlist" = "" ]
then
echo "$app_pidname 服务停止成功"
else
echo "$app_pidname 服务停止失败,请重新操作"
fi
fi
}
restart(){
stop
start
}
status(){
checkpid
if [ "$pidlist" = "" ]
then
echo "已停止"
else
echo "运行中"
fi
}
description(){
echo "$description_txt"
}
path(){
echo "$path_txt"
}
pidnum(){
checkpid
if [ "$pidlist" = "" ]
then
echo "无"
else
echo "$pidlist"
fi
} case $1 in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
status;;
description)
description;;
path)
path;;
pidnum)
pidnum;;
*);;
esac

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

测试命令:

linux服务创建及jq配置服务列表查看

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 "\033[33m%-26s %-9s %-10s %-30s %-20s\033[0m\n" 服务名 状态 进程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 "\033[44m%-23s %-10s %-10s %-30s %-20s\033[0m\n" $sl $sta $pidn $des $pat
elif [ "$pidn" = "无" ]
then
printf "\033[45m%-23s %-10s %-10s %-30s %-20s\033[0m\n" $sl $sta $pidn $des $pat
else
printf "\033[32m%-23s %-10s %-10s %-30s %-20s\033[0m\n" $sl ${sta} $pidn $des $pat
fi
done

运行如下:

linux服务创建及jq配置服务列表查看

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

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

3.2.1 创建服务配置test.json

 [
{
"index": 0,
"name": "ihr-rtfeedbackdeal.jar",
"desc": "简历转发",
"dir": "/service_rtfeedbackdeal_jar/ihr-rtfeedbackdeal.jar"
},
{
"index": 1,
"name": "ihr-resu3333333333.jar",
"desc": "简历填写",
"dir": "service_resum3333_jar/ihr-resumem33333.jar"
},
{
"index": 2,
"name": "ihr-resumemessdeal.jar",
"desc": "简历反馈",
"dir": "service_resumemessdeal_jar/ihr-resumemessdeal.jar"
},
{
"index": 3,
"name": "ihr-aaaaaaaaaa.jar",
"desc": "简历修改",
"dir": "service_resumeme11111_jar/ihr-resu111111111.jar"
},
{
"index": 4,
"name": "ihr-aaaarrrr.jar",
"desc": "简历回收",
"dir": "service_resumeme11111_jar/ihr-resu13333.jar"
}
]

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

cd /home/gzf/tools/jq
jqpath="/jq"
testpath="/home/gzf/tools/jq/test.json"
servicenames=`cat $testpath | .$jqpath .[] | .$jqpath .index`
printf "\033[33m%-26s %-9s %-10s %-30s %-20s\033[0m\n" 服务名 状态 进程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 "\033[44m%-23s %-10s %-10s %-30s %-20s\033[0m\n" $sl $sta $pidn $des $pat
elif [ "$pidn" = "" ]
then
sta="停止"
pidn="无"
printf "\033[45m%-23s %-10s %-10s %-30s %-20s\033[0m\n" $sl $sta $pidn $des $pat
else
printf "\033[32m%-23s %-10s %-10s %-30s %-20s\033[0m\n" $sl ${sta} $pidn $des $pat
fi
done

运行如下:

linux服务创建及jq配置服务列表查看

3.备注:

知识点:

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

上一篇:WCF服务配置编辑器使用


下一篇:vue--父组件主动获取子组件的方法