This question already has an answer here:

I have been trying to make a udev rule for a while, but without succes. For testing purposes, I have a file /etc/udev/rules.d/80-power.rules which contains

ACTION=="add", RUN+="/opt/eigen_scripts/waarschuwing_stroomkabel.sh"

Supposedly, this should run the specified script whenever a device like a USB stick is added (I monitored udevadm and the action indeed is add when plugging in a USB stick). This is not actually what I want to achieve in the end, but it seemed like the easiest way to at least get some kind of udev rule working. If you think the problem lies here, feel free to suggest an even simpler rule, I'd be glad to get any rule at all working.

The script in /opt/eigen_scripts/waarschuwing_stroomkabel.sh is

notify-send -i face-worried "Geen stroom!" "Sluit de adapter aan of de computer valt misschien uit."

and should send a notification.

Both files mentioned can be read and executed by root and all users. Using bash I have manually run the script in the second file (both as root and as normal user) and it works just fine. I have rebooted the system several times. I don't know what to try anymore. Thank you for your help.

I am working on a laptop with Ubuntu 16.04 installed.

Update: I have been looking at the duplicate question and the links in the comments, but haven't gotten much wiser. Updating my script to the script


export DISPLAY=":0"
notify-send "device plugged"

and rebooting did not solve anything, while it did at least partially work for the dupplicate questions's OP. I tried to follow the steps in the first two answers of this question but without effect. And I do not understand what this question is even about.

marked as duplicate by Gilles shell-script Sep 9 '16 at 23:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    notify-send probably relies on the environment variables from your session to work... Have you tried something simpler, like a touch /tmp/my-script-ran and seeing if /tmp/my-script-ran gets created? – derobert Sep 9 '16 at 15:17
  • Also, make sure your script is executable (chmod +x). Probably should have a #!/bin/sh line up top too. – derobert Sep 9 '16 at 15:18
  • Both scripts were executable, but I changed the second one to #!/bin/sh as the first line and tocuh /tmp/my-script-ran as the second one and this worked after reboot. Thanks for the help. How could I get notify-send to work? It works if I just execute it from the terminal ... – Bib-lost Sep 9 '16 at 15:44
  • 1
    notify-send needs the $DBUS_SESSION_BUS_ADDRESS environment variable from your desktop session. There are a few kludges to get it, search for a few and if none work ask a new question about it (or I suppose rewrite this one to be about it). – derobert Sep 9 '16 at 16:23
  • This and this are why your attempt didn't work. – Gilles Sep 9 '16 at 23:11