Monday, April 21, 2008

Someone recently asked how to shut down or restart Windows from their Ruby code. Windows provides an executable, "shutdown.exe", to accomplish this. Execute this command from your script with the appropriate parameters and you can log off, shutdown, or restart the OS.

Parameters accepted include, but are not limited to:

-l Log off (cannot be used with -m option)-s Shutdown the computer-r Shutdown and restart the computer-t xx Set timeout for shutdown to xx seconds-f Forces running applications to close without warning

So, the following line of code will close all applications without a warning and instantly shut down and restart your PC: