The headline is a bit misleading. I thought it meant it was about triggering a command on a computer that has no power, which is obviously impossible.

What it’s actually about how to automatically automatically trigger a command on a Linux computer (almost certainly a laptop) in the event that it switches from AC power to battery power.

The example given is your laptop is plugged it at a café. Someone steals it, which involves unplugging it from the wall. At the moment it is unplugged it automatically shuts down, locks itself, etc.

I wonder if the same approach will also work for a computer connected to a UPS. Probably not!

Maybe you know this, but UPSs have signaling software to tell whomever it may concern if it has lost power and is running on battery.

For big servers, the UPS's batteries might only give minutes of power, so they'd listen for such a signal to do an orderly shutdown.

Thanks for reporting, I updated the title, I hope it will be clearer for new readers.

For the APC, use nut :)

> I wonder if the same approach will also work for a computer connected to a UPS. Probably not!

Considering that my UPS appears as a battery in KDE when I plug the USB cable in, I wouldn't be surprised if it's treated the same as a laptop battery across most of the stack

Computers connected a UPS can use tools like NUT or more specific tools like apcupsd. It's much more common to need to know when a UPS powered device switches to battery to trigger stuff than it is to need to know when a laptop switches to battery, so there's a lot more tooling in that area.

Was the title changed since this comment? It now says “How to trigger a command on Linux when power switches from AC to battery”, which seems perfectly clear. I’m guessing “from AC to battery” was not present initially?

Yeah, it was when disconnected from power at first

"on power disconnection" (event) probably would've worked better than "when disconnected from power" (time period).

+1 to this.

This is still a logical use case, but it's not the mental image that immediately got conjured up after reading the title for me.

+1?

It's a way of signaling agreement.

They were correcting my initial "1+"

Shouldn't we programmers be just typing "++"?

i++

Yes, thank you. Fatfingered

With that example, why not just remove the battery entirely?

As for a UPS, I have a Synology NAS that can monitor an APC UPS over its serial USB port. When the UPS loses power or is told to gracefully shutdown, the NAS also gracefully shuts down.

>. Someone steals it, which involves unplugging it from the wall. At the moment it is unplugged it automatically shuts down

You could achieve same thing if you unplug your battery.

That's exactly how I understood the headline.