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 8: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
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 ...
| improve this answer | |
  • Sorry, but that didn't work in terminal, I doubt that it will work in the rules, Can you please check if the Command that you wrote in RUN works in your terminal? – Hemant Yadav Dec 11 '16 at 11:08
  • @HemantYadav , yes it is still working i.stack.imgur.com/TZaRf.png – user.dz Dec 11 '16 at 11:29
  • @HemantYadav, Did you find any solution? – user.dz Dec 17 '16 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.