Search code examples
sqlsql-serverservicesql-agent

SQL Server stored procedure or alternative method to restart SQL Server Agent


There are various ways to restart the SQL Server Agent on a server, but I would like to do it from a stored procedure in one of my databases (on the same server). How would one go about doing that? Is there some sort of a system stored procedure that I could call? Or would I need to call some sort of third party library/external language to accomplish that such as the following?


Solution

  • Apparently one of my co-workers had already solved this:

    EXEC xp_servicecontrol N'STOP',N'SQLServerAGENT';
    
    -- Give the service a little time to stop
    waitfor delay '00:00:10.000'
    
    EXEC xp_servicecontrol N'START',N'SQLServerAGENT';
    

    Not sure this the best solution, but should work for my purposes...