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
时遇到任何问题,请告诉我。