Ask Your Question
1

Execute a command onlyif service is not running.

asked 2017-06-22 08:55:55 -0500

rohithmn03 gravatar image

Hi Team,

exec {'start_service':
        command => '/opt/agent/bin/service start --accept-license',
        onlyif => ".....",
    }

The command should get execute only if the service "splunk" is not running. I would like to put ps -ef | grep -i "splunk"

Please help here..!

Regards, Rohith

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2017-06-23 07:12:18 -0500

DarylW gravatar image

updated 2017-06-23 07:18:56 -0500

You really should be managing services with the service resource A proper service setup (with an initscript or systemd configuration) would be set up to do what you need, like only run the start command when the service isn't running, do PID tracking, etc. Also, there are already some good splunk modules on the forge, including the one which used to be controlled by puppetlabs, which has been handed off to voxpupuli

If you need help creating an init script, look towards https://github.com/jordansissel/pleas...

However, if you absolutely MUST manage your service with an exec (which is a bad practice), look at the documentation for how the onlyif parameter to see how it works... Begin by understanding what is going on. You can put in /bin/true and /bin/false to see how the parameter will respond, and then check return value the command(s) that you want to validate that it is doing what you expect.. Typing echo $? after you've run a command will show you it's exit code. 0 is true, and nonzero is false.

With your above example, if you used onlyif => 'ps -ef | grep -i "splunk" ', what is it saying? it is saying 'run this exec onlyif 'splunk' is found in the output of the process list', which is the opposite of what you want. You want to say is 'run this exec unless 'splunk' is found in the output of the process list', so you would wand to use the unless parameter

Even understanding how unless, onlyif, and creates work with an exec, you are probably still better off managing it as a 'service' inside of puppet without a true init script using the base provider, and supplying the appropriate start, stop, and status commands.

service {'splunk':
  start    => '/opt/agent/bin/service start --accept-license',
  status   => '/opt/agent/bin/service status',
  stop     => '/opt/agent/bin/service stop',
  provider => 'base',
}
edit flag offensive delete link more

Comments

Hi, I used below onlyif, and this is working fine..! onlyif => "test `ps -ef | grep -v grep | grep splunk | wc -l` -eq 0", Thanks..! for your advice. will try to incorporate it

rohithmn03 gravatar imagerohithmn03 ( 2017-06-23 12:41:08 -0500 )edit

You should use 'unless' instead of 'onlyif', that will be 'true' in the opposite cases than 'onlyif'

DarylW gravatar imageDarylW ( 2017-06-26 07:54:42 -0500 )edit

Using the service type and base provider, you can still do EXACTLY what you're trying to do with `ps -ef | grep -i splunk`, but natively by using the "pattern" attribute. It really is better to use the service type. https://docs.puppet.com/puppet/latest/types/service.html#service-attribute-pattern

reidmv gravatar imagereidmv ( 2017-06-28 16:41:28 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-06-22 08:55:55 -0500

Seen: 108 times

Last updated: Jun 23