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!