9

I am in a situation where Chef might start a service (postgres) but it might subsequently be stopped out-of-band. I want a subsequent Chef run to cause the service to be running. I have tried this:

service "postgresql" do
    action :start
end

But it has no effect, saying (up to date) presumably because Chef knows it has been started and isn't able to tell that it has stopped. (Possibly due to how service ... status behaves for this service?) If I write this:

# anti-pattern warning!
execute "force-start-postgresql" do
  command "service postgresql start || /etc/init.d/postgresql start"
  action :run
end

I get the desired behaviour. Also an action :restart makes it running. However these seem like anti-patterns due to portability (and potentially stopping it before starting it again in the latter case).

So, how can I tell Chef to forcibly start the service, even if it thinks it is already running?

This is using Chef 11.6, hosted by OpsCode, and the default postgresql recipe. (Note this is similar but I think not quite the same as How to force actions on "up to date" resources in Chef? .)

--- EDIT (clarification following jtimberland post) ---

The -l debug here shows:

DEBUG: service[postgresql] supports status, running
DEBUG: service[postgresql] is running

Even when it is NOT running. So that sounds like one bug, and I'm interested in that. However I'm primarily interested in whether there is a way to tell Chef "always invoke the service start command, skipping the status check". That's the question here.

(I'm no expert but I think the most portable way to ensure a service is running is to start the service and that's nearly always idempotent. OTOH checking whether a service is running is less consistent and I don't see why we should care!)

11

By default, Chef will check if the service is running, and starts it if the service isn't running.

How it determines that the service is running depends.

By default, Chef will attempt to match the name of the service (postgresql here) in the process table using ps.

ps -ef | grep postgresql

Essentially. The name of the service will be used for the pattern match when inspecting the process table. This may or may not be what you want/need, especially depending on the platform and how it names the "postgresql" service.

However, you can tell Chef that the service supports a "status" command, which means Chef will generally do something like,

/etc/init.d/postgresql status

And use the return code to determine if it's running or not (non-zero is not running).

Chef doesn't do this by default because not all service scripts support a status command (frustratingly), and Chef doesn't innately know what the right thing to do is. It tries to do the sane default thing, but sometimes naïve. Thus, you can tell Chef that the resource has a status command and not be so naïve.

service "postgresql" do
  supports :status => true
  action :start
end

Now, if the service is not actually named "postgresql" but is instead "postgresql-92" or similar, you can do this as:

service "postgresql-92" do
  supports :status => true
  action :start
end

or

service "postgresql" do
  service_name "postgresql-92"
  supports :status => true
  action :start
end

You can find out what's going on in more detail by running chef with debug output, too:

chef-client -l debug
  • This is useful but doesn't quite answer the question. I want to tell it to :start irrespective of the :status. Also I hope it does a ps -ef | grep [p]ostgresql or similar, else it will usually match its own grep command and thus always think the service is running. (Or maybe that's the underlying problem?) – Partly Cloudy Aug 28 '13 at 21:38
  • It seems to me that there is either a bug in Chef, or something is wrong on the system. What platform/platform_version are you running? How did you install postgresql? Package, source? – jtimberman Aug 28 '13 at 22:34
  • To whit, Chef takes idempotent action to manage resources that are defined in a declarative way. The way that you ensure a service is running w/ Chef is to send the start action to the service. If that is not occurring, it is a bug in Chef, a bug in the recipe, or a bug on the system. – jtimberman Aug 28 '13 at 22:38
  • Thanks @jtimberman. I've opened a bug for wrong status being returned at tickets.opscode.com/browse/COOK-334 . However my question here is whether there is a way to force chef to issue the service-start command, skipping status check. service start is almost always idempotent so this should be fine. – Partly Cloudy Aug 29 '13 at 10:39
  • Sorry, bug is tickets.opscode.com/browse/COOK-3526 . – Partly Cloudy Aug 30 '13 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.