How to restart your windows server with SQL Server

Sometimes we need to restart a windows PC or server within or after an interval of time. We can do it easily using ‘scheduled task’ of windows. Here I‘ll discuss how to do this task from MS SQL SERVER. To do this we’ll use a stored procedure ‘xp_cmdshell’ and an ms-dos command ‘shutdown’.
“xp_cmdshell” is a stored procedure which allows us to execute ms-dos commands from SQL Server.
Syntax
xp_cmdshell {‘command_string’} [, no_output]

Arguments

command_string: command_string for operating system command (ms-dos command). The maximum length of this parameter is varchar(8000) or nvarchar(4000).
No_output: No_output Is an optional parameter. After executing the given command_string with No_output it does not return any output to the client.

Execute following script to enable xp_cmdshell:
use master
EXEC sp_configure ‘show advanced options’, 1
RECONFIGURE
EXEC sp_configure ‘xp_cmdshell’, 1
RECONFIGURE
Now we will use ‘shutdown -r -f -t 01’ as a command_string.
Here,
Shutdown is a dos-command which is used to shut down, restart, log off or hibernate a computer.
 -r for restart, -f Forces running applications to close without warning, -t 01 for restart within 1 second.
Finally we will execute following sql procedure.
EXEC master..xp_cmdshell ‘shutdown -r -f -t 01’
If it is required to restart a PC/server every day then we can use MS SQL SERVER JOB.

Tuning sysctl.conf

Dành cho con giời nào thích phá. Mở terminal, gõ sudo nano /etc/sysctl.conf enter, điền password xong paste cái này vào:

kern.sysv.shmmax=536870912
kern.sysv.shmall=131072
kern.sysv.shmmin=1

kern.sysv.shmmni=12288
kern.sysv.shmseg=12192
kern.sysv.semmns=99541
kern.sysv.semmni=87381
kern.sysv.semmsl=99541

kern.maxproc=2048
kern.maxprocperuid=1024
kern.ipc.somaxconn=8192
kern.ipc.maxsockbuf=4194304
kern.ipc.somaxconn=32768
net.inet.tcp.rfc1323=1
net.inet.tcp.win_scale_factor=3
net.inet.tcp.sockthreshold=16
net.inet.tcp.sendspace=250000
net.inet.tcp.recvspace=250000
net.inet.tcp.mssdflt=1440
net.inet.tcp.msl=10000
net.inet.tcp.always_keepalive=1
net.inet.tcp.keepidle=120000
net.inet.tcp.keepinit=1500
net.inet.tcp.keepintvl=1500
net.inet.tcp.delayed_ack=0
net.inet.tcp.slowstart_flightsize: 4
net.inet.tcp.blackhole=2
net.inet.udp.blackhole=1
net.inet.icmp.icmplim=50

net.local.stream.sendspace=163840
net.local.stream.recvspace=163840
net.local.dgram.recvspace=4096
net.inet.tcp.slowstart_flightsize=89
net.inet.tcp.local_slowstart_flightsize=10
net.inet.tcp.mssdflt=1460
net.inet.tcp.delayed_ack=3
net.inet.tcp.delayed_ack=0
net.inet.tcp.delayed_ack=3

xong bấm Ctrl+X, bấm Y, Enter, gõ reboot, enter và cảm nhận nhé.

Database size

use master

declare @PageSize varchar(10)
select @PageSize=v.low/1024.0
from master..spt_values v
where v.number=1 and v.type=’E’

select name as DatabaseName, convert(float,null) as Size
into #tem
From sysdatabases where dbid>4

declare @SQL varchar (8000)
set @SQL=”

while exists (select * from #tem where size is null)
begin
select @SQL=’update #tem set size=(select round(sum(size)*’+@PageSize+’/1024,0) From ‘+quotename(databasename)+’.dbo.sysfiles) where databasename=”’+databasename+””
from #tem
where size is null
exec (@SQL)
end

select * from #tem order by DatabaseName
drop table #tem