Reputation: 31
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.
Upvotes: 1
Views: 2752
Reputation: 84343
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.
Upvotes: 2
Reputation: 180113
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"
Upvotes: 4