我在食谱中有以下几行
service "apache" do action :stop end # Do something.. service "apache" do action :start end
我发现第二个块没有执行。 任何原因?
服务启动和重新启动的默认设置是将其全部保存到厨师客户端运行的最后。 如果您确实想要启动两个服务或重新启动,请指定它们立即发生:
service "apache" do action :start, :immediately end
这样做通常不是一个好主意,因为多次重启可能会导致不必要的服务中断。 这就是为什么Chef试图保存所有的服务重启,直到运行结束。
通知是处理这个问题的正确方法。
假设您想要执行以下操作:
你会这样做:
# 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
问题是您的资源具有相同的名称。 服务“阿帕奇”不是唯一的,所以厨师是重复他们。 你的select是给他们这样的单独名称
service "apache stop" do service_name "apache" action :stop end # Do something service "apache start" do service_name "apache" action :start end
你也可以使用“#Do something”块的通知来发送a:restart到服务“apache”。 这是人们通常使用的模式,单个服务,发送通知(或使用订阅)。 更多在这里:
http://wiki.opscode.com/display/chef/Resources#Resources-Notifications
Mray和kgilpin的答案是完美的,但是最近我遇到了问题。
不知何故,MySQL服务没有停止之前,我需要重新启动,因此我的食谱失败。 我用'''执行'''资源(我只需要在Ubuntu上工作的食谱)的解决方法。 执行正在等待停止/启动,而服务可能会更早结束(init脚本将完成,而守护进程不会)
执行“停止服务” 命令“服务停止” 结束
我对这个解决scheme并不感到自豪,但它为我工作。
为了增加这个;
除非/etc/init.d/<DAEMON> status返回的值不是0,否则厨师不会启动服务。如果返回0,则厨师会认为它正在运行。