Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when Windows rebooted after crash or powerfailure

Do anyone know if its possible to detect if the last boot up was preceded by a power-failure in Windows ?

My reason for wanting to detect this is to know when to re-scan files I frequently modify on disk with my service.

If there is a normal startup, I can be quite sure that the shutdown went smooth and the data that was in in-memory file buffers was flushed to disk.

like image 469
ROAR Avatar asked Mar 02 '11 15:03

ROAR


People also ask

How do I check Windows reboot history?

View Shutdown and Restart Log from Event ViewerOpen Event Viewer (press Win + R[Run] and type eventvwr). In the left pane, open “Windows Logs >> System.” In the middle pane, you will get a list of events that occurred while Windows was running. You can sort the event log with the Event ID.

How do I check power failure in Event Viewer?

Type the following command to view the event logs and press Enter: wevtutil qe System /q:"*[System[(EventID=41) or (EventID=1074) or (EventID=6006) or (EventID=6005) or (EventID=6008)]]" /c:100 /f:text /rd:true.


2 Answers

Detecting powerfailure will only cover one possible reason for abnormal termination. The safe way of doing this is to create a marker file when you start, then remove it when you cleanly shutdown. If the marker file exists on next startup you know that your service was not cleanly terminated.

If you want to look for powerloss only, reading the event log will tell you whether the last shutdown was unexpected.

like image 126
Erik Avatar answered Oct 01 '22 08:10

Erik


Why not to check if your process terminated gracefully instead? Place some special marker in registry or in file system, that will mean, that your process is still working and delete it on gracefull shutdown. Then check it at every startup.

like image 40
Vladislav Rastrusny Avatar answered Oct 01 '22 09:10

Vladislav Rastrusny