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
  • 7
  • 20

2 Answers2

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.

Zanna
  • 70,465
Sudheer
  • 5,113
  • 4
  • 24
  • 27
  • ./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 '14 at 16:25
  • yes that's correct – Sudheer Jul 11 '14 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 '14 at 16:30
  • can you post your <upower -d> output – Sudheer Jul 11 '14 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 '14 at 16:35
  • Device: /org/freedesktop/UPower/devices/line_power_AC native-path: AC power supply: yes updated: Fri 11 Jul 2014 09:51:15 PM IST (847 seconds ago) has history: no has statistics: no line-power online: yes – sameermw Jul 11 '14 at 16:35
  • k wait i am updating the code – Sudheer Jul 11 '14 at 16:37
  • i updated the shell script now. – Sudheer Jul 11 '14 at 16:38
  • Is it working now. – Sudheer Jul 11 '14 at 16:41
  • yes, It's working, Thank you very much sir. Great help. Thank you again. – sameermw Jul 11 '14 at 16:42
  • I use this --expire-time=5 But notification never expire, how do i add --expire-time= option to time out. – sameermw Jul 11 '14 at 16:44
  • you can use <upower -e> to view objects path for your power devices and then <upower -i> to view information regarding them. For more see upower --help – Sudheer Jul 11 '14 at 16:45
  • notify-send ignores the timeout command its a bug see it here http://askubuntu.com/questions/110969/notify-send-ignores-timeout – Sudheer Jul 11 '14 at 16:48
  • Oh. Thank you again for everything you've done sir. This is big favor. – sameermw Jul 11 '14 at 16:50
  • Instead of that add line after notify-send to script to end notify send <sleep 3; killall "processname of notify-send"> – Sudheer Jul 11 '14 at 16:53
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
  • 2
  • 7
  • 20
  • Time parameter is not implemented in notify-send!....one of the most controversial bug...https://bugs.launchpad.net/ubuntu/+source/notify-osd/+bug/390508 – Khurshid Alam Jul 11 '14 at 18:50