Linux service status from chef's recipe
How to check the status of a Linux service from a chef's recipe? Here's my scenario:
- I would like to follow a few steps only if the service is not running.
I would like to store the status of the service in a variable (if possible) so I can check the value of the variable and proceed accordingly.
Edit:
To clarify what I am trying to accomplish.
- I need to install my application as a linux service from a chef.
- But before installing the service, I want to check if the service is running on the machine.
-
In Linux terminal, I would use the command
service status myservice.
-
If the service is not installed the command will return
myservice: unrecognized service
-
If installed and running, it will return
run.sh (pid 10777) is running ...
I would like to define the status of myservice before proceeding with the setting in the chef's recipe.
source to share
You can create a simple helper like:
module MyServiceChecker
def my_service_running?
cmd = Mixlib::ShellOut.new('/etc/init.d/my_service status')
cmd.run_command
cmd.exitstatus == 0
end
end
Chef::Recipe.send(:include, MyServiceChecker)
Chef::Resource.send(:include, MyServiceChecker)
Chef::Provider.send(:include, MyServiceChecker)
And then use this helper if you want to check if the service is running. The command may differ depending on your service and Linux derivative.
source to share