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.
Best Answer
Create a
udev
rule that runs a script when the AC adapter is plugged and unplugged: