solving sql server problems for millions of dbas and developers since 2006



SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Tip Categories SQL Server Tutorials SQL Server Webcasts SQL Server Whitepapers SQL Server Tools SQL Server Questions and Answers MSSQLTips Authors About MSSQLTips SQL Server User Groups MSSLQTips Giveaways MSSQLTips Advertising Options

MSSQLTips Facebook Page MSSQLTips LinkedIn Page MSSQLTips RSS Feed MSSQLTips Twitter Page MSSQLTips Google+ Page





Monitor, Start and Stop SQL Server services using xp_servicecontrol

By: | Read Comments (5) | Print

Jugal has 8+ years of extensive SQL Server experience and has worked on SQL Server 2000, 2005, 2008 and 2008 R2.

Related Tips: 1 | 2 | 3 | 4 | More

Problem

As you know SQL Server runs using a bunch of different services in order for it to operate.  These can be monitored, started and stopped using SQL Server Configuration Manager or Windows Services, but this is not always the most convenient way of checking the services.  Is there any way this can be done within SQL Server Management Studio?  In this tip we take a look at a undocumented stored procedure that will allow you to do this.

Solution

To check the SQL Server services or any other service status using SSMS you can execute the extended stored procedure xp_servicecontrol.  This is un documented extended stored procedure.

Syntax
EXEC xp_servicecontrol N'Querystat|Start|Stop’,N'Service Name'

QueryState 
Use this command to check the Services status 
whether service is running or stopped
Start
Use this command to start the service
Stop
use this command to stop the service

Examples

--See below example to check the status of SQL Services
EXEC xp_servicecontrol N'querystate',N'MSSQLServer'
EXEC xp_servicecontrol N'querystate',N'SQLServerAGENT'
EXEC xp_servicecontrol N'querystate',N'msdtc'
EXEC xp_servicecontrol N'querystate',N'sqlbrowser'
EXEC xp_servicecontrol N'querystate',N'MSSQLServerOLAPService'
EXEC xp_servicecontrol N'querystate',N'ReportServer'
--See below example to start/stop service using SSMS
EXEC xp_servicecontrol N'stop',N'SQLServerAGENT'
EXEC xp_servicecontrol N'start',N'SQLServerAGENT'
--See below example to check non-SQL Service
EXEC xp_servicecontrol querystate, DHCPServer


To run this on multiple servers you can use SQLCMD as shown below or you can use Central Management Servers.

Step 1 Create "MyScript.sql" file using the below T-SQL:

set nocount on
CREATE TABLE #ServicesStatus
( 
myid int identity(1,1),
serverName nvarchar(100) default @@serverName,
serviceName varchar(100),
Status varchar(50),
checkdatetime datetime default (getdate())
)
INSERT #ServicesStatus (Status)
EXEC xp_servicecontrol N'QUERYSTATE',N'MSSQLServer'
update #ServicesStatus set serviceName = 'MSSQLServer' where myid = @@identity
INSERT #ServicesStatus (Status)
EXEC xp_servicecontrol N'QUERYSTATE',N'SQLServerAGENT'
update #ServicesStatus set serviceName = 'SQLServerAGENT' where myid = @@identity
INSERT #ServicesStatus (Status)
EXEC xp_servicecontrol N'QUERYSTATE',N'msdtc';
update #ServicesStatus set serviceName = 'msdtc' where myid = @@identity;
INSERT #ServicesStatus (Status)
EXEC xp_servicecontrol N'QUERYSTATE',N'sqlbrowser'
update #ServicesStatus set serviceName = 'sqlbrowser' where myid = @@identity
select * from #ServicesStatus 
select char(13)
select char(13)
drop table #ServicesStatus

Step 2 Create a Windows batch file and save it with .bat extension. Add the list of server names you want to check as shown below
 
sqlcmd -SServerName1 -E -dtempdb -iMyScript.sql -W >>sqlServicesStatus.txt
sqlcmd -SServerName2 -E -dtempdb -iMyScript.sql -W >>sqlServicesStatus.txt
sqlcmd -SServerName3 -E -dtempdb -iMyScript.sql -W >>sqlServicesStatus.txt
sqlcmd -SServerName4 -E -dtempdb -iMyScript.sql -W >>sqlServicesStatus.txt
sqlcmd -SServerName5 -E -dtempdb -iMyScript.sql -W >>sqlServicesStatus.txt

Step 3 Execute the batch file and then open "sqlServicesStatus.txt" to see the status for all services that were checked.


Notes

If the service that you are checking does not exist you will get an error message like the following:
Msg 22003, Level 16, State 1, Line 0
OpenSOpenService() returned error 1060, 'The specified service does not 
exist as an installed service.'
Also, xp_servicecontrol is an undocument system stored procedure, so you will not get a support for it and moreover there is a chance of code changes or parameter changes, so be aware of this if you decide to use this.

Next Steps

  • Use the above or different commands with xp_servicecontrol extended stored procedure
  • Create a batch file by adding a number of servers and servicess with xp_servicecontrol to list out service status
  • Using this you can extend this to setup a monitoring process to check whether services are running as needed


Related Tips: 1 | 2 | 3 | 4 | More | Become a paid author


Last Update: 6/11/2010

Share: Share 






Comments and Feedback:

Friday, June 11, 2010 - 9:23:45 AM - tskelley Read The Tip

Thanks for the tip.  I have been looking into some type of health monitoring capability for our application that includes a mix of database and windows services.  As the author mentioned, this is an undocumented and therefore not supported by Microsoft.  See link below:

Usage of xp_servicecontrol is unsupported

Now for my question.  How would I select the results from this procedure into a variable, so I could report back to the user that the process failed?  Something like (but does not work):

DECLARE @result nvarchar(255)

EXEC @result = xp_servicecontrol N'querystate',N'sqlbrowser'

IF CHARINDEX('Running', @result) > 0

     PRINT 'Success'

ELSE

     PRINT 'Failure'


Friday, June 11, 2010 - 10:05:28 AM - admin Read The Tip

You could try something like this:

DECLARE @result nvarchar(255)
DECLARE @status table (result nvarchar(255))
INSERT @status EXEC xp_servicecontrol N'querystate',N'MSSQLServer'
SELECT @result = result FROM @status
IF @result = 'Running.'
  
PRINT 'Success'
ELSE
  
PRINT 'Failure'

 


Thursday, August 12, 2010 - 3:23:17 PM - Dr DBA Read The Tip

 Love this tip! I would like to know how to check the Analysis and FulltextSearch service, any idea?

Thanks


Wednesday, August 18, 2010 - 9:58:44 AM - Dr DBA Read The Tip

Here are some more info for analysis and full text search

EXEC master.dbo.xp_servicecontrol N'QUERYSTATE',N'MSSQLServerOLAPService'  - Analysis Service

EXEC xp_servicecontrol N'QUERYSTATE',N'MSFTESQL '  -- Full Text Search Service

 

Does anyone now how to check to see if these, or others, services are not present? If a service say Reporting Services is not installed you get an error. I would be good to first check to see if the service is there then check its status


Thursday, April 05, 2012 - 2:15:35 AM - sagar Read The Tip

Thanks for the tip, is there any option inerting all records in table....

Many thanks in advance



Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email   Notify for updates
Comments
*Enter Code refresh code


 
New SQL Monitor v3.0

New SQL Monitor v3.0


Sponsor Information
"SQL doctor ROCKS! As soon as I ran it, problems that have been giving me headaches were identified and cured."

Quickly and accurately deploy database changes with Red Gate's SQL Compare - the industry standard comparison and deployment tool.

Need SQL Server help and not sure where to turn? Reach out to the Edgewood consultants for a Health Check.

Find and Fix SQL issues with Foglight Performance Analysis. Get a free copy.

Solving SQL Server problems for millions of DBAs and Devs since 2006. Join now.

The SQL Server Security THREAT - It’s Closer Than You Think


Copyright (c) 2006-2012 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com