Friday 14 October 2016

Periodic Restart Production Server using sql script

Suppose I want to restart my server on every Sunday mid night. We can use below sql script and schedule it on sql server agent for mid night.
USE master
go
 -- On Sunday Executes
IF (DATEPART(DW,GETDATE()) = 1)
BEGIN
EXEC Master.dbo.Sp_Configure 'Show Advanced Options', 1
RECONFIGURE
EXEC Master.dbo.Sp_Configure 'XP_CmdShell', 1
RECONFIGURE

EXEC Master.dbo.xp_cmdshell 'C:\WINDOWS\system32\shutdown.exe /r'

EXEC Master.dbo.Sp_Configure 'Show Advanced Options', 1
RECONFIGURE
EXEC Master.dbo.Sp_Configure 'XP_CmdShell', 0
RECONFIGURE
END
With the help of DATEPART Function we are finding the day.
DATEPART (dw,@Date) will return the numeric value.
Day
Numeric value
Sunday
1
Monday
2
Tuesday
3
Wednesday
4
Thursday
5
Friday
6
Saturday
7
Now I am running this script. We will get below
  
After 1 min server will be restart

  

No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts