1

Background (can be skipped): Because of some (probably hardware) defect, my laptop sometimes disconnects from the battery without warning, causing an immediate and insecure shutdown if the AC adapter is not attached to computer and power supply. Several so-called computer repairers have proven completely unable to locate the problem (one tried to make me change my harddrive, claiming that 80% of it was corrupted even though I could still access all my files) so after having wasted a lot of money and getting nothing, I decided to just live with it and only use the laptop when it's connected to an external power supply.

Actual question: How can I make Ubuntu (16.04) immediately notify me (by text message and/or sound) whenever the power supply through the AC adapter is interrupted?

Own attempt: I wrote the following script

notify-send -i face-worried "No power!" "Connect the adapter."

which can be run from /opt/custom_scripts/warning_AC.sh both as root and as user (I tried both in bash). I have then tried to mimic the method explained in this answer on unix SA to let this script be run whenever the AC chord is plugged in. That is, I have monitored

sudo udevadm info --path=/sys/class/power_supply/AC0

to identify the changes made when plugging adapter in or out. I saw (among others)

E: POWER_SUPPLY_NAME=AC0
E: POWER_SUPPLY_ONLINE=0
E: SUBSYSTEM=power_supply

where in the second line the 0 changed to 1 when the power chord was plugged in and vice versa when it was plugged out. I thus decided the script I needed to place in /etc/udev/rules.d/80-power.rules was

SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_NAME}=="AC0", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/opt/custom_scripts/warning_AC.sh"

I rebooted the computer, but nothing happened when plugging the power in or out.

I am very new to Linux and Ubuntu and willing to learn as much as I can about commands and scripts and stuff but this is as far as I got. Thanks for your help.

Bib-lost
  • 302
  • 3
    May be http://askubuntu.com/questions/196333/how-to-play-a-sound-when-laptop-gets-unplugged?rq=1 will be helpful. On another note, did you try changing your house, may be 100% of the power lines are corrupted #not-serious – Ejaz Sep 08 '16 at 19:08
  • I will take a look at it. The #not-serious made my day though. – Bib-lost Sep 08 '16 at 19:20

1 Answers1

2

From OmgUbuntu, the app that perfectly fits your definition, BATTERY MONITOR.

First install the dependencies:

sudo apt install python3 python3-gi libnotify-dev acpi

Then, install via ppa:

sudo add-apt-repository ppa:maateen/battery-monitor -y; sudo apt-get update; sudo apt-get install battery-monitor -y

OR

Download the app from the developer's Github page and install it (considering you are using Xenial):

cd ~/Downloads && wget -c https://github.com/maateen/battery-monitor/releases/download/v0.4/battery-monitor_0.4-xenial_all.deb && sudo dpkg -i battery-monitor_0.4-xenial_all.deb

Though not mentioned but it doesn't hurt to do a quick restart.


EDIT:- Also, it seems you need to manually launch the app atleast once for the app to start working.

Raphael
  • 8,035
  • I had just stumbled upon this myself when you answered. Will try this at once! – Bib-lost Sep 08 '16 at 20:22
  • Just intalled it - still wondering if there's any options, since the default settings don't do what I need ... – Bib-lost Sep 08 '16 at 20:29
  • @Bib-lost - Did you reboot? – Raphael Sep 08 '16 at 20:34
  • I did, there seems to be no GUI for customization. – Bib-lost Sep 08 '16 at 20:34
  • @Bib-lost - Did you launch it atleast once? – Raphael Sep 08 '16 at 20:36
  • Sure, both graphically and via the terminal, it just says "Congrats! Started to monitor now" and that's it. Not even a way to terminate it without the terminal. – Bib-lost Sep 08 '16 at 20:38
  • @Bib-lost - Nothing happens on plugging in power source? – Raphael Sep 08 '16 at 20:39
  • No, as far as I can tell it doesn't react to plugging the power source in or out (at least not within the first twenty seconds) – Bib-lost Sep 08 '16 at 20:40
  • Update: Battery Monitor seems to give notifications when the battery changes from charging to discharging or vice versa. If the battery is quasi full, this does NOT coincide with plugging the adapter in or out, e.g. if the battery is 98% full and the adapter is plugged in, the battery will discharge to a certain level (idk, 80% or so) then start charging again. – Bib-lost Sep 08 '16 at 22:15