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 "\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
运行如下:
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 "\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
运行如下:
3.备注:
知识点:
3.1 nohup命令的用法
3.2 linux服务的创建
3.3 jq(linux读取json配置)的用法
3.4 printf命令的用法