Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

Ansible – How to Grep (ps -few) and Kill any linux process running on Remote Host?

Ansible – How to Grep (ps -few) and Kill any linux process running on Remote Host

Ansible is pretty amazing system admin tool. We have published number of articles on Ansible in last few weeks on how to copy files on remote host, How to Execute Commands on remote Hosts, how to install Java, Python on remote host and so on.

In this tutorial, we will go over how to grep java process running on remote host and kill that remote process using simple ansible playbook.

Here are the steps we will do in this tutorial:

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'

How to check if process is started and running on remote host?

Ansible - How to Grep (ps -few) and Kill Process running on Remote Host?

check out process ID 18174.

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
  • create file crunchify-hosts file which has remote host IP
  • create file crunchify-grep-kill-process.yml with has ansible tasks for grep and kill java process
  • run command: ansible-playbook -i ./crunchify-hosts crunchify-grep-kill-process.yml
  • check result on macOS terminal console

crunchify-hosts file

[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

File contains remote IP address and credentials which will help ansible to login without password.

crunchify-grep-kill-process.yml file

---
- 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 "
      with_items: ""

    - wait_for:
        path: "/proc//status"
        state: absent
      with_items: ""
      ignore_errors: yes
      register: crunchify_processes

    - name: Force kill stuck processes
      ignore_errors: yes
      shell: "kill -9 "
      with_items: "Liquid error: wrong number of arguments (given 1, expected 2)"

Here ansible playbook file is getting all java processes, killing it using simple kill -9 command.

Execute Ansible Playbook:

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

How to verify?

Just try to grep process again on remote host.

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

As you notice, you won’t see process ID 18174 in list and there isn’t any java process running.

That’s it.

This is the simplest way to grep Java process and kill using Ansible. Let me know if you face any issue running this Ansible playbook.

The post Ansible – How to Grep (ps -few) and Kill any linux process running on Remote Host? appeared first on Crunchify.

Enregistrer un commentaire

0 Commentaires