Ansible – 如何 Grep (ps -few) 並殺死遠程主機上運行的任何 linux 進程?
已發表: 2022-02-24Ansible 是一個非常了不起的系統管理工具。 過去幾週,我們在 Ansible 上發表了許多文章,內容涉及如何在遠程主機上複製文件、如何在遠程主機上執行命令、如何在遠程主機上安裝 Java、Python 等等。
在本教程中,我們將介紹如何 grep 在遠程主機上運行的 java 進程並使用簡單的 ansible playbook 殺死該遠程進程。
以下是我們將在本教程中執行的步驟:
- 在遠程主機上,運行 CrunchifyAlwaysRunningProgram.java
- 遵循如何在 Java 中永遠運行程序的教程
- 使用
nohup java CrunchifyAlwaysRunningProgram &
運行 java 程序
1 2 3 |
ubuntu @ ip - 172 - 31 - 10 - 150 : ~ $ nohup java CrunchifyAlwaysRunningProgram & [ 1 ] 18174 ubuntu @ ip - 172 - 31 - 10 - 150 : ~ $ nohup : ignoring input and appending output to 'nohup.out' |
如何檢查進程是否在遠程主機上啟動並運行?
簽出進程ID 18174
。
1 2 3 |
ubuntu @ ip - 172 - 31 - 10 - 150 : ~ $ ps - few | grep CrunchifyAlwaysRunningProgram ubuntu 18174 15069 1 15 : 15 pts / 0 00 : 00 : 00 java CrunchifyAlwaysRunningProgram ubuntu 18187 15069 0 15 : 16 pts / 0 00 : 00 : 00 grep -- color = auto CrunchifyAlwaysRunningProgram |
- 創建具有遠程主機 IP 的文件
crunchify-hosts
文件 - 創建文件
crunchify-grep-kill-process.yml
具有用於 grep 和殺死 java 進程的 ansible 任務 - 運行命令:ansible-playbook -i ./crunchify-hosts crunchify-grep-kill-process.yml
- 在 macOS 終端控制台上檢查結果
crunchify-hosts 文件
1 2 3 4 5 6 7 8 9 10 |
[ local ] localhost ansible_connection = local ansible_python_interpreter = python [ crunchify ] 3.16.83.84 [ crunchify : vars ] ansible_ssh_user = ubuntu ansible_ssh_private_key_file =/ Users / crunchify / Documents / ansible / crunchify . pem ansible_python_interpreter =/ usr / bin / python3 |
文件包含遠程 IP 地址和憑據,這將有助於 ansible 無需密碼即可登錄。
crunchify-grep-kill-process.yml 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
--- - hosts : crunchify become : yes tasks : - name : Get running processes list from remote host ignore_errors : yes shell : "ps -few | grep CrunchifyAlwaysRunningProgram | awk '{print $2}'" register : running_processes - name : Kill running processes ignore_errors : yes 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 : crunchify_processes - name : Force kill stuck processes ignore_errors : yes shell : "kill -9 {{ item }}" with_items : "{{ crunchify_processes.results | select('failed') | map(attribute='item') | list }}" |
這裡 ansible playbook 文件正在獲取所有 java 進程,使用簡單的kill -9
命令將其殺死。
執行 Ansible 劇本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
bash1 . 2 $ ansible - playbook - i . / crunchify - hosts crunchify - grep - kill - process . yml PLAY [ crunchify ] ************************************************************************************************************************************************************** TASK [ Gathering Facts ] ******************************************************************************************************************************************************** ok : [ 3.16.83.84 ] TASK [ Get running processes list from remote host ] **************************************************************************************************************************** changed : [ 3.16.83.84 ] TASK [ Kill running processes ] ************************************************************************************************************************************************* changed : [ 3.16.83.84 ] = > ( item = 18174 ) failed : [ 3.16.83.84 ] ( item = 18342 ) = > { "changed" : true , "cmd" : "kill 18342" , "delta" : "0:00:00.002602" , "end" : "2019-05-10 15:20:36.957062" , "item" : "18342" , "msg" : "non-zero return code" , "rc" : 1 , "start" : "2019-05-10 15:20:36.954460" , "stderr" : "/bin/sh: 1: kill: No such process" , "stderr_lines" : [ "/bin/sh: 1: kill: No such process" ] , "stdout" : "" , "stdout_lines" : [ ] } failed : [ 3.16.83.84 ] ( item = 18344 ) = > { "changed" : true , "cmd" : "kill 18344" , "delta" : "0:00:00.002648" , "end" : "2019-05-10 15:20:38.479354" , "item" : "18344" , "msg" : "non-zero return code" , "rc" : 1 , "start" : "2019-05-10 15:20:38.476706" , "stderr" : "/bin/sh: 1: kill: No such process" , "stderr_lines" : [ "/bin/sh: 1: kill: No such process" ] , "stdout" : "" , "stdout_lines" : [ ] } . . . ignoring TASK [ wait_for ] *************************************************************************************************************************************************************** ok : [ 3.16.83.84 ] = > ( item = 18174 ) ok : [ 3.16.83.84 ] = > ( item = 18342 ) ok : [ 3.16.83.84 ] = > ( item = 18344 ) TASK [ Force kill stuck processes ] ********************************************************************************************************************************************* PLAY RECAP * ******************************************************************************************************************************************************************* 3.16.83.84 : ok = 4 changed = 2 unreachable = 0 failed = 0 |

如何驗證?
只需嘗試在遠程主機上再次 grep 進程。
1 2 |
ubuntu @ ip - 172 - 31 - 10 - 150 : ~ $ ps - few | grep CrunchifyAlwaysRunningProgram ubuntu 18484 15069 0 15 : 22 pts / 0 00 : 00 : 00 grep -- color = auto CrunchifyAlwaysRunningProgram |
如您所見,您不會在列表中看到process ID 18174
,並且沒有任何 Java 進程正在運行。
而已。
這是使用 Ansible grep Java 進程和殺死的最簡單方法。 如果您在運行此Ansible playbook
時遇到任何問題,請告訴我。