1

I made a script that displays a notificaiton whenever I unplug the charger. I made the script to run by udev by making a rule in rules.d. The problem is the script run fine but the notification didn't show up. So far I know that the script requires the address of the display server, here is what my rule and script looks like.

export DISPLAY=:0
export XAUTHORITY=/home/user/.Xauthority
/usr/bin/notify-send --urgency=critical -i face-cool "Charger removed"
zenity --info --text 'Happy New Year'

Here is the rule that I created

SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_STATUS}=="Discharging", RUN+="/home/user/alertchargerremoved.sh"

Is there any other way to notify myself when the charger is removed, like instead of notification which doesn't require the X server? Do you have any other ideas, how can accomplish this thing?

  • Theoretically less clean, but very easy to set up: use a small background script running acpi -a once per five seconds or so, in case of change in output > show notification. – Jacob Vlijm Dec 11 '16 at 08:53
  • I found a workaround by adding root user as xhost +si:localuser:root and the script worked, but is it safe to do so? I will be adding this command in startup. – Hemant Yadav Dec 11 '16 at 11:13

1 Answers1

0

I have this worked for me before:

ACTION=="remove", KERNEL=="sd?", ENV{ID_PART_TABLE_TYPE}!="", RUN+="/usr/bin/sudo -u username DISPLAY=:0 notify-send 'Unsafe Remove' '<b><i>Your long message</b></i>' -i /usr/share/icons/gnome/48x48/emotes/face-worried.png -t 10000"

From: Alert the user when they unsafely unplug an USB thumb drive

So use sudo to run notify-send as same user (Not as root)

/usr/bin/sudo -u username notify-send ...
user.dz
  • 48,105