Restart SQL Server Periodically using Task Scheduler

Kailash Chandra Behera | Monday, December 20, 2021

Introduction

Here in this blog, we are going to discuss, how to use the windows task scheduler to restart the Microsoft SQL Server periodically in a specific time.

start and stop sql server
Windows Task Scheduler for SQL Server

Getting Started

Recently one day one of my projects database application consumed 80 percent of memory and the application which uses this data base runs very slow.

The number of transactions of the database is as usual as normal days, no extra number of transactions was identified. But suddenly the memory consumption by SQL Server increased.

My team member and I verified everything in the server but did not get success to increase the performance of application and decrease the memory consumption.

Then we thought to stop SQL server and start SQL Server, after stop and start, the memory consumption by SQL server is decreased. Later we come to know that the SQL server is not restarted from longer time and due to SQL Server cache, the memory consumption was increased.

We decided to restart SQL server everyday periodically. To do start and stop SQL server manually is very difficult in our case, so we though to create a crone job to stop SQL server then start SQL server.

The windows also provide same service to achieve our goal that is windows task scheduler, so we decided to use the task scheduler.

Windows Task Scheduler

The Task Scheduler is a crone job which enables you to automatically perform routine tasks on a chosen computer. Task Scheduler does this by monitoring whatever criteria you choose (referred to as triggers) and then executing the tasks when those criteria are met.

You can use the Task Scheduler to execute tasks such as starting an application, sending an email message, or showing a message box. Tasks can be scheduled to execute in response to these events, or triggers.

Steps to use Task Scheduler

The following steps describes how to use the Windows Task Scheduler to stop SQL server and start SQL server. Here two different task scheduler are created to start and stop SQL server.

  1. First get the service name of SQL server, to get the name of SQL server follow the below steps.

    1. Open the Windows Services Management Console by typing services.msc in the run

    2. If you are windows10 then type services in search.

    3. Locate the SQL Service and open its properties.

    4. Take note of the Service name for future use: here my database service name is MSSQL$SQLEXPRESS

    SQL Service

  2. Create a task scheduler to stop the SQL Server first, follow the below stpes

    1. Open the Task Scheduler by typing Task in the Windows search box.

    2. Select Create a Basic Task from the Actions list on the right.

    3. Name the task Stop SQL Server and write details in the description (Optional)

    4. Click Next and select Daily Trigger

    5. Pick a time where there will be no activity

    6. Click Next and for the Action select Start a program

    7. For the Program/Script type NET and add the arguments Stop "MSSQL$SQLEXPRESS"

    8. Before Finishing, select the Open the Properties dialog option

    9. In the General tab select Run whether user is logged in or not and Run with highest privileges

    task scheduler
    stop sql server

Note:- You can click on the Entable All Tasks History to monitor your scheduler activity

Thanks