2

Is it possible to detect loss of power on a laptop/desktop without an UPS? Obviously a computer without an UPS will shutdown at least almost immediately after being unplugged/having its battery pulled. However, I thought maybe that the kernel could notice the loss of power before the capacitors/inductors drained completely and have a very short chance to do something.

I know if I have a properly configured UPS, powerd will send SIGPWR to init so an organized shutdown can happen. I'm interested in the case where I don't have an UPS attached, so obviously there would be at best a very short window in which to shutdown. All I'd really want is to write a single timestamp (or failing that, any string) to a logfile so I could know about the power failure.

I somehow doubt that SIGPWR will actually be helpful in this case, since I doubt the kernel is going to send it to every single process, it was just the first thing that came to mind. Perhaps there would be some control line or file I could poll on that the kernel would update? Or perhaps I'm just screwed, which to be honest is kind of what I expect.

3

The time window even for an external 680uFx400V capacitor battery is very low for an average 100W desktop PC - just a half a second or so. With internal PSU caps it's even less. What are you really trying to achieve? Even if you will able to write a timestamp to a log file, you still need sync() cycle to happen or to do manual sync() call, and if you're unlucky and your machine does busy work dumping other queued disk buffers - you're out.

If you're need to detect a power failure almost reliably, then I suggest you to touch a flag file at boot, remove it during normal shutdown. The existence of this file will tell you that machine was not shutdown cleanly (but it will not save you from other failures, mostly software ones like kernel panics).

3

Some machines have short run batteries designed for just shutdown (AT&T 3B series did, at least, and IIRC some IBM RS/6000-pSeries did too). In these machines, the SIGPWR signal was sent on power loss and was actually useable. Not so much these days.

  • Do you think that is because software these days is generally more resilient to sudden process termination? I don't understand how that idea would hold-up to scrutiny especially since some software performs writes in the gigabytes, which I don't think ever takes less than half a second. – Jonathan Neufeld Jan 6 '18 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.