How to check if SQL Server Agent is running using t-sql

08 October,2020 by Jack Vamvas

Question: I want to check if SQL Server Agent is running using t-sql.    I know I can check through the services.msc or other methods such as Powershell scripts to report on the SQL Server Agent status , but the requirement is to extract this information via t-sql. Do you have a script using t-sql to get the SQL Server Agent Status?

 

Answer: If you check the SQL generic Refresher  program in the sysprocesses list , it is possible to establish the SQL Server Agent status through t-sql.  The SQL Generic Refresher  is an app program. When the SQL Server Agent starts a connection appears with the Program name SQL generic Refresher

 

IF EXISTS(
SELECT 1 FROM MASTER.dbo.sysprocesses WHERE program_name = N'SQLAgent - Generic Refresher')
BEGIN
SELECT SERVERPROPERTY('ServerName') AS 'SQL_Server_Instance', 'Running' AS 'SQL_ServerAgent_Status'
END
ELSE
BEGIN
SELECT SERVERPROPERTY('ServerName') AS 'SQL_Server_Instance', 'Stopped' AS 'SQL_ServerAgent_Status'
END

Read more on SQL Server Agent 

Disaster Recovery Powershell Script to check for failed Windows services

Powershell script - Get the SQL Server Instance status with Get-Service

SQL Server - Working with Windows Services Examples and Powershell


Author: Jack Vamvas (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to check if SQL Server Agent is running using t-sql


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer