2017-08-30 50 views
0

有沒有一種方法可以使用這個腳本(或者看起來大致相同的東西)作爲廚師唯一的警衛?不同只有 - 如果條件作爲廚師衛士

#!/bin/bash 
application = application_name 
if (($(ps -ef | grep -v grep | grep $service | wc -l) > 0)) 
then *do something* 
fi 

我嘗試了經典的方式,但這樣的only_if條件默認情況下通過,要麼什麼都不返回或者它告訴我,我不能與整數比較字符串(這是顯而易見的)。

execute "script" do 
command "my command here" 
only_if "$(ps -ef | grep -v grep | grep service_name | wc -l) > 0" 

任何意見或建議表示讚賞。

回答

2

要直接轉換它,您需要使用test命令,通常將其命名爲[only_if "[ $(ps -ef | grep -v grep | grep service_name | wc -l) > 0 ]"。但你也可以做到這一點稍微更漂亮的Ruby代碼來代替:

only_if { shell_out!('ps -ef').include?('service_name') } 
+0

你好,測試命令工作得很好,非常感謝。但是當我嘗試第二段代碼時,我收到了這個錯誤:「NoMethodError:undefined method'include?'爲#「我需要事先聲明該方法嗎? – EdTed

0

要更普遍回答這個問題,如果你有你想用你的only_ifnot_if後衛的任意腳本,你可以沿着做些什麼這些行:

my_guard_script = File.join Chef::Config[:file_cache_path], 'my_guard_script.sh' 

file my_guard_script do 
    content <<- EOF 
#!/bin/sh 

# contents of your script here 
EOF 
end 

resource 'my_awesome_resource' do 
    some_parameter 'some_value' 
    only_if my_guard_script 
end 

警告:我沒有測試過這個,所以有可能是錯誤。