如何检查PID是否在正常运行,如果没有,则使任务失败?

问题描述 投票:0回答:1

我有一本可完成3件事的剧本:

a)启动烧瓶服务器

b)等待10秒钟启动

c)检查PID是否正在运行

- name: "execute script to start flask"
  shell: nohup python main.py &
  args:
    chdir: /home/ubuntu/flask
    executable: /bin/bash

- pause: seconds=10

- name: "verify that the server has started"
  command: ?????

手动执行此操作以获取PID

pgrep -f /'main.py'

哪个返回PID可以说...

8212

我如何在ansible剧本中执行步骤(c),并确保是否未找到PID,任务将失败?

ansible pid
1个回答
0
投票

我不会暂停,它会不必要地降低您的剧本的播放速度。您可以简单地使用pids Ansible-Module和until

Pids:https://docs.ansible.com/ansible/latest/modules/pids_module.html

循环直到:https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#retrying-a-task-until-a-condition-is-met

示例每5秒检查一次重试10次,如果main.py的Pid不可用,则剧本将失败。

---
- hosts: localhost
  tasks:
  - name: check the pid of main.py
    pids:
      name: main.py
    register: pid
    until: pid.pids | length > 0
    delay: 5
    retries: 10
  - debug: var=pid
© www.soinside.com 2019 - 2024. All rights reserved.