RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
ansible远程服务管理-Java应用启动脚本;

**1.背景; **

张掖网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。创新互联成立与2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联

(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 应用启动脚本;


分享题目:ansible远程服务管理-Java应用启动脚本;
网站链接:http://scyingshan.cn/article/poseej.html