'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?



Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohan Kumar P
Solution 2
Solution 3 Karthick Udatha