https://dataswamp.org/~solene/2025-05-31-linux-killswitch-on-power-disconnect.html * Index * Everything * RSS * RSS using HTML About me: My name is Solene Rapenne, pronouns she/her. I like learning and sharing knowledge. Hobbies: '(Qubes OS BSD OpenBSD Lisp cmdline gaming security QubesOS internet-stuff). I love percent and lambda characters. Qubes OS core team member, former OpenBSD developer solene@. No AI is involved in this blog. Contact me: solene at dataswamp dot org or @solene@bsd.network (mastodon). I'm a freelance OpenBSD, FreeBSD, Linux and Qubes OS consultant, this includes DevOps, DevSecOps, technical writing or documentation work. How to trigger a command on Linux when disconnected from power Written by Solene, on 31 May 2025. Tags: #security #linux Comments on Fediverse/Mastodon Table of contents * 1. Introduction * 2. Setup * 3. Testing * 4. Script ideas * 5. Conclusion 1. Introduction SS After thinking about BusKill product that triggers a command once the USB cord disconnects, I have been thinking at a simple alternative. BusKill official project website When using a laptop connected to power most of the time, you may want it to power off once it gets disconnected, this can be really useful if you use it in a public area like a bar or a train. The idea is to protect the laptop if it gets stolen while in use and unlocked. Here is how to proceed on Linux, using a trigger on an udev rule looking for a change in the power_supply subsystem. For OpenBSD users, it is possible to use apmd as I explained in this article: => Rarely known OpenBSD features: apmd daemon hooks In the example, the script will just power off the machine, it is up to you to do whatever you want like destroy the LUKS master key or trigger the coffee machine :D 2. Setup SS Create a file /etc/udev/rules.d/disconnect.rules, you can name it how you want as long as it ends with .rules: SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", ENV{POWER_SUPPLY_TYPE}=="Mains", RUN+="/usr/local/bin/power_supply_off" Create a file /usr/local/bin/power_supply_off that will be executed when you unplug the laptop: #!/bin/sh echo "Going off because power supply got disconnected" | systemd-cat systemctl poweroff This simple script will add an entry in journald before triggering the system shutdown. Mark this script executable with: chmod +x /usr/local/bin/power_supply_off Reload udev rules using the following commands: udevadm control --reload-rules udevadm trigger 3. Testing SS If you unplug your laptop power, it should power off, you should find an entry in the logs. If nothing happens, looks at systemd logs to see if something is wrong in udev, like a syntax error in the file you created or an incorrect path for the script. 4. Script ideas SS Depending on your needs, here is a list of actions the script could do, from gentle to hardcore: * Lock user sessions * Hibernate * Proper shutdown * Instant power off (through sysrq) * Destroy LUKS master key to make LUKS volume unrecoverable + Instant power off 5. Conclusion SS While BusKill is an effective / unusual product that is certainly useful for a niche, protecting a running laptop against thieves is an extra layer when being outside. Obviously, this use case works only when the laptop is connected to power. --------------------------------------------------------------------- This article has been useful for you? Please consider hiring me as a freelance or send me a tip. Content under CC-BY-4.0. This blog is powered by own Common LISP blog engine