08 October,2020 by Tom Collins
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
This is only a preview. Your comment has not yet been posted.
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.
Posted by: |