Despite the common belief, the practice of programming a system reboot or shutdown of a client or server machine isn't necessarily a bad thing: in some limited scenario planning a regular shutdown or reboot of the operating system could be a decent workaround to securely put the machine in a desired state - for example, to make it ready for a VM-based backup or for installing a planned update. Depending on the situation, it could even be acceptable to setup a recurrent period - once a week, for example - to issue a system reboot.
The ideal interval, needless to say, might vary a lot depending on the features performed by our server, its service availability expectactions and other variables in play: if the server performs its activities only during daytime can probably be restarted every night without major issue, while a web server that serves sites or services troughout the world should be restarted only in exceptional circumstances. It's also worth noting that you can often get the same benefits by recycling the Application Pools or other resource-heavy processes, as we'll see in a short while.
Setting up our machines to reboot at regular intervals could also be useful to ensure that the system will be able to get back on track in case of a planned reboot - such as those issued by system updates or due to unforeseen situations - service interruption, power failure, hardware failure and other more or less serious events that could always happen. Since the ability to recover after those unexpected events is the fundamental premise of any Disaster Recovery procedure, setting a reboot at regular intervals might also be a not-so-terrible way to periodically test our system against these kind of threats.
To set up an automatic system restart, open the Control Panel (if you do not know how to do on Windows 10, read here) and open the Task Scheduler in the following way, depending on your version of Windows:
- Windows 2000/XP, Windows Server 2003 and previous versions/builds: select the Task Scheduler (or Scheduled Tasks) icon.
- Windows Vista, Windows 7/8/8.1/10, Windows Server 2008 and newer versions: open the Administrative Tools folder, then select the Task Scheduler icon.
Once the Task Scheduler window is open, create a new task using the Create Task action in the following way:
- Run whether user is logged on or not, to ensure that the task will be executed even if there are no logged-in users.
- Run with highest privileges, to make sure that the task will be executed with administrator rights - this is required for issuing a system hutdown or a reboot.
For further info regarding the other options, check out this MS TechNet article.
Once done, move to the Trigger tab and click on the New button to create an execution trigger - which is, the event that will make the system run the task itself. A New Trigger modal window will then be opened, which you can use to setup such event. The most common trigger to setup here is a time-based trigger, which can be set by selecting the On a schedule option on the Begin the task dropdown list:
For further info regarding triggers, take a look at this MS TechNet page.
Once done, move to the Actions tab and click to the New button to setup the command to execute whenever the trigger fires.
Setup an automatic Shutdown (without reboot)
%SystemRoot%\system32\shutdown.exe -s -f -t 10
Setup an automatic Reboot
%SystemRoot%\system32\shutdown.exe -r -f -t 10
Here's a brief explanation of the parameters used above:
-s : setup the automatic shutdown of the system without a reboot. Alternative to the -r option (see below).
-r : setup the automatic reboot of the system: this option is required to ensure that your system will actually restart. Alternative to the -s option (see above).
-f : ensure that all the processes, services and user sessions will be forcefully closed before performing the shutdown or reboot: this is a required option to ensure the shutdown even there are active processes that cannot be terminated in a graceful way; be sure to only use it if you don't have active user sessions and/or running processes that performs critical tasks (such as backup jobs, anti-malware scans and the likes), as they will most likely be killed before completion.
-t : setup a timeout before executing the shutdown or reboot (in seconds): the default is 20 (20 seconds), but can be changed by adding a number right after it, separated by a space: for example, -t 50 for 50 seconds. This option is great to use because it will also show a warning popup to logged-in users (if there are any) when the shutdown/reboot time occurs, allowing them to close their tasks and log out.
For a detailed explanation of all the shutdown.exe options and parameters, you can take a look at this official MS TechNet doc.
As soon as you define the Action, your planned activity should be ready: take a few seconds to check all the settings once again, then click OK to create the task. If you asked to run the task with elevated privileges the system will ask you to enter the credentials of the Administrator account (or the account with administrative privileges you have chosen in the General tab): once done, the result of your work will appear, summarized in a single line, within the Task Scheduler main panel:
A good thing to do before closing everything is to run the task immediately to prove it works (right mouse button -> Run): alternatively, you can just wait for it to occur automatically at the time you have set and then check the Last Run Time / Last Run Result parameters to determine the outcome.
That's it for now: I hope that this small tutorial will help other System Administrator looking for a way to setup an automatic shutdown and/or reboot on Windows. In case you need to do the same on Linux machines, check up this post!