Search code examples
crestartshutdown

How to check for computer power loss?


Hopefully this is a simple question to answer.

Basically I would like to use C to see if there was a power loss in my computer. This will decide how a program runs. If there was a power loss then it would go one way. Otherwise it would respond another way:

#include nopower.h
#include power.h 
//------------------------
if(!powerloss){
  power_procedure();
}
else no_power_procedure(); 
//--------------------------

I'm running Ubuntu 12.04 LTS. I'm hoping that this can be run directly on the computer running this code. In otherwords is there a way to check a registry status to see if power was loss. The operating system knows when there is an improper shutdown, and I'd like to know if I can tap into the same or a similar resource. I'd rather not constantly write to a file.


Solution

  • int main() 
    {    
       printf ("power is currently on");
    }
    

    Coding the "power is currently off" case is somewhat trickier.

    Alternately if you want to know the time since the last boot, and put up a message if it was recent, then see Uptime under linux in C