1

I need to check if a service is already running before it installed using puppet.

My code is as following, but it is keep failing.

exec {  'bud_sh':
cwd      => '/working_dir/',
command  => "Some Command",
path     => '/usr/bin:/usr/sbin:/bin:/usr/local/bin',
provider => 'shell',
onlyif   => "test -f /path/to/shell/script/exist",
unless  =>  "`ps -eaf | grep service | grep -v grep | wc -l` -eq 3"

}

Following is the Error Message.

 Could not evaluate: /bin/sh: 3: command not found 

Appreciate your time and consideration on this matter.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
buddhima87
  • 31
  • 6
  • 1
    As an aside: this is not really a good way to check if a service is running in Puppet, and only in very unusual circumstances would you need to do this. Could you elaborate on what you are trying to achieve here? – Matthew Schuchard Jan 02 '19 at 18:11
  • Actually I need to check if a service is already installed and running on a host before it is installed. – buddhima87 Jan 03 '19 at 12:35
  • It looks like you're having Puppet execute a shell script to perform the installation. Although that can work, I urge you to consider instead packaging up your service (as an RPM, DEB, or whatever is appropriate for your system), putting it in a suitable repository, and managing its installation via a `Package` resource. Aside from being good practice administratively, this will get you the behavior you seem to want for no additional cost. – John Bollinger Jan 04 '19 at 17:19

2 Answers2

3

This error message ...

Could not evaluate: /bin/sh: 3: command not found

indicates that the shell tried to execute '3' as a command, and, unsurprisingly, did not find it. The only plausible source of such an issue in the code you presented is your Exec's unless command:

unless  =>  "`ps -eaf | grep service | grep -v grep | wc -l` -eq 3"

When the command there is executed by the shell, it first executes

ps -eaf | grep service | grep -v grep | wc -l

in a subshell and captures its standard output. That output is slightly cleaned up, and then substituted into the overall command to yield, apparently,

3 -eq 3

, which the shell then tries to execute as the command '3', with two arguments. To instead evaluate that as a conditional expression, you need to present it as arguments to test or [ or similar:

unless  =>  "test `ps -eaf | grep service | grep -v grep | wc -l` -eq 3"
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
2

unless => "ps -eaf | grep service | grep -v grep | wc -l -eq 3"

Other issues aside, you have a syntax error: -eq 3 is not a valid command. If you want to evaluate the output of a shell command in sh, you need to use a test construct. For example:

unless => '[ "$(ps -eaf | grep service | grep -v grep | wc -l)" -eq 3 ]'

On a broader level, the unless statement is looking for a truthy Boolean value. The test construct does that by providing its exit status. Write your statements with that in mind.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • Thank you Todd, I used John's approach and it worked. I tested your solution in a lower environment and this also works for my initial requirement. – buddhima87 Jan 10 '19 at 13:53