AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 836596
Accepted
grizzthedj
grizzthedj
Asked: 2017-03-07 06:55:12 +0800 CST2017-03-07 06:55:12 +0800 CST 2017-03-07 06:55:12 +0800 CST

如何测试重启是否完成?

  • 772

我目前正在构建一个基础设施管理工具,用于配置裸机和虚拟机等。我们有一个工作虚拟机,它通过 SSH 在远程节点上运行命令(通过 ansible)。

其中一个步骤需要重新启动节点以应用一些配置。重启完成后,工作进程必须在节点上运行更多命令(必须同步完成)。

我的问题是,如何检查重启是否完成?

我可以添加一个睡眠定时器(等到重启完成),但我觉得这是一个糟糕的解决方案,原因有很多。

另一种选择是每隔 5 秒左右从我的工作进程尝试 SSH 到远程节点,如果失败,请继续重试,直到我获得成功连接。

还有另一种方法吗?

linux foreman
  • 2 2 个回答
  • 3659 Views

2 个回答

  • Voted
  1. Best Answer
    Sethos II
    2017-03-07T23:07:36+08:002017-03-07T23:07:36+08:00

    正如您提到您正在通过 ansible 运行命令,这是我在剧本中用于重新启动的内容(我正在管理 Ubuntu 14/16.04 机器):

    ---
    # execute like:
    # ansible-playbook reboot.yaml --inventory hosts --extra-vars "hosts=all user=admin"
    # or
    # ansible-playbook reboot.yaml -i hosts -e "hosts=all user=admin"
    - hosts: "{{ hosts }}"
      remote_user: "{{ user }}"
      become: yes
      tasks:
        # add this to to guard you from yourself ;)
        #- name: "ask for verification"
        #  pause:
        #    prompt: "Are you sure you want to restart all specified hosts?"
    
        # here comes the juicy part
        - name: "reboot hosts"
          shell: "sleep 2 && shutdown -r now 'Reboot triggered by Ansible'" # sleep 2 is needed, else this task might fail
          async: "1" # run asynchronously
          poll: "0" # don't ask for the status of the command, just fire and forget
          ignore_errors: yes # this command will get cut off by the reboot, so ignore errors
        - name: "wait for hosts to come up again"
          wait_for:
            host: "{{ inventory_hostname }}"
            port: "22" # wait for ssh as this is what is needed for ansible
            state: "started"
            delay: "120" # start checking after this amount of time
            timeout: "360" # give up after this amount of time
          delegate_to: "localhost" # check from the machine executing the playbook
    ...
    

    更新

    Ansible 2.7 现在有一个reboot 模块,所以你不需要自己创建命令。上面的剧本将转化为:

    ---
    # execute like:
    # ansible-playbook reboot.yaml --inventory hosts --extra-vars "hosts=all user=admin"
    # or
    # ansible-playbook reboot.yaml -i hosts -e "hosts=all user=admin"
    - hosts: "{{ hosts }}"
      remote_user: "{{ user }}"
      become: yes
      tasks:
        # add this to to guard you from yourself ;)
        #- name: "ask for verification"
        #  pause:
        #    prompt: "Are you sure you want to restart all specified hosts?"
    
        - name: "reboot hosts"
          reboot:
            msg: "Reboot triggered by Ansible"
            reboot_timeout: 360
    ...
    
    • 1
  2. Mikhail Khirgiy
    2017-03-07T10:06:50+08:002017-03-07T10:06:50+08:00

    如果你想检查主机的状态、重启时间和许多其他参数,那么你应该使用Zabbix、Nagios等监控软件。

    重启时间可以通过uptime系统参数检查。它显示自上次启动以来的时间。您可以uptime在 Linux/UNIX 主机上通过命令或在主机上运行 snmpd 服务时通过 SNMP 协议远程获取它:

    snmpget -v2c -c public host_name_or_ip_address sysUpTime.0
    
    • -2

相关问题

  • 多操作系统环境的首选电子邮件客户端

  • 你最喜欢的 Linux 发行版是什么?[关闭]

  • 更改 PHP 的默认配置设置?

  • 保护新的 Ubuntu 服务器 [关闭]

  • (软)Ubuntu 7.10 上的 RAID 6,我应该迁移到 8.10 吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve