Monday, May 6, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 1148  / 3 Years ago, tue, may 11, 2021, 11:55:41

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

More From » power-management

 Answers
1

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.


[#24181] Wednesday, May 12, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
percol

Total Points: 493
Total Questions: 116
Total Answers: 107

Location: Taiwan
Member since Mon, Sep 6, 2021
3 Years ago
percol questions
Thu, Sep 29, 22, 07:42, 2 Years ago
Sat, Jul 16, 22, 21:00, 2 Years ago
Tue, May 30, 23, 19:16, 1 Year ago
Wed, Aug 18, 21, 02:15, 3 Years ago
;