惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Last Week in AI
Last Week in AI
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
V
Visual Studio Blog
宝玉的分享
宝玉的分享
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
博客园 - 聂微东
S
SegmentFault 最新的问题
博客园 - 司徒正美
罗磊的独立博客
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
美团技术团队
小众软件
小众软件
Jina AI
Jina AI

Solene'%

Solene Solene'% : Software to keep photos organized Solene'% : Make your own container base images from trusted sources Solene'% : File transfer made easier with Tailscale Solene'% : Comparison of cloud storage encryption software Solene'% : Revert fish shell deleting shortcuts behavior Solene'% : Declaratively manage containers on Linux Solene'% : Hardware review: ergonomic mouse Logitech Lift Solene'% : URL filtering HTTP(S) proxy on Qubes OS Solene'% : Introduction to Qubes OS when you do not know what it is
Solene'% : How to trigger a command on a running Linux la...
2025-05-31 · via Solene'%

Written by Solène, on 31 May 2025.
Tags: #security #linux

Table of contents

  • 1. Introduction
  • 2. Setup
  • 3. Testing
  • 4. Script ideas
  • 5. Conclusion

1. Introduction §

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 §

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 §

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 §

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 §

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.