Recipe 2.19. Restarting or Shutting Down a ServerProblemYou want to restart or shut down a server. SolutionUsing a graphical user interfaceWith Windows Server 2003 and Windows 2000 you can obviously shut a machine down by going to Start Shut Down, but I'll describe the new graphical interface with the shutdown.exe command, which is available in Windows Server 2003.
Here is another option that is available from either Windows 2000 or Windows Server 2003:
Using a command-line interfaceThe following two commands work with the Windows Server 2003 version of shutdown.exe. This shuts a server down after the 30 seconds (default wait timer): > shutdown /m \\<ServerName> /s /c "Server requires reboot due to app install" This command restarts a server after 20 seconds: > shutdown /m \\<ServerName> /r /t 20 /c "Server is going down for repairs" You can use the /f option to force applications to close. On Windows 2000, the shutdown options are a little different. This command shuts down a server (in 30 seconds by default): > shutdown \\<ServerName> "Server is going down for repairs" And this restarts (/r option) a server in 15 seconds: > shutdown \\<ServerName> /r /t:15 "Server requires reboot due to app install" You can force all applications to close by using the /c option. Using VBScript' This code shuts a server down. ' ------ SCRIPT CONFIGURATION ------ strComputer = "<ServerName>" ' e.g., rallen-srv01 ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set colOS = objWMI.InstancesOf("Win32_OperatingSystem") for each objOS in colOS intRC = ObjOS.Shutdown( ) if intRC <> 0 then WScript.Echo "Error attempting to shutdown server: " & intRC else WScript.Echo "Shutting down server..." end if next ' This code forcefully shuts a server down. ' ------ SCRIPT CONFIGURATION ------ strComputer = "<ServerName>" ' e.g., rallen-srv01 intFlag = 1 + 4 ' Flag for forceful shut down ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set colOS = objWMI.InstancesOf("Win32_OperatingSystem") for each objOS in colOS intRC = ObjOS.Win32Shutdown(intFlag) if intRC <> 0 then WScript.Echo "Error attempting to shutdown server: " & intRC else WScript.Echo "Shutting down server..." end if next ' This code reboots a server. ' ------ SCRIPT CONFIGURATION ------ strComputer = "<ServerName>" ' e.g. rallen-srv01 ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set colOS = objWMI.InstancesOf("Win32_OperatingSystem") for each objOS in colOS intRC = ObjOS.Reboot( ) if intRC <> 0 then WScript.Echo "Error attempting to reboot server: " & intRC else WScript.Echo "Rebooting server..." end if next DiscussionWhen you shut down or restart a Windows server, a signal is sent to all devices, services, and programs running on the system. This signal announces that the system is preparing to shut down so everything needs to close gracefully, if possible. A normal shutdown will wait for a response from all devices and processes to make sure they have saved any files and can close before proceeding with the shutdown. If you've ever had Notepad or Word open with an unsaved file, you've seen the message asking if you want to save the file before the application closes. If you need to automate the shutdown or restart of a system, then politely asking each application to close can be problematic. If someone had an open unsaved file on the target system, the shutdown wouldn't proceed as expected. That's why Windows also supports forcefully shutting down a system. With the shutdown.exe command, you can specify an additional option (/f on Windows Server 2003 and /c on Windows 2000) to force all applications to close. If an application like Notepad is running, it automatically closes any unsaved files and your unsaved changes will be lost. On the programmatic side, the Win32_OperatingSystem class supports two shutdown methods. The first one (Shutdown), performs a normal shutdown. It will not forcefully close files. The second, however, Win32Shutdown, will. Table 2-4 contains all of the flags that can be set with Win32Shutdown.
So you can perform a forceful shutdown with a command line and from a script. How about from the GUI? You sure can. Just hit Ctrl+Alt+Del on your keyboard. Using the Tab key move to the Shut Down button. Hold the Ctrl key down and click Enter. Click OK to confirm the shutdown. One other thing that you may need to do at some point is schedule a server to reboot or shutdown at a specific time. This is sometimes needed if an entire network or data center needs to move or go down for maintenance. You could automate this job pretty quickly using a combination of the scripting solutions and the Task Scheduler, but there is another way using the command line. The at.exe command lets you schedule a task to run either locally or remotely at a specified time. This command causes a server to shut down at 20:00 (8 P.M.) tonight: > at \\<ServerName> 20:00 shutdown.exe /s /c "Datacenter move" This command causes the server to restart at 21:00 (9 P.M.) every Friday night: > at \\<ServerName> 21:00 /every:F shutdown.exe /r /c "Weekly restart" See AlsoMS KB 232399 (How to Enable Logon Screen Shutdown Button in Windows 2000 Server), MS KB 324268 (HOW TO: Troubleshoot Shutdown Problems in Windows Server 2003), MS KB 325343 (HOW TO: Perform an Emergency Shutdown in Windows Server 2003), MS KB 325376 (HOW TO: Enable Verbose Startup, Shutdown, Logon, and Logoff Status Messages in the Windows Server 2003 Family), MS KB 816569 (HOW TO: Make the Shutdown Button Unavailable in the Logon Dialog Box in Windows Server 2003), and MS KB 821287 (The Computer Does Not Automatically Shut Down When the Shutdown.exe Command Is Invoked) |