6

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?

jto
  • 378
  • 1
  • 6
  • 19
  • 1
  • @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 Aug 21 '17 at 14:47
  • 2
    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 will do. – Tensibai Aug 21 '17 at 14:51
  • 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 Aug 22 '17 at 06:34
  • @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 Aug 22 '17 at 07:49
  • 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 Aug 22 '17 at 07:53

1 Answers1

7

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.

Josh Correia
  • 103
  • 4
user2640621
  • 1,395
  • 8
  • 20
  • 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 Sep 18 '17 at 07:15
  • 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 @jto – Tensibai Jan 05 '18 at 17:05