<p>It's a bit hacky, but you can always do something like:</p>
<pre><code>check_process:
  cmd.run:
    - name: ps aux | grep '[f]oobar'
</code></pre>
<p>The exit code will be non-zero and the state will fail if <code>foobar</code> doesn't exist.</p>

CC BY-SA 3.0
2017-09-18T07:15:03.133Z
Thanks for the answer @user2640621 - This is what I was looking for, albeit a bit hacky as you mentioned it works for my situation! If the process isn't active it fails which is what I need!
jto
CC BY-SA 3.0
2018-01-05T17:05:54.067Z
This is always true as `ps aux` will return the grep command (test with `ps aux | grep 'foobar' && echo "ok" || echo "not found"` if you wish to verify)... Usual trick to ensure you're getting really what you're looking for is using the first letter as a character class, this way the grep command line won't match: `ps aux | grep '[f]oobar'` cc @jto
Tensibai
CC BY-SA 4.0
2017-09-17T08:49:37.970Z
2023-02-02T05:36:09.670Z
2023-02-02T05:36:09.670Z
user2640621
<p>Is it possible to check if a specific process is running via a Salt State? Looking through Salt Stack documentation and scouring forums I haven't found any way of simply checking if a service is running and reporting the output.</p>

<p>I have however found this:</p>

<pre><code>httpd-absent:
  process.absent:
    - name: apache2
</code></pre>

<p>The opposite of this would be ideal for my needs e.g:</p>

<pre><code>httpd-present:
  process.present:
    - name: apache2
</code></pre>

<p>Is anyone aware of a method in Salt which would check if a defined process is running based on a name or pattern (regex) and simply report back with a true/false or similar?</p>

CC BY-SA 3.0
2017-08-21T14:43:08.960Z
[SALT.STATES.SERVICE](https://docs.saltstack.com/en/latest/ref/states/all/salt.states.service.html) maybe ?
Tensibai
CC BY-SA 3.0
2017-08-21T14:47:49.127Z
@Tensibai Unforunately not for my specific situation as the process I'm looking to monitor is a process which isn't configured to run as a service.
jto
CC BY-SA 3.0
2017-08-21T14:51:10.817Z
maybe that's the real problem then (sounds unwise to have a daemon running without a supervisor) ? But if that's what you're after maybe [salt.modules.ps.pgrep](https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.ps.html#salt.modules.ps.pgrep) will do.
Tensibai
CC BY-SA 3.0
2017-08-22T06:34:27.277Z
Of course it is! However I'm unable to change this (long story) - The salt.modules.ps.pgrep is something I'll definitely look in to, thanks @Tensibai
jto
CC BY-SA 3.0
2017-08-22T07:49:41.757Z
@Tensibai After looking in to it I've found that I can't go down this route as my setup requires Python 2.7 on all minions which again isn't possible for a similarly long story.
jto
CC BY-SA 3.0
2017-08-22T07:53:06.433Z
Well, I'm not a salt user, I'm using chef and one of the points driving me there was that is comes with its own ruby interpreter isolated from the managed system (avoiding this kind of issues between the configuration manager needs and my applications needs.
Tensibai
<salt><process>
Check a process is running with Salt Stack?
jto
# Check a process is running with Salt Stack?

(Asked by: username_0 on 2017-08-21)

Is it possible to check if a specific process is running via a Salt State? Looking through Salt Stack documentation and scouring forums I haven't found any way of simply checking if a service is running and reporting the output.
I have however found this:
httpd-absent:
  process.absent:
    - name: apache2

The opposite of this would be ideal for my needs e.g:
httpd-present:
  process.present:
    - name: apache2

Is anyone aware of a method in Salt which would check if a defined process is running based on a name or pattern (regex) and simply report back with a true/false or similar?

**Tags:** salt, process

> **Comments:**
> **username_1**: [SALT.STATES.SERVICE](https://docs.saltstack.com/en/latest/ref/states/all/salt.states.service.html) maybe ?  
> **username_0**: @username_1 Unforunately not for my specific situation as the process I'm looking to monitor is a process which isn't configured to run as a service.  
> **username_1**: maybe that's the real problem then (sounds unwise to have a daemon running without a supervisor) ? But if that's what you're after maybe [salt.modules.ps.pgrep](https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.ps.html#salt.modules.ps.pgrep) will do.  
> **username_0**: Of course it is! However I'm unable to change this (long story) - The salt.modules.ps.pgrep is something I'll definitely look in to, thanks @username_1  
> **username_0**: @username_1 After looking in to it I've found that I can't go down this route as my setup requires Python 2.7 on all minions which again isn't possible for a similarly long story.  
> **username_1**: Well, I'm not a salt user, I'm using chef and one of the points driving me there was that is comes with its own ruby interpreter isolated from the managed system (avoiding this kind of issues between the configuration manager needs and my applications needs.  

---

## 1 answers

### Answer by username_2 on 2017-09-17. Score: 7 | Accepted answer

It's a bit hacky, but you can always do something like:
check_process:
  cmd.run:
    - name: ps aux | grep '[f]oobar'

The exit code will be non-zero and the state will fail if foobar doesn't exist.

> **Comments:**
> **username_0**: Thanks for the answer @username_2 - This is what I was looking for, albeit a bit hacky as you mentioned it works for my situation! If the process isn't active it fails which is what I need!  
> **username_1**: This is always true as `ps aux` will return the grep command (test with `ps aux | grep 'foobar' && echo "ok" || echo "not found"` if you wish to verify)... Usual trick to ensure you're getting really what you're looking for is using the first letter as a character class, this way the grep command line won't match: `ps aux | grep '[f]oobar'` cc @username_0  

---

