Default Ubuntu power scripts can be triggered this way.
Add a file pm-utils
with this content
SUBSYSTEM=="power_supply", ATTR{online}=="0", RUN+="/usr/sbin/pm-powersave true"
SUBSYSTEM=="power_supply", ATTR{online}=="1", RUN+="/usr/sbin pm-powersave false"
to /etc/udev/rules.d/
And the default Ubuntu powersave features will be applied when you switch from the battery to AC and back.
There is a problem that it is not triggered on boot. To fix it, add
udevadm trigger -s power_supply
to /etc/rc.local
before exit0
.
With Ubuntu 16.04 another problem is that Network Manager defaults to Power management off. When an interface is brought up, it overrides the pm-utils
setting.
This can also be fixed by adding a file 02-powersave
#!/bin/sh
[ "$1" = "wlan0" ] && [ "$2" = "up" ] && udevadm trigger -s power_supply
to /etc/NetworkManager/dispatcher.d
.
Note: You need to replace wlan0
with your wireless interface that can be found in ifconfig
.