Auto Starting SQL Server Agent

Comments 0

Share to social media

I faced this problem some years ago. The company I was working for had a lot of clients with very low-quality communications, so each client needed its own local server and BI solutions. A very small DBA team had the task to deal remotely with hundreds of SQL Servers across low-quality communication links and sometimes, during an emergency and after restarting SQL Server service, someone forgot to start SQL Server Agent again.

The problem used to be noticed when some manager or director in the client company perceived outdated BI dashboards, becoming a severe trouble. It was a human error and the company was looking for a way to avoid it.

When we stop SQL Server service, SQL Server Agent service also stops, but when we start SQL Server again, SQL Server Agent won’t start together and the existing configurations weren’t enough to solve this:

  • As a service, we can configure recovery actions for SQL Server Agent, but they are all for errors, this situation isn’t an error.
  • We can configure SQL Server Agent to start automatically if it stops unexpectedly, however this will not help either.

The solution: Configure an auto-start procedure in SQL Server to start SQL Server Agent service, this way making the service start every time SQL Server service starts.

This solution depends on enabling xp_cmdshell stored procedure. This is always seen as a risk for security, however, only sysadmins will have access to this procedure, so if you avoid the most the use of logins with sysadmin permissions and follow the best security practices with SQL Server service account, you may have no problem.

 

1) Enabling ‘xp_cmdshell’

— To allow advanced options to be changed. 
EXEC Sp_configure ‘show advanced options’, 1;
go
— To update the currently configured value for advanced options. 
RECONFIGURE;
go
— To enable the feature. 
EXEC Sp_configure ‘xp_cmdshell’, 1;
go
— To update the currently configured value for this feature. 
RECONFIGURE;
go 

2) Create the stored procedure to execute ‘xp_cmdshell’ to start SQL Server Agent

CREATE PROCEDURE Autoagent
AS
    EXEC Xp_cmdshell
      ‘NET START SQLSERVERAGENT’ 

3) Configure the stored procedure as an auto-start stored procedure. It will be executed every time SQL Server Service starts

EXEC Sp_procoption autoagent, startup, true 

Load comments

About the author

Dennes Torres

See Profile

Dennes Torres is a Data Platform MVP and Software Architect living in Malta who loves SQL Server and software development and has more than 20 years of experience. Dennes can improve Data Platform Architectures and transform data in knowledge. He moved to Malta after more than 10 years leading devSQL PASS Chapter in Rio de Janeiro and now is a member of the leadership team of MMDPUG PASS Chapter in Malta organizing meetings, events, and webcasts about SQL Server. He is an MCT, MCSE in Data Platforms and BI, with more titles in software development. You can get in touch on his blog https://dennestorres.com or at his work https://dtowersoftware.com