How can I schedule a server restart from command line on Windows 2012?
Solution 1:
The shutdown
command itself has a delay parameter /t
that delays a shutdown for a number of seconds up to 10 years. If you want to schedule a shutdown in 14 hours, say, you might run
shutdown /r /t 50400
You might also add a reason with the /d
parameter or a comment with /c
; run shutdown /?
for details.
Solution 2:
Despite the documentation, the /SD
parameter seems to be compatible with the /SC ONCE
. The task is successfully created to run on the date provided, at the time provided. (Tested on W8 and W7)
Additionally, the XP documentation for schtasks.exe
goes so far as to say the /SD
parameter is required when using /SC ONCE
, so I imagine there are a fair number of scripts using the combination.
Example:
C:\Users\Mitch>schtasks /create /sc once /tn restart /tr "shutdown -r -f ""restart""" /st 23:00 /sd 2013-06-13
SUCCESS: The scheduled task "restart" has successfully been created.
C:\Users\Mitch>
If going against the documentation does not sit well with you, consider generating the XML file directly (schema is here), which is definitely supported and definitely supports a task scheduled to run once on a future date. An easy way to get the proper file is to create it in the Task Scheduler mmc
snap in and use the export command.
Example:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2013-06-12T21:20:51.004181</Date>
<Author>FOOBAR\Mitch</Author>
</RegistrationInfo>
<Triggers>
<TimeTrigger>
<StartBoundary>2013-06-13T22:20:29</StartBoundary>
<Enabled>true</Enabled>
</TimeTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>FOOBAR\Mitch</UserId>
<LogonType>InteractiveToken</LogonType>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>P3D</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>Your command here</Command>
</Exec>
</Actions>
</Task>
Command to import:
schtasks /CREATE /TN "Task Name" /XML filename.xml
Solution 3:
I ended up creating runat.ps1
to replicate some of the simple features of at
, incorporating the wisdom from Mitch's answer.
The syntax is runat (time) (command)
, e.g.
runat 2am shutdown -r -f -c "restarting the server"
runat 10:15 msg * it`'s 10:15 everyone!
If you, too, are nostalgic for the good old days when you didn't have to type...
schtasks /create /ru system /rl highest /sc once /tn restart /tr shutdown -r -f -c \"restarting the server\" /st 02:00 /sd 06/15/2013
... then you can install it by running this powershell command:
iex (new-object net.webclient).downloadstring('https://gist.github.com/lukesampson/5779205/raw/install.ps1');install runat https://gist.github.com/lukesampson/5778559/raw
Or you can manually download runat.ps1
script here.