问题描述
我有一个有趣的剧本,可以杀死正在运行的进程,并且在大多数情况下都能正常工作!但是,有时我们会发现无法杀死进程,因此,"wait_for"超时,并抛出错误并且它停止了该过程.
I have an ansible playbook to kill running processes and works great most of the time!, however, from time to time we find processes that just can't be killed so, "wait_for" gets to the timeout, throws an error and it stops the process.
当前的解决方法是手动进入包装盒,使用"kill -9"并再次运行ansible剧本,所以我想知道是否有任何方法可以解决来自ansible本身的情况?不想从一开始就使用kill -9,但我也许可以解决超时问题?即使在300秒内没有杀死进程的情况下,也可以使用kill -9?但是最好的方法是什么?
The current workaround is to manually go into the box, use "kill -9" and run the ansible playbook again so I was wondering if there is any way to handle this scenario from ansible itself?, I mean, I don't want to use kill -9 from the beginning but I maybe a way to handle the timeout?, even to use kill -9 only if process hasn't been killed in 300 seconds? but what would be the best way to do it?
这些是我目前拥有的任务:
These are the tasks I currently have:
- name: Get running processes
shell: "ps -ef | grep -v grep | grep -w {{ PROCESS }} | awk '{print $2}'"
register: running_processes
- name: Kill running processes
shell: "kill {{ item }}"
with_items: "{{ running_processes.stdout_lines }}"
- name: Waiting until all running processes are killed
wait_for:
path: "/proc/{{ item }}/status"
state: absent
with_items: "{{ running_processes.stdout_lines }}"
谢谢!
推荐答案
您可以忽略wait_for
上的错误,并注册结果以强制杀死失败的项目:
You could ignore errors on wait_for
and register the result to force kill failed items:
- name: Get running processes
shell: "ps -ef | grep -v grep | grep -w {{ PROCESS }} | awk '{print $2}'"
register: running_processes
- name: Kill running processes
shell: "kill {{ item }}"
with_items: "{{ running_processes.stdout_lines }}"
- wait_for:
path: "/proc/{{ item }}/status"
state: absent
with_items: "{{ running_processes.stdout_lines }}"
ignore_errors: yes
register: killed_processes
- name: Force kill stuck processes
shell: "kill -9 {{ item }}"
with_items: "{{ killed_processes.results | select('failed') | map(attribute='item') | list }}"
这篇关于如何使用Ansible杀死正在运行的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!