Search code examples
chef-infrasystemd

How to reload a systemd service when using Chef


I have a systemd service that is managed using Chef.

What is the Chef way to do a systemctl daemon-reload?

So for example as shown below, I can perform a reload of my myservice but this doensn't do a systemctl daemon-reload.

template '/etc/systemd/system/myservice.service do
  notifies :reload,'service[myservice]', :immediately 
end

service 'myservice' do
  supports status: true ...
  action %i[start enable]
end

Solution

  • We can manage Systemd service definitions using the systemd_unit resource. This way, if the service configuration changes it will trigger a reload with the triggers_reload (set to true by default) property.

    Since you are using a template, you could use the systemd_unit resource with reload action for your service.

    template '/etc/systemd/system/myservice.service' do
      notifies :reload, 'systemd_unit[myservice.service]', :immediately 
    end
    
    systemd_unit 'myservice.service' do
      action :nothing
    end
    

    Other option is to create an execute resource, and run the actual daemon-reload command:

    template '/etc/systemd/system/myservice.service' do
      notifies :run, 'execute[daemon-reload]', :immediately 
    end
    
    execute 'daemon-reload' do
      command 'systemctl daemon-reload'
      action :nothing
    end