温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ansible 远程服务管理-Java 应用启动脚本;

发布时间:2020-04-06 22:15:35 来源:网络 阅读:1345 作者:breaklinux 栏目:系统运维

**1.背景; **

(1).实际运维过程中难免出现大规模通过运维批量工具类似于ansible 进行远程管理服务,类似启动java 环境应用

出现异常终止运行,无法运行;

 

**2.ansible 相关知识预热;**

  (1).ansible 自动化运维工具属于非交互式登陆方式进行机器管理--(默认不加载bash 和系统env 环境变量)

  (2).ansible 命令执行流程--->ansible master 执行--> 客户端机器临时家目录路径如:/home/ops/.ansible/tmp/ 

  (3).如上目录/home/ops/.ansible/tmp/AnsiballZ_command.py  新增临时py 文件然后执行py文件->清理生成临时脚本;

  (4).ansible -vvv 查看执行过程;

   

**3.应用场景;**


(1).第三方外包项目提供shell 脚本通过我们发布系统进行发布-发布部署阶段使用了ansible-playbook 进行,第三方提供脚本如下;

#!/bin/bash
APP_NAME="apie-0.0.1-SNAPSHOT.jar"
case $1 in 
    start)
        nohup java -Dfile.encoding=UTF-8 -jar ${APP_NAME} --spring.config.location=application-apicenter.yml --spring.profiles.active=none  &
        echo ${APP_NAME} start!
        ;;
    stop)    
        ps -ef| grep ${APP_NAME} |grep -v grep |awk '{print $2}'  | sed -e "s/^/kill -9 /g" | sh - 
        echo ${APP_NAME} stop!
        ;;
    restart)
        "$0" stop
        sleep 3
        "$0" start
        ;;
    status)  ps -aux | grep ${APP_NAME} | grep -v 'grep'
        ;;
    log)
    case $2 in
debug)
tail -f -n ${3-400} logs/debug.log
;;
error)
tail -f -n ${3-400} logs/error.log
;;
*)
echo "Example: services.sh log {debug|error}" ;;
esac
        ;;
    *)       
        echo "Example: services.sh [start|stop|restart|status]" ;;
esac


(2).远程执行脚本出现;

[ops@op ~]$ ansible -i 192.168.1.53, all -m shell -a "su - work -c '/chj/app/web_app/apiCenter/services-api.sh restart'" -b  

172.21.204.53 | CHANGED | rc=0 >>

apiCenter-0.0.1-SNAPSHOT.jar stop!

apiCenter-0.0.1-SNAPSHOT.jar start!Error: Unable to access jarfile api-0.0.1-SNAPSHOT.jar

原因分析:

   1.ansible 执行脚本流程会在 /home/ops/.ansible/tmp/ 目录下找  apiCenter-0.0.1-SNAPSHOT.jar 发现没有此文件 故报错; Unable to access jarfile api-0.0.1-SNAPSHOT.jar


(3).进行脚本改造;

#!/bin/bash
CURDIR=$(cd $(dirname ${BASH_SOURCE[0]}); pwd )  #如果第一条语句顺利执行,就执行pwd显示当前目录,并将结果赋值给变量“DIR” 
cd $CURDIR 
APP_NAME="apiCenter-0.0.1-SNAPSHOT.jar"
case $1 in 
    start)
        nohup /usr/local/jdk/bin/java -Dfile.encoding=UTF-8 -jar ${APP_NAME} --spring.config.location=application-apicenter.yml --spring.profiles.active=none >> console.`date "+%FT%TZ"`.log 2>&1 &
        echo ${APP_NAME} start!
        ;;
    stop)    
        ps -ef| grep ${APP_NAME} |grep -v grep |awk '{print $2}'  | sed -e "s/^/kill -9 /g" | sh - 
        echo ${APP_NAME} stop!
        ;;
    restart)
        "$0" stop
        sleep 3
        "$0" start
        ;;
    status)  ps -aux | grep ${APP_NAME} | grep -v 'grep'
        ;;
    log)
    	case $2 in
	debug)
		tail -f -n ${3-400} logs/debug.log
		;;
	error)
		tail -f -n ${3-400} logs/error.log
		;;
	*)
		echo "Example: services.sh log {debug|error}" ;;
	esac
        ;;
    *)       
        echo "Example: services.sh [start|stop|restart|status]" ;;
esac

(4).改造后测试;


[ops@op-opsbmc-2-prod ~]$ ansible -i   192.168.1.53, all -m shell -a "su - work -c '/chj/app/web_app/apiCenter/services-apicenter.sh restart'" -b  


 192.168.1.53,| CHANGED | rc=0 >>

apiCenter-0.0.1-SNAPSHOT.jar stop!

apiCenter-0.0.1-SNAPSHOT.jar start!


(5).登陆机器查看进程;

ansible 远程服务管理-Java 应用启动脚本;


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI