Ubuntu – How to shutdown automatically when AC power is not available

automationbatterypower-managementshutdown

My old laptop has a faulty battery, which shows 100% when on AC power but when unplugged it dramatically drops to random percentages within seconds and makes the machine turn off brutally. I am afraid of damaging my SSD (and my HDD) on which Ubuntu is loaded.

I wanted to turn the PC off as soon as the AC power is not available. I searched here and found this. But I don't understand the answer for that question or atleast it is relevant to my situation.

Please explain me a way to shut down my laptop automatically as soon as the AC power is unplugged or when there is a power failure.

Best Answer

Create a new rule in udev by opening the terminal and using:

gksu gedit /etc/udev/rules.d/50-ac-unplugged.rules

(If you are using Ubuntu 18.04 or a newer version gksu will not be available by default. In that situation please refer this question or use the above command as sudo -H gedit /etc/udev/rules.d/50-ac-unplugged.rules)

Put in the following line:

SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/sbin/shutdown now"

Save the file and then restart udev services with:

sudo udevadm control --reload-rules

Save all your work and unplug your power supply.

Related Question