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?

6
  • 1
    SALT.STATES.SERVICE maybe ?
    – Tensibai
    Commented Aug 21, 2017 at 14:43
  • @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
    Commented Aug 21, 2017 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
    Commented Aug 21, 2017 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
    Commented Aug 22, 2017 at 6: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
    Commented Aug 22, 2017 at 7:49

1 Answer 1

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.

2
  • 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
    Commented Sep 18, 2017 at 7: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
    Commented Jan 5, 2018 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.