With Chef, how can I have a service start if it's not running or restart if it is?
I'm running Ubuntu 12.04 and the service I'm currently dealing with is PostgreSQL. Stopping and starting it almost works but Chef seems to try starting it before it's stopped fully and fail.
For :restart, the init script or other service provider can use a restart command; if :restart is not specified, the chef-client attempts to stop and then start a service. For :reload, the init script or other service provider can use a reload command.
Stops and disables chef-client systemd unit: sudo systemctl stop chef-client and sudo systemctl disable chef-client.
There are two stages to a chef run though. A compile phase, to organise what resources need to be run and resolve all variables. Then a run phase where each resource is actually executed.
Usually, you use action :start
when installing a service to make sure it's running after the chef run, see Service resource.
When you modify a file that requires the service to restart in order to make the changes active, you use the actions :reload
or :restart
(dependent on what the init script offers). It then looks like this:
template "/etc/postgresql/pg.conf" do
source "pg.conf.erb"
notifies :restart, "service[postgres]"
end
More about Notifications.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With