puppet onlyif with pgrep 不起作用

Spy*_*dis 3 linux puppet pgrep

只有当它的进程没有运行时,我才想在 puppet 中使用 exec

exec { "execute me":
    onlyif  => "pgrep -fc 'ruby execute.rb'",  
    command => "execute me",
}
Run Code Online (Sandbox Code Playgroud)

因此,在上述情况下,如果进程 'ruby execute.rb' 已经在运行,则 exec 不应作为

pgrep -fc 'ruby execute.rb' will return 1.
Run Code Online (Sandbox Code Playgroud)

然而

该命令似乎总是在 puppet 中运行。

在我的 linux 机器上,当我执行 pgrep -fc 'ruby execute.rb' 时,我总是得到大于 0 的值。

我的命令有问题吗?

小智 5

只有当 pgrep 返回 0 时,您的 exec 才会运行。pgrep 手册页告诉我们,只有在找到匹配的进程时 pgrep 的退出代码才会为零 (0),当没有匹配的进程时它将返回一 (1)。另一方面,puppet 文档告诉我们,参数 onlyif导致仅当参数中的命令返回 0 时才执行资源。

而不是onlyif你应该使用参数,除非. 这样你的 exec 只会在你的 pgrep 返回非零时运行,即。它没有找到任何匹配的进程。

exec { "execute me":
  unless  => "pgrep -fc 'ruby execute.rb'",  
  command => "execute me",
}
Run Code Online (Sandbox Code Playgroud)