Recipe2.19.Restarting or Shutting Down a Server


Recipe 2.19. Restarting or Shutting Down a Server

Problem

You want to restart or shut down a server.

Solution

Using a graphical user interface

With 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.

  1. From the Start menu select Run.

  2. Type cmd.exe and click OK.

  3. In the CMD window, type shutdown /i and hit enter.

  4. Click the Add button, type the names of the server(s) you want to shut down or restart, and click OK.

  5. Select whether you want to restart or shut down the server(s).

  6. Enter the number of seconds to warn logged on users (or uncheck to not warn).

  7. Configure the Shutdown Event Tracker options.

  8. Click OK. In the CMD window, you'll see a status message stating if the operation was successful.

Here is another option that is available from either Windows 2000 or Windows Server 2003:

  1. Open the Computer Management snap-in.

  2. If you want to target a remote system, right-click on the Computer Management icon in the left pane and select Connect to another computer. Enter the computer name beside Another computer and click OK.

  3. Right-click on the Computer Management icon in the left pane and select Properties.

  4. Click the Advanced tab.

  5. Under Startup and recovery, click the Settings button.

  6. Click the Shut Down button.

  7. Select from the list of actions and options under Forced Closed Apps.

  8. Click OK.

Using a command-line interface

The 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

Discussion

When 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.

Table 2-4. Bitmapped Win32Shutdown flags

Value

Meaning

0

Log off

0 + 4 = 4

Forcefully log off

1

Shutdown

1 + 4 = 5

Forcefully shutdown

2

Restart

2 + 4 = 6

Forcefully restart

8

Power off

8 + 4 = 12

Forcefully power off


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 Also

MS 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)



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net