首页 > 系统相关 >ansible执行带有参数的shell脚本

ansible执行带有参数的shell脚本

时间:2022-10-27 17:56:03浏览次数:65  
标签:脚本 shell grep app echo XX ansible fi bank

1. 重启应用脚本
#!/bin/bash
java_path="/lxserver/env/jdk/8/bin/java"
deploy_path="/lxserver/project/backend/${1}"
env_config="--spring.profiles.active=product"
package_name="lxzl-${1}.jar"

jvm="-server -Xms2g -Xmx2g -Xmn1g -Xss1M -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=256M \
-XX:+DisableExplicitGC -XX:SurvivorRatio=1 -XX:+UseConcMarkSweepGC \
-XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection \
-XX:CMSFullGCsBeforeCompaction=0 -XX:+CMSClassUnloadingEnabled -XX:LargePageSizeInBytes=128M \
-XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=80 \
-XX:SoftRefLRUPolicyMSPerMB=0 -XX:+PrintClassHistogram -XX:+PrintGCDetails \
-XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC"

#if [ $1 -ne "ams-dingding" -o $1 -ne "analytics" -o $1 -ne "bank" -o \
$1 -ne "file-preview" -o $1 -ne "mall-app" -o $1 -ne "worker" ];then
#	echo "参数错误,请执行:ansible-playbook bank_app.yml \
-e project=\"ams-dingding | analytics | bank | file-preview | mall-app | worker\" -v"
#	exit
#fi

if [ $1 == "ams-dingding" -o $1 == "analytics" -o $1 == "bank" -o \
$1 == "file-preview" -o $1 == "mall-app" -o $1 == "worker" ];then
        break
else
        echo "参数错误,请执行:ansible-playbook bank_app.yml -e \
project=\"ams-dingding | analytics | bank | file-preview | mall-app | worker\" -v"
        exit
fi

start_cmd="${java_path} ${jvm} -jar ${deploy_path}/${package_name} ${env_config}"

PID=`ps -ef | grep ${deploy_path}/${package_name} | grep -v grep | awk '{print $2}'`

# 停止应用
if [[ ! -n "$PID" ]];then
	echo "进程不存在"
else
        # 停止服务
        kill -9 ${PID}
        sleep 5

        process_couunt=$(/usr/bin/ps -ef | grep "${deploy_path}/${package_name}" | grep -v grep | wc -l)
        if [[ ${process_couunt} != 0 ]];then
            error_message="旧版本服务停止失败"
	fi
fi

sleep 3

# 启动应用
# ams-dingding  analytics  bank  file-preview  mall-app
if [ $1 == "ams-dingding" -o $1 == "analytics" -o $1 == "bank" ];then
	${start_cmd} > /dev/null 2>&1 &
	if [[ $? == 0 ]];then
        	sleep 5
        	pid=`ps -ef | grep "${deploy_path}/${package_name}" | grep -v grep | awk '{print $2}'`
        	if [ ! -n "$pid" ];then
                	echo "启动失败"
        	else
                	echo "${pid}"
        	fi
	fi

elif [ $1 == "mall-app" -o $1 == "worker" ];then
	sh /lxserver/project/backend/${1}/bin/startup.sh
	if [[ $? == 0 ]];then
        	sleep 5
        	pid=`ps -ef | grep "${1}" | grep -v grep | awk '{print $2}'`
        	if [ ! -n "$pid" ];then
               		echo "启动失败"
        	else
                	echo "${pid}"
        	fi
	fi

elif [ $1 ==  "file-preview" ];then
	sh /lxserver/project/backend/${1}/bin/startup.sh
	if [[ $? == 0 ]];then
        	sleep 5
        	pid=`ps -ef | grep "${1}" | grep -v grep | awk '{print $2}'`
        	if [ ! -n "$pid" ];then
                	echo "启动失败"
        	else
                	echo "${pid}"
        	fi
	fi
else
	exit
fi

  

2. main.yml
- name: copy script
  copy:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: "{{ item.mode }}"
  with_items:
    - { src: 'restart.sh', dest: '/tmp', mode: '0644' }

- name: sh restart
  command: "nohup sh /tmp/restart.sh {{project}}"    # 就是这个nohup要加
3. 执行yml
---
- name: init server
  remote_user: lxops
  become: yes
  become_method: sudo
  become_user: root
  hosts: BANK
  roles:
    - bank
ansible-playbook bank_app.yml -e project=analytics -v

 

标签:脚本,shell,grep,app,echo,XX,ansible,fi,bank
From: https://www.cnblogs.com/The-day-of-the-wind/p/16833162.html

相关文章

  • ansible-playbook 用法
     catinstall_zabbix_3.yaml----name:#名称hosts:new#hosts为文件名,new为hosts文件里得[new]tasks:#任务-name:shell:|rpm-ivhhtt......
  • shell脚本获取今天、昨天、前天、几天前日期
    1、获取当天日期:   today=$(date-d"now"+%Y-%m-%d)  或  today="`date +%Y-%m-%d`00:00:002、获取昨天日期:   yesterday=$(date-d"yesterday"+%Y......
  • python脚本编写流程
    自己实现了一个自动打卡的脚本文件,记录一下过程编写脚本流程1.编写脚本2.将脚本设置为定时启动其实自己用的话,就是这两步,但是我后面多做了两件事3.电脑设置定时提醒......
  • 如何开发一个油猴脚本- 从零开始编写一个油猴脚本
    前言之所以开发这个插件,是因为昨天在知乎看了一篇文章:为什么CSDN上很少人会去给文章点赞?,我引用一下其中一个答主的回答:只有我自己写博客了,我才发发现这个问题,阅读几百......
  • Linux Shell 函数
    Shell函数#!/bin/bash定义在被使用之前,函数必须已经被定义如果函数名重复,后一个函数会覆盖前一个函数functionfoo{ echo"foo"}#也可以不写function关键字ba......
  • Ansible
    1、特点基于ssh架构模块丰富支持自定义模块支持异构IT架构(支持Linux和Windows、Mac)Paramiko(python对ssh的实现),PyYaml和Jinja2三个安全基于openssh幂等性一......
  • ansible练习四
    创建Web内容目录按照下方所述,创建一个名为/home/student/ansible/webcontent.yml的playbook:该playbook在dev主机组中的受管节点上运行创建符合下列要求的目录......
  • b站 bilibili 哔哩哔哩 js小脚本 油猴 查找搜索某年的视频 自动翻页
    varccount=0;varnumnum=5constsleep=(delay)=>newPromise((resolve)=>setTimeout(resolve,delay))while(numnum--){  document.querySelect......
  • jmeter部分脚本执行失败但jenkins构建结果依然成功
    jmeter部分脚本执行失败,但jenkins构建结果依然成功--处理方法 使用jenkins构建执行脚本的时候,大家可能遇到过:明明部分脚本执行失败,但是收到的邮件结果依然是构建成功呢,......
  • mysql学习---简单的数据库初始化脚本和sql分析
    1、创建库、表、权限等DROPDATABASEIFEXISTS`KMS_DB`;CREATEDATABASEIFNOTEXISTS`KMS_DB`CHARACTERSET`utf8`COLLATE`utf8_general_ci`;/*characterset,数......