我只想在puppet中使用一个exec,如果它的进程没有运行的话
exec { "execute me": onlyif => "pgrep -fc 'ruby execute.rb'", command => "execute me", }
所以在上面的情况下,如果进程“ruby execute.rb”已经在运行,exec不应该运行
pgrep -fc 'ruby execute.rb' will return 1.
然而
这个命令似乎总是在傀儡中运行。
在我的Linux机器上,当我做pgrep -fc'ruby execute.rb'我总是得到一个超过0的值。
我的命令有什么问题吗?
只有当pgrep返回0时,你的exec才会运行。 pgrep手册页告诉我们,只有当find匹配的进程时,pgrep的退出代码才会是0(0),当没有匹配的进程时它将返回1(1)。 另一方面,puppet文档告诉我们,只有当参数中的命令返回0时, 参数onlyif才会导致资源被执行。
而不是只有你应该使用参数除非 。 这样你的exec只会在你的pgrep返回非零时运行。 它没有find任何匹配的进程。
exec { "execute me": unless => "pgrep -fc 'ruby execute.rb'", command => "execute me", }
您是否尝试过使用完整path:/ usr / bin / pgrep?
也许puppet agent在onlyif / unless语句中找不到pgrep命令。 也可以将exec资源types中的命令searchpath指定为属性pathhttps://docs.puppet.com/puppet/latest/reference/type.html#exec-attribute-path
exec { "execute me": onlyif => "pgrep -fc 'ruby execute.rb'", command => "execute me", path => "/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin", }