Johnny Gillespie
Johnny Gillespie

Reputation: 225

Checking if process is running

I have written a small playbook that checks to see if a given process is running on the host machine. I have written this as follows:

- name: checking process running for clients and start process when not
  service:
    name: marketaccess {{ item.key|lower }} -vi
    state: started
  register: process
  with_dict: "{{ customers }}"

When the dictionary's item.key = BROADWAY and the process I am trying to check is as follows :

enter image description here

But when I run this playbook the error that is reported is:

Could not find the requested service marketacces broadway -vi: host

Please can someone see what I am doing wrong?

Upvotes: 5

Views: 28088

Answers (4)

Castiel Li
Castiel Li

Reputation: 11

There's a new windows module on ansible that can be used for this purpose.

https://docs.ansible.com/ansible/latest/collections/community/windows/win_wait_for_process_module.html#examples

You can find the process name:

  1. Right-click on the process in task manager
  2. Open properties
  3. Copy the name WITHOUT extension

Upvotes: 0

Karthick Udatha
Karthick Udatha

Reputation: 1

As per the ansible documentation its better to use the "pid" module to check if there are any processes running. References are below https://docs.ansible.com/ansible/2.9/modules/pids_module.html

Upvotes: 0

Mohan Kumar P
Mohan Kumar P

Reputation: 3284

I have used pgrep since "ps -ef" output varies between the servers (at least in my case).

- name: "Get process PID"
  shell: >
    echo -n
    $(pgrep {{ item.binary_name }})
  args:
    executable: "{{ shell_path }}"
  register: my_procs

Upvotes: 4

techraf
techraf

Reputation: 68559

Please can someone see what I am doing wrong?

Service is not the same as process in computing.

There is no module to manage/check processes in Ansible. You can use the command included in the question with shell module (pay attention to this), register the result and run a task to start the process.

However, it is equally possible that your marketaccess is in fact a service, or that yet another service creates child processes. There is no way to tell. Refer to the manual of your software.

You can provide arguments to the service module as arguments parameter.

Upvotes: 1

Related Questions