Score:3

What are the commands for restarting a service after a file is changed in Chef?

cn flag

I am unable to figure out the correct solution in that if only when a file is updated, i.e. a conf file of a service, then the service is restarted (stopped and then started).

This is what I have in my foo.rb file.

cookbook_file '/etc/foo/foo.conf' do
  source 'foo.conf'
  action :touch
end

service 'foo' do
  subscribes :restart, 'file[/etc/foo/foo.conf]', :immediately
end

And what I got after running sudo chef-client on the server

cookbook_file[/etc/foo/foo.conf] action touch
    - update content in file /etc/foo/foo.conf from 695507 to 06105e

* service[foo] action nothing (skipped due to action :nothing)

From what I understand, it recognized the file changed, but the service wasn't restarted.

Score:2
br flag

subscribes must match the resource being subscribed to. It won't give you any errors if it doesn't though so you want

cookbook_file '/etc/foo/foo.conf' do
  source 'foo.conf'
  action :create
end

service 'foo' do
  subscribes :restart, 'cookbook_file[/etc/foo/foo.conf]', :immediately
end

Alternatively, you can do it the other way round i.e. have the cookbook_file notify the service that it needs to restart.

cookbook_file '/etc/foo/foo.conf' do
  source 'foo.conf'
  action :create
  notifies :restart, 'service[foo]', :immediately
end
runningraptor avatar
cn flag
Thank you for the response. This helped! What is odd is that option one works for one service, but doesn't for another. And vice versa for option two. For example, my rsyslog service option two works, but doesn't if I use option one. While my 'foo' service only works with option one. Also, I needed to change the action from `touch` to `create` for either option to work.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.