问题
Is there a way for Chef to determine if it has changed any file in a given directory? I'd like to restart a server process if any settings in a conf.d directory are updated.
I'm sure it's possible to write a list of md5sums to a file on each chef-client run, and compare current to previous iterations. But that's a fair bit of code to address what seems to be a common scenario. Is there a better way?
回答1:
Chef provides a concept called notifications.
This allows you to define that a change in one resource triggers execution of another resource. The mentioned restart of a service after the config file changed is probably the most common use case.
template "/etc/foo/conf.d/example.conf" do
notifies :restart, "service[foo]"
end
service "foo" do
supports :restart => true, :reload => true
action :enable
end
By default, notifications are :delayed
, which means that they are triggered at the end of the Chef run. This helps you to avoid e.g. a service to restart once for every single changed configuration file. If you want an immediate notification, use
notifies :restart, "service[foo]", :immediately
Of course, you can use Chef's notifications not only for services, but for any resource. More examples are given in the documentation.
回答2:
You can use inotifywait utility from the inotify-tools package
来源:https://stackoverflow.com/questions/29419190/how-to-determine-if-any-file-in-a-directory-has-changed