如何使用 ansible 终止 运行 进程?
How to kill a running process using ansible?
我有一个 ansible playbook 可以杀死 运行ning 进程并且大部分时间都很好用!但是,有时我们会发现进程无法被杀死,"wait_for" 超时,抛出错误并停止进程。
当前的解决方法是手动进入框,再次使用 "kill -9" 和 运行 ansible 剧本,所以我想知道是否有任何方法可以从 ansible 本身处理这种情况?,我的意思是,我不想从一开始就使用 kill -9 但我也许是一种处理超时的方法?甚至仅当进程在 300 秒内未被终止时才使用 kill -9?但最好的方法是什么?
这些是我目前的任务:
- name: Get running processes
shell: "ps -ef | grep -v grep | grep -w {{ PROCESS }} | awk '{print }'"
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
上的错误并注册结果以强制终止失败的项目:
- name: Get running processes
shell: "ps -ef | grep -v grep | grep -w {{ PROCESS }} | awk '{print }'"
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 }}"
使用pkill
(1)代替grep
+kill
。
killall
命令有一个可能有用的等待选项。
安装 psmisc:
tasks:
- apt: pkg=psmisc state=present
然后像这样使用 killall:
tasks:
- shell: "killall screen --wait"
ignore_errors: true # In case there is no process
-w, --wait
:
等待所有被杀死的进程死亡。 killall 每秒检查一次是否有任何被杀死的进程仍然存在,如果还剩下 none,则只检查 returns。
经过一些调查和与 ansible_linter
的斗争后,下面是另一种方法(例如,不使用 ignore_error
)
- name: "disable and stop PROCESS"
service:
name: PROCESS
enabled: no
state: stopped
tags:
- stop
- name: "evaluate running PROCESS processes from remote host"
command: "/usr/bin/pgrep PROCESS"
register: running_PROCESS
failed_when: running_PROCESS.rc > 1
changed_when:
- running_PROCESS.rc == 0
tags:
- stop
- name: "pkill PROCESS running processes"
command: "/usr/bin/pkill PROCESS"
register: kill_PROCESS
changed_when: kill_PROCESS.rc == 0
when: running_PROCESS.stdout_lines | length > 0
tags:
- stop
- name: "check PROCESS processes"
wait_for:
path: "/proc/{{ item }}/status"
state: absent
with_items: "{{ running_PROCESS.stdout_lines }}"
when: running_PROCESS.stdout_lines | length > 0
tags:
- stop
- name: "force kill stuck PROCESS processes"
command: "/usr/bin/pkill -9 PROCESS"
register: kill9_PROCESS
changed_when: kill9_PROCESS.rc == 0
failed_when: kill9_PROCESS.rc > 1
when:
- running_PROCESS.stdout_lines | length > 0
tags:
- stop
我有一个 ansible playbook 可以杀死 运行ning 进程并且大部分时间都很好用!但是,有时我们会发现进程无法被杀死,"wait_for" 超时,抛出错误并停止进程。
当前的解决方法是手动进入框,再次使用 "kill -9" 和 运行 ansible 剧本,所以我想知道是否有任何方法可以从 ansible 本身处理这种情况?,我的意思是,我不想从一开始就使用 kill -9 但我也许是一种处理超时的方法?甚至仅当进程在 300 秒内未被终止时才使用 kill -9?但最好的方法是什么?
这些是我目前的任务:
- name: Get running processes
shell: "ps -ef | grep -v grep | grep -w {{ PROCESS }} | awk '{print }'"
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
上的错误并注册结果以强制终止失败的项目:
- name: Get running processes
shell: "ps -ef | grep -v grep | grep -w {{ PROCESS }} | awk '{print }'"
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 }}"
使用pkill
(1)代替grep
+kill
。
killall
命令有一个可能有用的等待选项。
安装 psmisc:
tasks:
- apt: pkg=psmisc state=present
然后像这样使用 killall:
tasks:
- shell: "killall screen --wait"
ignore_errors: true # In case there is no process
-w, --wait
:
等待所有被杀死的进程死亡。 killall 每秒检查一次是否有任何被杀死的进程仍然存在,如果还剩下 none,则只检查 returns。
经过一些调查和与 ansible_linter
的斗争后,下面是另一种方法(例如,不使用 ignore_error
)
- name: "disable and stop PROCESS"
service:
name: PROCESS
enabled: no
state: stopped
tags:
- stop
- name: "evaluate running PROCESS processes from remote host"
command: "/usr/bin/pgrep PROCESS"
register: running_PROCESS
failed_when: running_PROCESS.rc > 1
changed_when:
- running_PROCESS.rc == 0
tags:
- stop
- name: "pkill PROCESS running processes"
command: "/usr/bin/pkill PROCESS"
register: kill_PROCESS
changed_when: kill_PROCESS.rc == 0
when: running_PROCESS.stdout_lines | length > 0
tags:
- stop
- name: "check PROCESS processes"
wait_for:
path: "/proc/{{ item }}/status"
state: absent
with_items: "{{ running_PROCESS.stdout_lines }}"
when: running_PROCESS.stdout_lines | length > 0
tags:
- stop
- name: "force kill stuck PROCESS processes"
command: "/usr/bin/pkill -9 PROCESS"
register: kill9_PROCESS
changed_when: kill9_PROCESS.rc == 0
failed_when: kill9_PROCESS.rc > 1
when:
- running_PROCESS.stdout_lines | length > 0
tags:
- stop