detecting if a Windows shutdown command has been issued
Is it possible to detect if the shutdown command has been issued (through the start menu by the user, or any other program running on the computer or having remote access to the computer), then cancel it ?
Alternatively, is it possible to tweak the registry to force Windows to display a shutdown timer whenever the shutdown command gets issued, so that we get some to get it disabled?
Yes and (mostly) no.
You can detect a shutdown event. When Windows is about to shut down, it sends a WM_QueryEndSession
to all open applications. To detect this, you would have to have a script or program running in the background to detect and handle the event. However, there doesn't seem to be any way to cancel the shutdown, unless the shutdown was initiated from the local machine and user.
The script/app could then catch the shutdown even and ask you if you want to quit the script/app, in which you can then cancel. This will prevent the machine from shutting down, but it won't prevent other apps and possibly services from being terminated.
You don't even need a script for this, you could just open notepad and type in some text. Since you have unsaved data in Notepad, Windows will wait and ask if you want to continue the shutdown, giving you the opportunity to cancel. Again, this will only work if you initiated the shutdown locally.
There is a WMI Win32_ComputerShutdownEvent
event, which occurs when a computer gets shutdown via a remote command. However, it can only receive this event. When the event is received, the local computer will terminate the script when the event is delivered.
This is for Windows 7 (in corporate environment): If an admin triggered a remote shutdown (shutdown /m PCName) windows 7 should display a notice saying that the PC is getting instruction to shutdown / restart and give you the countdown.
The user sitting on the PC CAN cancel it, assuming that the user also have admin right on the machine by running:
shutdown /a
to abort any existing shutdown in progress.
But of course this point is moot if the admin put a timer for 1 second, or immediate shutdown.
Windows 8 - You get a notification at the System Tray (when the shutdown is issued) that windows is about to shutdown in xxx minutes, and shutdown will happen at xxx time/date. And I believe at 1 minute to shutdown, Windows 8 will display a full screen notification that you will be shutdown in 60 seconds, and you actually have to click on close to confirm the message.
Windows XP - I believe it will display the timer as well, but I can't confirm as I don't have WinXP machine to test.
Again, you can cancel all 'scheduled' shutdown by the command above, but if you run out of time, not much you can do.
I'm hoping to find a way to do this as well, in order to ensure postgreSQL has enough time to shutdown properly before it's terminated, but is not looking super hopeful at the moment.
This is basically just restating others' answers, but I tried these two commands from an admin prompt on Windows 7:
shutdown /s & shutdown /a
shutdown /s /t 0 & shutdown /a
The first command successfully aborted the shutdown as expected, while the second command didn't. This proves Keltari's statement about an immediate shutdown not being able to be aborted with shutdown /a
.