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.
Windows Task Scheduler for SQL Server |
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.
Restart SQL Server Periodically using Task Scheduler
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.
- First get the service name of SQL server, to get the name of SQL server follow the below steps.
- Open the Windows Services Management Console by typing services.msc in the run
- If you are windows10 then type services in search.
- Locate the SQL Service and open its properties.
- Take note of the Service name for future use: here my database service name is MSSQL$SQLEXPRESS
- Create a task scheduler to stop the SQL Server first, follow the below stpes
- Open the Task Scheduler by typing Task in the Windows search box.
- Select Create a Basic Task from the Actions list on the right.
- Name the task Stop SQL Server and write details in the description (Optional)
- Click Next and select Daily Trigger
- Pick a time where there will be no activity
- Click Next and for the Action select Start a program
- Type NET in the Program/Script field and in add the arguments type Stop "MSSQL$SQLEXPRESS" as shown in the image
- Before Finishing, select the Open the Properties dialog option
- In the General tab select Run whether user is logged in or not and Run with highest privileges
- Create another task scheduler to start the SQL Service again.
- To create another task scheduler follow all the steps point number 2
- Mention the scheduler name as Start SQL Server
- Make sure that there is mininum 5 minutes delay of running time between stop scheduler and start scheduler
- In the argument field type Start "MSSQL$SQLEXPRESS"
Note:- You can click on the Entable All Tasks History to monitor your scheduler activity
Thanks
Really helpful content, keep it up
ReplyDeleteexcellent article
ReplyDeleteAwasome keep it up
ReplyDeleteThank, it helped me
ReplyDelete