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

2 Answers 2

3

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.

15
  • ./script.sh: line 5: [: ==: unary operator expected ./script.sh: line 7: [: ==: unary operator expected And i made some changes if [ "$stat" == 'yes' ] elif [ "$stat" == 'no' ] Is it correct
    – sameermw
    Jul 11, 2014 at 16:25
  • yes that's correct
    – Sudheer
    Jul 11, 2014 at 16:28
  • i ran this code in terminal but out put is empty, echo $(upower -i /org/freedesktop/UPower/devices/line_power_ACAD | grep online | awk '{ print $2}') Any idea Sir?
    – sameermw
    Jul 11, 2014 at 16:30
  • can you post your <upower -d> output
    – Sudheer
    Jul 11, 2014 at 16:31
  • Device: /org/freedesktop/UPower/devices/battery_BAT0 native-path: BAT0 vendor: Samsung SDI model: DELL JXFRP1C serial: 9489 power supply: yes updated: Fri 11 Jul 2014 09:51:37 PM IST (825 seconds ago) has history: yes has statistics: yes
    – sameermw
    Jul 11, 2014 at 16:35
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
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .