System event on AC adapter insert or battery unplugged?

8,065

You are looking for udev, which detects changes to various system states, including plugging devices in (or unplugging them). You can script udev to take some action when a device is attached or removed.

Udev is, frankly, pretty complex, and you will want to do some really basic tests to make sure that you are detecting what you are looking for.

The basic steps are these:

Monitor your system:

sudo udevadm monitor

This tells you what udev "sees".

With this running, plug in the power supply, and see what udevadm tells you. Unplug it, again: see what udevadm tells you. It will probably make no sense yet, but that's ok; you know how to monitor udev now, and that's useful.

Stop that process, and do this:

sudo udevadm info --path=/sys/class/power_supply/ac

or

sudo udevadm info --path=/sys/class/power_supply/battery

which gives you information about the AC and BATTERY systems. Like this:

P: /devices/platform/sunxi-i2c.0/i2c-0/0-0034/axp20-supplyer.28/power_supply/battery
E: DEVPATH=/devices/platform/sunxi-i2c.0/i2c-0/0-0034/axp20-supplyer.28/power_supply/battery
E: POWER_SUPPLY_CAPACITY=100
E: POWER_SUPPLY_CURRENT_NOW=0
E: POWER_SUPPLY_ENERGY_FULL_DESIGN=3200
E: POWER_SUPPLY_HEALTH=Good
E: POWER_SUPPLY_MODEL_NAME=battery
E: POWER_SUPPLY_NAME=battery
E: POWER_SUPPLY_ONLINE=0
E: POWER_SUPPLY_PRESENT=0
E: POWER_SUPPLY_STATUS=Full
E: POWER_SUPPLY_TECHNOLOGY=Li-ion
E: POWER_SUPPLY_TEMP=300
E: POWER_SUPPLY_VOLTAGE_MAX_DESIGN=4200000
E: POWER_SUPPLY_VOLTAGE_MIN_DESIGN=3300
E: POWER_SUPPLY_VOLTAGE_NOW=0
E: SUBSYSTEM=power_supply

The E: lines are environment attributes you can use to write a udev rule.

Write a basic udev rule (I usually just write one to create a log entry somewhere on my drive when the even happens; that way, I know I am triggering something. Then I go back and make the script useful.)

For example: SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_STATUS}=="Charging", RUN+="/path/to/some/script.sh"

or you might use POWER_SUPPLY_PRESENT=1 ... you'll have to experiment based on what values you see on your computer.

Place this udev script in /etc/udev/rules.d (a name like 80.power.rules would be conventional) and restart your computer. Technically you can just restart udev but I find that unreliable.

See if your script gets triggered when your AC is plugged in.

Refine your udev rule and the script it triggers.

There is much more to udev, so dive in!

Share:
8,065

Related videos on Youtube

armadadrive
Author by

armadadrive

Hobbyist programmer, marketer, musician, sound-engineer, writer, husband, father.

Updated on September 18, 2022

Comments

  • armadadrive
    armadadrive over 1 year

    Does Ubuntu 15.04 (or maybe just Linux in general) have any sort of measurable event that fires when a laptop's AC power is disconnected and the unit flips over to battery power?

    I would like to write a small program that waits for that event and then makes some adjustments to things like screen brightness, for instance. I know there are some solutions for this, but I'm trying to teach myself at the same time. I'm new(ish) to Linux and have done the majority of my coding in .NET on Windows systems.

    I have seen battery status info in /sys/class/power_supply/, but I am assuming there must be a better way to determine if the adapter has been unplugged rather than just scanning for changes to files on some sort of timer.

    I've tried searching Google and these forums, but haven't yet figured out how to properly phrase the search.

    • Admin
      Admin about 5 years
      If wanting more detail than excelent aswer from @[Klaatu von Schlacker] there is a wiki page on archlinux wiki about ACPI
  • armadadrive
    armadadrive over 8 years
    Fantastic! Thanks for such a thoughtful answer. I'll have fun reading up about udev.