gpt4 book ai didi

linux - 如何处理永无休止的循环linux命令-ansible

转载 作者:太空宇宙 更新时间:2023-11-04 05:23:06 25 4
gpt4 key购买 nike

请引用此link了解我所做的事情。

简短描述

我需要在远程计算机中运行 top 命令并获取结果内容,然后将该文件保存在本地计算机中

test.yml

---
- hosts: webservers
remote_user: root
tasks:
- name: 'Copy top.sh to remote machine'
synchronize: mode=push src=top.sh dest=/home/raj

- name: Execute the script
command: sh /home/raj/top.sh
async: 45
poll: 5

- name: 'Copy system.txt to local machine'
synchronize: mode=pull src=system.txt dest=/home/bu

top.sh

 #!/bin/bash
top > system.txt

问题

top.sh永远不会结束,所以我尝试每五秒轮询一次结果并将其复制到本地计算机中,但它不起作用。它会抛出以下错误。

stderr: top: failed tty get
<job 351267881857.24744> FAILED on 192.168.1.7

注意:仅当我包含异步和轮询选项时,我才收到此错误

最佳答案

你好比拉尔,我希望这对你有用

您的语法:使用 poll:5 按照此链接 http://docs.ansible.com/ansible/playbooks_async.html

poll 正在使用等待任务完成的方式。但 top 命令直到使用 stop 或系统关闭后才会停止。使用民意调查:0

“或者,如果您不需要等待任务完成,您可以通过指定轮询值 0 来“即发即忘”:”

现在忘记任务,从远程收集顶部结果文件并将其存储到本地使用以下语法

<小时/>
    - hosts: webservers
remote_user: root
tasks:
- name: 'Copy top.sh to remote machine'
synchronize: mode=push src=top.sh dest=/home/raj

- name: collecting top result
command: sh /home/raj/top.sh
async: 45
poll: 0

- name: 'Copy top command result to local machine'
synchronize: mode=pull src=/home/raj/Top.txt dest=/home/raj2/Documents/Ansible

top.sh:

  #!/bin/bash

top -b > /home/raj/Top.txt

这对我有用。如果您有任何问题,请联系我。

关于linux - 如何处理永无休止的循环linux命令-ansible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37958779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com