2

I want a notification in desktop that a particular USB device is inserted.Hence the following is the udev rule.

KERNEL=="sd*",SUBSYSTEMS=="usb",ACTION=="add",RUN+="/home/username/Desktop/notify_script"

notify_script is as follows

#!/bin/sh  
su username -c 'notify-send "USB Inserted"'  
echo USB_inserted >> /home/username/Desktop/test_file 

Problem :

The above script works perfectly if it is executed as root from command line but, notify-send in script is not working if executed from udev.

Question:

How to make notify-send work from udev? or Is there any other way by which I can achieve notification ?
Is it possible to invoke any GUI from udev?

Thank you.

1 Answers1

2

The main problem is, that the udev-rule will not run in any xorg-related environment per default, thus not knowing which DISPLAY to use. Therefore it will always fail, if You want to echo something into a terminal like a gnome-terminal for example. The script, which shall be executed on the udev-rule-match, must prior to any ui-related execution first export the DISPLAY. This is done via

export DISPLAY=:0

I assume, that this also will be the problem, and notify-send will just run against the wall.

I am actually also playing with udev-rules, and I managed it to work, though i am acting as root, similar to my answer and this one found already here :

https://unix.stackexchange.com/questions/80882/udev-running-a-shellscript-that-accesses-an-x-display

And also here

Scripts launched from udev do not have DISPLAY access anymore?

You might want also to check zenity. Very helpful for small notifications

Community
  • 1
  • 1
icbytes
  • 1,831
  • 1
  • 17
  • 27
  • it's not working a GUI display called by udev rules if not pre-run at least one time the same script directly from a terminal. you test it directly after a restart it doesn't display anything. but script get called... do you have idea? i have tested a script with zenity – vivi Aug 30 '16 at 13:45