Jaishree Alavandar
Jaishree Alavandar

Reputation: 45

notify-send not working (in script) executed from udev

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.

Upvotes: 2

Views: 3047

Answers (1)

icbytes
icbytes

Reputation: 1851

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

Upvotes: 2

Related Questions