How to trigger a command on Linux when disconnected from power

Hi

I wrote a guide explaining how to soft implement an equivalent to BusKill using a power supply event to trigger a command in dom0.

Feel free to ask if you have questions or Qubes OS specific implementation questions :slight_smile:

11 Likes

For udev rules you should add

ENV{POWER_SUPPLY_TYPE}=="Mains"

because there is alot of devices in SUBSYTEM=="power_supply" with envinriment POWER_SUPPLY_ONLINE, including usb

2 Likes

Thank you very much for bringing this, I fixed my guide :slight_smile:

I tested it on my laptop too, works fine with this extra bit added.

1 Like

On Qubes OS, this could easily be extended if you want to run a command only if a given wifi SSID is not found around (like, you disconnect the computer and you are not at home / office).

#!/bin/sh
if ! qvm-run -u user sys-net "nmcli device wifi list | grep MY-WIFI-SSID"
then
    systemctl poweroff
fi
1 Like

Implemented the original idea (udev rule for power disconnect), but it won’t work if you suspend.

(i.e. Arm while plugged in, close laptop lid, unplug. Doesn’t matter if you open it and resume, leave it, plug it back in, whatever; I can’t make it work.) I’m not familiar ehough with the power subsystem to find out right now, so this is just a PSA.

What do you arm and what don’t work?
There is udev service which runs when qube start and is active all the time and there is script to detect SSID disconnected which runs once.

My bad. I’m doing the udev rule for the power subsystem. If you pull the plug, it’s supposed to power off. I edited it to fix the ambiguity.

@solene, this may warrant a warning in your article, since the demonstrated use-case is as a killswitch, and it’s far from unlikely someone would close the lid before power is disconnected when taking a laptop.