Chef push jobs - detecting if a service is running

Hi all,
I want to restart a service on a windows node using push jobs.

maybe i can have a recipe like below that push jobs can execute:


service ‘w3svc’ do
action [:enable, :restart]
end


Now, how do I know if the restart was successful or not?
Can the service resource above return come code to indicate as such?
Or is there anything I can add in my recipe after the above code to check the status of the service to see if it is running
or not?

thanks.

anyone ?

anyone know ?

Hi @agshah, I had the same question with tomcat service. In my case, I make http requests to localhost to know if the service was ready, then you should make something like that but in your context.

The following link was the example that other user give me:

And my code to verify if tomcat is ready:

# Method to pause execution while tomcat start
def self.wait
	if isRunning?
		require 'mechanize'	
		agent = Mechanize.new	
		agent.user_agent_alias = 'Windows Chrome'

    		begin
			agent.read_timeout = 5 #set the agent time out
			page = agent.get('http://localhost:8080')
			agent.history.pop()   #delete this request in the history
			Chef::Log.info("Tomcat7 Started")
		rescue
    			Chef::Log.info("Wait for Tomcat7 to continue...")
    			agent.shutdown
    			agent = Mechanize.new { |agent| agent.user_agent_alias = 'Windows Chrome'}
    			agent.request_headers
    			sleep(150)
    			retry
    		end
	end
end

[formatting fixed by moderator]

I hope it help you too.

Hi Carlos727,
thanks for the response. I am very new to Chef and Ruby so please allow me to ask you this:

From a workstation I want to restart service on remote windows node. So I am thinking of using Chef push jobs to execute the recipe I have posted in my question. So essentially start the job from the workstation as below:
knife job start ‘iis_restart’ ‘node1’

This will execute the job/recipe on the node. Now how do I know on the workstation whether the service was restarted successfully.

Will this approach that you suggest get me the status on the workstation whether the service restarted successfully on the remote node? Because the job/recipe is executing on the node.

Sorry, I am looking for that in this moment. I suppose that chef has a resourse to notify with mails or something like that. However, you can use ruby_block resource and some gem that allow you send one notification with service status. In this moment, I don’t have any example but I believe that it’s possible.

From a workstation I want to restart service on remote windows node. So I am thinking of using Chef push jobs to execute the recipe I have posted in my question. So essentially start the job from the workstation as below:
knife job start 'iis_restart' 'node1'

Now this is possible, but I suggest you take a look at: https://learn.chef.io/tutorials/ to get a strong grasp of Chef and the paradigm that Chef uses.

As an aside, i would actually use knife winrm to do the remote restart, knife job is designed for larger groups of machines compared to one or two.

@Matt_Wrock might have a good suggestion on using knife winrm to restart services. :metal:

That is correct. you can use something like:

knife winrm -m <host or ip> 'net stop <service> && net start <service>' -x administrator -P mypassword

awesome, thanks everyone.

Hi,
I am able to use knife winrm as suggested to restart IIS as seen below. It provides the output that can tell me if the restart was successful or not. But I want to have this automated. It seems I need to parse the output to determine the success or failure? Is there a way to get the return code of what I execute so I know if it is a success or not?

D:\learn-chef\chef-repo>knife winrm -m node1 ‘iisreset’ -x testuser -P testpass
node1
node1 Attempting stop…
node1 Internet services successfully stopped
node1 Attempting start…
node1 Internet services successfully restarted

knife winrm will exit with the same exit code returned from the command

that’s great. thanks Matt. One more question please. I see there is a --returns option that winrm takes.
What exactly is this?

The returns parameter can be a list of exit code you expect to indicate success. 0 is the default. If knife-windows returns an exit code that does not match this list then it raises an error.