Contents
English (United Kingdom)French (Fr)Deutsch (DE-CH-AT)
Search
Login
Who Is Online?
We have 113 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: / 12
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.


 

Copy the two batch files in a folder of your harddrive. Change the files with your preferred texts and shutdown reason codes.

Running the file InstallShutdown installs the scheduled shutdown.

Running the file UnInstallShutdown removes the scheduled shutdown again.

 

Open InstallShutdown.cmd
 

InstallShutdown.cmd:

  1. @ECHO OFF
  2. REM
  3. REM Windows command line batch script to to install a scheduled shutdown.
  4. REM
  5. SETLOCAL
  6.  
  7. REM Set the text strings.
  8. SET MyTitle=Scheduled System Cleanup Restart
  9. SET MyDescription=This is the message.
  10.  
  11. REM Prepare the shutdown command line.
  12. REM Copy the following three lines into the uninstall script after they have been
  13. REM changed.
  14. SET MyComment=This is my comment.
  15. SET OurShutdown=SHUTDOWN -r -t 180 -d up:241:50000 -c "%MyComment%"
  16. SET OurValue=P;241;50000
  17.  
  18. REM Create the key, start the scheduler service if not running, and create
  19. REM the scheduled task.
  20. REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Reliability\UserDefined /v "%OurValue%" /t REG_MULTI_SZ /d "%MyTitle%\0%MyDescription%" /f
  21. NET START schedule
  22.  
  23. REM Remove the double quotation marks for the comparisons.
  24. SET OurShutdown=%OurShutdown:"=%
  25.  
  26. REM Check whether our task entry already exists.
  27. REM Find the lines in the AT output that contain OurShutdown
  28. AT>"%TEMP%\TheAtOutput.dat"
  29. SET Terminate=No
  30. FOR /F "eol=; tokens=* delims=," %%a IN ('TYPE "%TEMP%\TheAtOutput.dat"^|FINDSTR "%OurShutdown%"') DO CALL :Analyse %%a
  31. IF "%Terminate%."=="Yes." GOTO :KeyEnd
  32.  
  33. REM Add the entry.
  34. AT 09:00 /every:SUNDAY %OurShutDown%
  35. GOTO :KeyEnd
  36.  
  37. :Analyse
  38. REM Remove the quotation marks of the retrieved AT output lines.
  39. SET AtLine=%*
  40. SET AtLine=%AtLine:"=%
  41.  
  42. REM Take the first 4 characters and convert them into a number.
  43. SET AtID=%AtLine:~0,4%
  44. SET /A AtID=%AtID% + 0
  45. IF "%AtID%."=="0." GOTO :EOF
  46. SET AtID=%AtID: =%
  47.  
  48. REM Taks already exists. Exit.
  49. ECHO The task already exists with ID %AtID%.
  50. SET Terminate=Yes
  51. GOTO :EOF
  52.  
  53. :KeyEnd
  54. REM Clean up.
  55. DEL /F/Q "%TEMP%\TheAtOutput.dat"
  56.  
  57. ECHO.
  58. ECHO Finished.
  59.  
  60. REM Check for some common 'Quiet' switches.
  61. IF /i "%1."=="/Q." GOTO :End
  62. IF /i "%1."=="/SILENT." GOTO :End
  63.  
  64. ECHO Press any key to continue.
  65. PAUSE>NUL
  66.  
  67. :End
  68. ENDLOCAL
  69.  
Open InstallShutdown.cmd

 

Open UninstallShutdown.cmd
 

UninstallShutdown.cmd:

  1. @ECHO OFF
  2. REM
  3. REM Windows command line batch script to remove the scheduled shutdown again.
  4. REM
  5. SETLOCAL
  6.  
  7. REM Prepare the shutdown command line.
  8. REM Copy the following three lines from the installation script when changed.
  9. SET MyComment=This is my comment.
  10. SET OurShutdown=SHUTDOWN -r -t 180 -d up:241:50000 -c "%MyComment%"
  11. SET OurValue=P;241;50000
  12.  
  13. REM Remove the double quotation marks for the comparisons.
  14. SET OurShutdown=%OurShutdown:"=%
  15. AT>"%TEMP%\TheAtOutput.dat"
  16.  
  17. REM Find the lines in the AT output that contain OurShutdown
  18. FOR /F "eol=; tokens=* delims=," %%a IN ('TYPE "%TEMP%\TheAtOutput.dat"^|FINDSTR "%OurShutdown%"') DO CALL :Analyse %%a
  19.  
  20. REM Remove the reason text from the registry.
  21. REG DELETE HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Reliability\UserDefined /v "%OurValue%" /f
  22.  
  23. GOTO :KeyEnd
  24.  
  25. :Analyse
  26. REM Remove the quotation marks of the retrieved AT output lines.
  27. SET AtLine=%*
  28. SET AtLine=%AtLine:"=%
  29.  
  30. REM Take the first 4 characters and convert them into a number.
  31. SET AtID=%AtLine:~0,4%
  32. SET /A AtID=%AtID% + 0
  33. IF "%AtID%."=="0." GOTO :EOF
  34. SET AtID=%AtID: =%
  35.  
  36. REM Remove the task from the scheduler.
  37. AT %AtID% /DELETE
  38. GOTO :EOF
  39.  
  40. :KeyEnd
  41. REM Clean up.
  42. DEL /F/Q "%TEMP%\TheAtOutput.dat"
  43.  
  44. ECHO.
  45. ECHO Finished.
  46.  
  47. REM Check for some common 'Quiet' switches.
  48. IF /i "%1."=="/Q." GOTO :End
  49. IF /i "%1."=="/SILENT." GOTO :End
  50.  
  51. ECHO Press any key to continue.
  52. PAUSE>NUL
  53.  
  54. :End
  55. ENDLOCAL
  56.  
Open UninstallShutdown.cmd

 

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)