Chef

廚師按順序停止和啟動服務

  • May 20, 2014

我的食譜中有以下幾行

service "apache" do
 action :stop
end

# Do something..

service "apache" do
 action :start
end

我發現第二個塊沒有執行。任何原因?

服務啟動和重新啟動的預設設置是將其全部保存到 chef-client 執行結束。如果您確實想要啟動或重新啟動兩個服務,請指定它們將立即發生:

service "apache" do 
 action :start, :immediately
end

這樣做通常不是一個好主意,因為多次重新啟動會導致不必要的服務中斷。這就是 Chef 嘗試保存所有服務重新啟動直到執行結束的原因。

通知是處理此問題的正確方法。

假設您要執行以下操作:

  • 有條件地下載文件

  • 如果文件已下載

    • 立即停止 apache
    • 處理文件(例如解壓縮或移動它)
    • 再次啟動apache

你會這樣做:

# Define the apache service but don't do anything with it
service "apache" do
 action :nothing
end

# Define your post-fetch script but don't actually do it
execute "process my file" do
  ... your code here
 action :nothing
 notifies :start, "service[apache]"
end

# Fetch the file. Maybe the file won't be fetched because of not_if or checksum.
# In that case apache won't be stopped or started, it will just keep running.
remote_file "/tmp/myfile" do
 source "http://fileserver/myfile"
 notifies :stop, "service[apache]", :immediately
 notifies :run, execute["process my file"]
end

引用自:https://serverfault.com/questions/378520