How do I automate SQL server restart daily?

Posted on 2007-12-04
Last Modified: 2008-02-01

How can I create a job to have SQL Server restart at a particular point in time daily?  Thx.
Question by:skip1000
LVL 17

Expert Comment

by:Chris Mangus
ID: 20407472
It's pretty risky to automate restarting SQL Server since you never know what is going on in your database at the time.

What is the reason for a scheduled restart?  Perhaps we can look at that and see why it's needed.
LVL 17

Accepted Solution

Chris Mangus earned 200 total points
ID: 20407486
Most SQL MVP's will strongly advise against doing an automated restart, but, here is some code that will do it.

Again, it's a pretty risky idea...
Code Snippet--recycle sqlserver
declare @s sysname, @cmd nvarchar(1000)
set @s=case when serverproperty('instancename') is not null then N'mssql$'+cast(serverproperty('instancename') as nvarchar(15)) else N'MSSQLSERVER' end
set @cmd=N'net stop '+@s+' /y && net start '+@s+' /y'
exec master..xp_cmdshell @cmd

Open in new window


Author Comment

ID: 20407606
it is a test environment and the space is limited.  the tempdb grows to 22gb after a few days and server runs out of room.  i'm not sure which process causes tempdb to grow too big, but i need to keep server available w/ resources which is why i'm looking at auto restarting sql server.
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

LVL 17

Expert Comment

by:Chris Mangus
ID: 20407624
I can see why you'd want to do the restart.  You may need to encapsulate this code in some others that will check for running transactions and user connections to make sure you don't restart it while someone is in the middle of something.

Author Comment

ID: 20407689
true.  since this a dev server w/ few users, not many ppl will be impacted I think.  just need to do this until i can figure out which process is causing the tempdb to grow to 22gb and how quickly it grows to that size.
LVL 17

Expert Comment

by:Chris Mangus
ID: 20407711
Well, put the code in a job and let 'er rip!
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 50 total points
ID: 20409184
you cannot get a sql job to start sql server, because once it stopped, the job will be aborted automatically, as the sql agent is also stopped, resp looses connection...

so, you would need a windows scheduled job to do that, simply.
however, I agree with above that you should not need to do that. if you have the tempdb filling up, there is either
* a job that does not properly commit
* a job that does excessive temp table creation during run... if that is the case, stopping+starting the server will not help, except for 1 day, and the same process that filled up the tempdb will run again, and blow up the tempdb.
  -> result, you will make those operations slower, as the tempdb file has to grow for it...

so: solve the original problem, and you solved 2-3 problems at once.
LVL 17

Expert Comment

by:Chris Mangus
ID: 20411704
angellll, the code I posted will stop and restart SQL Server...give it a try...
LVL 18

Expert Comment

ID: 20413854
"I can see why you'd want to do the restart.  " ?   If tempdb growth is the problem firstly you should try and identify why as angel says.    However in the interim, why not simply schedule a tempdb shrinkrather than a restart?
LVL 17

Expert Comment

by:Chris Mangus
ID: 20414385
You can't just run DBCC SHRINKFILE or SHRINKDATABASE on tempdb.  If ANY process is using tempdb then the shrink will fail.  See:

Unfortunately, while skip1000 is troubleshooting why tempdb is growing so large he will without a doubt have to restart his SQL Server.

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle DB monitor SW 21 48
sql server insert 12 30
Query group by data in SQL Server - cursor? 3 31
How to use three values with DATEDIFF 3 20
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question