如何使用ansible杀死正在运行的进程? [英] How to kill a running process using ansible?

查看:152
本文介绍了如何使用ansible杀死正在运行的进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可靠的剧本来杀死正在运行的进程,并且大部分时间都运行良好!但是,有时我们会发现无法杀死的进程,因此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 playbook,所以我想知道是否有任何方法可以从 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?

这些是我目前的任务:

- 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杀死正在运行的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆