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 "