How to kill a running process using ansible?

Julio Fernandez

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.

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 }}"

Thanks!

Eric Citaire

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 }}"

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Check if process is running and kill it

分類Dev

How to kill a process on a port on ubuntu

分類Dev

How to kill process of another user?

分類Dev

How to kill a daemon process in linux?

分類Dev

How to kill a child process started by process in java?

分類Dev

How to kill process if thread encounters exception?

分類Dev

How to hide a running process ?

分類Dev

How to access open terminal with running process using SSH?

分類Dev

How to check the process is already running or not

分類Dev

when parent process is killed by 'kill -9', how to terminate child processes

分類Dev

How to kill VueJS application running on localhost:8080 (MacOS)

分類Dev

How can I fully kill a program and/or python code running on Windows?

分類Dev

kill unresponsive process

分類Dev

Kill Local Process

分類Dev

Kill #### but process still there?

分類Dev

How to create directories on the server where ansible is running

分類Dev

How to stop a running python script by Ansible?

分類Dev

Check if a process is running and if not, restart it using Cron

分類Dev

How can I kill a child process without the child process 'exit' event triggering?

分類Dev

How to view the memory map of a running process?

分類Dev

How to test if the Couchbase process is running in Ubuntu?

分類Dev

Capturing kill code of go process?

分類Dev

Kill system process in Thread ruby

分類Dev

Kill child process created with fork

分類Dev

Unable to kill processes running concurrently

分類Dev

Kill my process if the other process is killed

分類Dev

How to trigger an alert notification of a long-running process in Azure Data Factory V2 using either Azure Monitor or ADF itself?

分類Dev

How to run long running commands with pipes using Node.js child_process spawn [Edit: Especially piping into grep]

分類Dev

How do I get a variable with the name of the user running ansible?

Related 関連記事

  1. 1

    Check if process is running and kill it

  2. 2

    How to kill a process on a port on ubuntu

  3. 3

    How to kill process of another user?

  4. 4

    How to kill a daemon process in linux?

  5. 5

    How to kill a child process started by process in java?

  6. 6

    How to kill process if thread encounters exception?

  7. 7

    How to hide a running process ?

  8. 8

    How to access open terminal with running process using SSH?

  9. 9

    How to check the process is already running or not

  10. 10

    when parent process is killed by 'kill -9', how to terminate child processes

  11. 11

    How to kill VueJS application running on localhost:8080 (MacOS)

  12. 12

    How can I fully kill a program and/or python code running on Windows?

  13. 13

    kill unresponsive process

  14. 14

    Kill Local Process

  15. 15

    Kill #### but process still there?

  16. 16

    How to create directories on the server where ansible is running

  17. 17

    How to stop a running python script by Ansible?

  18. 18

    Check if a process is running and if not, restart it using Cron

  19. 19

    How can I kill a child process without the child process 'exit' event triggering?

  20. 20

    How to view the memory map of a running process?

  21. 21

    How to test if the Couchbase process is running in Ubuntu?

  22. 22

    Capturing kill code of go process?

  23. 23

    Kill system process in Thread ruby

  24. 24

    Kill child process created with fork

  25. 25

    Unable to kill processes running concurrently

  26. 26

    Kill my process if the other process is killed

  27. 27

    How to trigger an alert notification of a long-running process in Azure Data Factory V2 using either Azure Monitor or ADF itself?

  28. 28

    How to run long running commands with pipes using Node.js child_process spawn [Edit: Especially piping into grep]

  29. 29

    How do I get a variable with the name of the user running ansible?

ホットタグ

アーカイブ