4

I need to enable power (AC) failure (off-line) and power on (on-line) notifications, like this notification:

Enter image description here![

I searched and tried to do that, but I didn't find any successful articles. I use these commands to monitor my AC adapter:

acpi- a

echo ac_adapter=$(acpi -a | cut -d' ' -f3 | cut -d- -f1)

But I don't know how to write notification on code.

Can I write a shell script like the following?

#!/bin/bash

power=ac_adapter=$(acpi -a | cut -d' ' -f3 | cut -d- -f1)
s1="$power"


if [ "$s1" = "off-line" ]; then

    notify-send  --urgency=low "Power Manager" "Power Down" -i battery_low
    echo "notification: off" >~/.scripts/notification

else
  if [ $s1 = "on-line" ]; then
    notify-send  --urgency=normal "Power Manager" "Power Up" -i battery_full

  fi
fi
sameermw
  • 525

2 Answers2

4

The shell script below works for AC power updates like plugged in and plugged out. You should run this code at start-up; it runs in an infinite loop.

#!/bin/bash

old="$(upower -i /org/freedesktop/UPower/devices/line_power_AC | fgrep online | awk '{print $2}')"
while sleep 1; do
    new="$(upower -i /org/freedesktop/UPower/devices/line_power_AC | fgrep online | awk '{print $2}')"
    if [ "$new" != "$old" ]; then
        if [ "$new" == "yes" ]; then
            notify-send --icon=gnome-power-manager "AC power on"
        elif [ "$new" == "no" ]; then
            notify-send --icon=gnome-power-manager "Battery power on"
        fi
    fi
    old="$new"
done

Edit the notify-send as you wish.

Zanna
  • 72,312
Sudheer
  • 5,213
0

According to Sudheer's answer I wrote another shell script, and it works fine on UbuntuĀ 14.04 (Trusty Tahr) with notify-send -t option. When I add --expire-time=TIME it won't work, but notify-send -t 30 works perfectly. Why?

Here is my script:

#!/bin/bash

stat=$(acpi -a | cut -d' ' -f3 | cut -d- -f1)


if [ "$stat" == 'on' ];then
a=yes
elif [ "$stat" == 'off' ];then
a=no
fi

while true; do

    stat=$(acpi -a | cut -d' ' -f3 | cut -d- -f1)

    if [ "$stat" != "$a" ]; then
        if [ "$stat" == "on" ];then
            notify-send -t 30 --icon=gpm-ac-adapter "AC power on"
        elif [ "$stat" == "off" ];then
            notify-send -t 30 --icon=notification-power-disconnected "AC Power Off Battery power on"
        fi
    fi
    a=$stat
    sleep 1
done
sameermw
  • 525