You are not logged in.

#1 2017-10-22 17:18:39

Head_on_a_Stick
Member
From: London
Registered: 2015-09-29
Posts: 9,093
Website

Notification popup when mains charger is unplugged

My laptops are all very old now and their batteries are pretty much fried so I need a notification when the charger becomes accidentally dislodged.

Although xfce4-power-manager shows popups for such things, the connector on the family laptop is dodgy so it will spam the desktop with multiple messages.

To solve this, I have instead created a udev rule to run a notification script when the cable is unplugged.

This method needs the rule itself, located at /etc/udev/rules.d/99-popup.rules with this content:

ACTION=="change", SUBSYSTEM=="power_supply", ATTR{type}=="Mains", ATTR{online}=="0" RUN+="/bin/su empty -c /usr/local/bin/popup"

Note that this rule is user-specific and will only work for empty, change this to suit your system.

If more than one user is present, create a new rule for each user — for example /etc/udev/rules.d/98-popup-colin.rules with colin in the RUN command instead of empty.

Then create the notification script (/usr/local/bin/popup):

#! /bin/sh

export XAUTHORITY="$HOME/.Xauthority"
export DISPLAY=":0"
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u $USER)/bus"

/usr/bin/notify-send 'Charger is unplugged!' --expire-time=6000 --urgency=critical

Finally, give the script and rules the requisite permissions:

sudo chmod +x /usr/local/bin/popup
sudo chmod 600 /etc/udev/rules.d/*

The new rules will take effect upon a reboot or they can be reloaded manually with:

sudo udevadm control --reload-rules

Last edited by Head_on_a_Stick (2017-10-22 18:58:07)

Offline

Board footer

Powered by FluxBB