2

I'm looking to run some code every time my laptop is plugged into, or unplugged from power. I know this can be done; for example, GNOME hooks into this functionality (somehow) to update the battery indicator in my toolbar. The code I want to execute is a simple shell script which switches between the integrated and discrete GPU depending on whether external power is available (a wrapper around gpu-switch).

I'm specifically interested in knowing about ways to do this other than polling and parsing the output of e.g. acpi -V to determine whether the battery is charging or not.

3

Create a udev rule that runs a script when the AC adapter is plugged and unplugged:

/etc/udev/rules.d/powersave.rules
SUBSYSTEM=="power_supply", ATTR{online}=="0", RUN+="/path/to/your/script true"
SUBSYSTEM=="power_supply", ATTR{online}=="1", RUN+="/path/to/your/script false"
  • yep, but worth noting the script must be fast otherwise have the script spawn a backgroud process (long scripts in udev can make it miss events, see man page). – mikejonesey Nov 8 '16 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.