Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chef - start OR restart service as needed

Tags:

chef-infra

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.

like image 386
Jake Avatar asked May 20 '13 22:05

Jake


People also ask

How do I restart chef client service?

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.

How do I stop chef services in Linux?

Stops and disables chef-client systemd unit: sudo systemctl stop chef-client and sudo systemctl disable chef-client.

What are phases of chef execution?

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.


1 Answers

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.

like image 189
StephenKing Avatar answered Oct 02 '22 14:10

StephenKing