Contents
English (United Kingdom)French (Fr)Deutsch (DE-CH-AT)
Search
Login
Who Is Online?
We have 85 guests online
Navigation
Home Source Code Command line scripts Scheduled Windows restart
Most Recent
Featured Articles
Joomla 1.5 Featured Articles
Navigation
Home Source Code Command line scripts Scheduled Windows restart
English (United Kingdom)French (Fr)Deutsch (DE-CH-AT)
Scheduled Windows restart E-mail
User Rating: / 10
PoorBest 
Source Code - Command line scripts
Written by Thomas   
Thursday, 20 August 2009 17:15
Article Index
Scheduled Windows restart
Deinstallation
All Pages

 

In general, Windows is a relatively stable operating system. Applications on the other hand are not always so reliable, and even some Microsoft applications are pretty good examples for poorly developed software. Sometimes a long running computer should be restarted to clear up resources consumed by leaky and crashy software.

We show you how to implement a scheduled restart in a professional way.


A restart can be initiated with the console utility Shutdown, which is shipped with Windows.

The example from the Microsoft page shuts the computer down in 60 seconds, forces running applications to close, restarts after the shutdown, indicates a user code, marks the shutdown as planned, and uses some reason codes:

shutdown -r -f -m \\MyServer -t 60 -d up:125:1

The description of this shutdown command seems quite suitable if it wasn't for the reason codes. The remarks section contains:

"If you indicate a major and minor reason code, you must first define these reason codes on each computer for which you plan to use the particular reason. If the reason codes are not defined on the target computer, Event Viewer cannot log the correct reason text."

What Shutdown's documentation doesn't tell us is how to define these reason codes. This informaton can be found in the Microsoft Developer Network (MSDN): http://msdn.microsoft.com/en-us/library/aa376885%28VS.85%29.aspx

Excerpt from this page:
"Custom reasons are sorted in the user interface by major reason number, then by minor reason number. No two custom reasons can use the same major and minor reasons, unless one is planned and the other is unplanned. Otherwise, the system will use the first instance and ignore the others."

This means that we should have a unique number and text. We won't use the numbers from the Microsoft example, because the likelyhood that someone else may have the same idea is higher for these codes. Let's go for the major reason code 241 and 50000 for the minor. These numbers sound random enough for the purpose. Since the restart should happen scheduled, for example once a week, a 'P' for a planned shutdown will do. We don't want the dialog box in which the user has to enter some notes to pop up after the restart. The solution is supposed to be fully automated.

To write the value to the registry, the command line tool Reg can be used.

The MS knowledge base article http://support.microsoft.com/kb/293814 explains where we have to start tweaking.

To get the reason P;241;5000 in the registry and the system log we can type this command in a command line window or in a batch script file:

REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Reliability\UserDefined /v "P;241;50000" /t REG_MULTI_SZ /d "Scheduled System Cleanup Restart\0This is the message." /f


Once the custom reasons have been entered into the registry, we modify the shutdown command accordingly:

SHUTDOWN -r -t 180 -d up:241:50000 -c "This is my comment."

Windows still doesn't know when to initiate our shutdown. We're going to use the Task Scheduler for this. It is important that this service is up and running, which is the case on most systems anyway:

NET START schedule

 

The command line utility At lets us schedule tasks.

This line performs the restart on every Sunday at 9:00 in the morning:

AT 09:00 /every:SUNDAY SHUTDOWN -r -t 180 -d up:241:50000 -c "This is my comment."

 

Putting all these commands together in one command line script gives us a pretty good installer for the scheduled shutdown:


REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Reliability\UserDefined /v "P;241;50000" /t REG_MULTI_SZ /d "Scheduled System Cleanup Restart\0This is the message." /f
NET START schedule
AT 09:00 /every:SUNDAY SHUTDOWN -r -t 180 -d up:241:50000 -c "This is my comment."
PAUSE

For the deinstallation we will later need the task ID the AT command has created for our scheduled entry. We get this ID directly when AT has finished processing the task creation. Storing this ID somewhere for later use when the scheduled shutdown should be removed again would be one way of remembering it, but I do want to keep it simpler than that.

Instead of storing the ID somewhere for the task removal, the AT command can output a list of scheduled tasks. All we have to do is find our entry and remove it again. This means that we remember our SHUTDOWN parameters instead of the AT ID.

SETLOCAL
SET OurShutdown=SHUTDOWN -r -t 180 -d up:241:50000 -c "This is my comment."
REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Reliability\UserDefined /v "P;241;50000" /t REG_MULTI_SZ /d "Scheduled System Cleanup Restart\0This is the message." /f
NET START schedule
AT 09:00 /every:SUNDAY %OurShutDown%
ENDLOCAL
PAUSE

Copying the line with the SET gives us access to our shutdown command to find it in the task scheduler again.



Last Updated on Saturday, 22 August 2009 17:42
 
You need to login or register to post comments.
Discuss this item on the forums. (0 posts)
Discuss (0 posts)