O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Rebooting a Machine

Occasionally, it’s necessary to force a Windows NT computer to reboot programmatically. You may need to perform a scheduled reboot of the current machine or force a reboot of a remote PC programmatically.

The function win32api.InitiateSystemShutdown() appears perfect for the job.

win32api.InitiateSystemShutdown(machine, message, timeout, bForce, bReboot)
machine

The name of the machine to shutdown or None for the current machine.

message

A message to be displayed to the user in a dialog while the timeout period expires.

timeout

A timeout in seconds, during which time a dialog is displayed warning the user of the pending shutdown. After the timeout expires, the shutdown process begins. If this is zero, the shutdown commences immediately.

bForce

Specifies whether applications with unsaved changes are to be forcibly closed. If this parameter is true, such applications are closed. If this parameter is false, a dialog box is displayed prompting the user to close the applications. Note that this implies the user could cancel the shutdown process by selecting Cancel in the dialog his application displays for unsaved data.

bReboot

Specifies whether the machine is rebooted after the shutdown process.

Let’s try this function. Start by rebooting the current machine with a 30-second timeout (without forcing applications shut) and finally restart after shutdown:

>>> import win32api >>> message = "This machine is being rebooted because it has been naughty" >>> win32api.InitiateSystemShutdown(None, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required