2

I am currently working on a mini project for which I need to set up a udev rule that sends a notification to the desktop when an external storage drive is plugged into the machine.

This notification should contain some basic information of the drive like SN, model, size etc...

I was able get the udev rule to run the script and send the notification to the desktop, but my problem is the notification bubble only appears for 5 seconds and then disappears.

I've tried to set the expiry time in the command, but it still does the same thing . The weird thing is if I run the command in Terminal by itself it's perfectly fine.

Here is my udev rule script

# Mark new block devices as read-only. Only keep the main drive as RW
KERNEL=="sd[c-z]*",ACTION=="add", SUBSYSTEM=="block",  KERNEL!="ram*",RUN+="/home/notify-send.sh '%E{DEVNAME}' '%E{ID_MODEL}'"

Here is my notify-send.sh script

export DISPLAY=:0
export XAUTHORITY=/home/akl_dennis/.Xauthority 
device_name=$DEVNAME
model_id=$ID_MODEL
icon="/home/READ-WRITE.png"
sn=$(hdparm -I $device_name |awk '/Serial Number:/ { print $3}')
size=$(lsblk $device_name |awk 'FNR ==2 {print $4}')
disk_status=$(blockdev --getro $device_name)
if [ "$disk_status" == 0 ]; then
   disk_status="READ-WRITE"
else
   disk_status="READ-ONLY"
   icon="/home/READ-ONLY.png"
fi

  notify-send -i $icon  "USB INSERTED" "Device: $device_name\\nSerial Number: $sn\\nModel: $model_id\\nSize: $size\\nStatus: $disk_status"

I noticed that there's some error logs which might relate to the issue, but I'm not sure how to interpret it

org.freedesktop.Notifications[2938]: ** (notify-osd:2942): WARNING **: dnd_is_idle_inhibited(): got error "The name org.gnome.SessionManager was not provided by any .service files"
0

If programs do NOT run in a terminal, for example cron, you must make sure certain environment variables are set in your script prior to issuing commands.
Also commands must either have the full path or their paths must be set in PATH in your script.

I created this little example script to show a notification for 10 seconds when the script is started by cron:

$ ls -l DoSomething 
-rwxrwxr-x 1 willem willem 286 May 31 12:12 DoSomething

$ crontab -l
# m h  dom mon dow   command
* * * * * /home/willem/DoSomething > /tmp/DoSomething.log 2> /tmp/DoSomething.err

$ cat DoSomething 
#!/bin/bash
eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME xfce4-session)/environ)";
echo "Setting vars..."
export DISPLAY=:0
export XAUTHORITY=/home/willem/.Xauthority

echo "Calling notify..."
/usr/bin/notify-send "Hello, world !" -t 10000
echo "Done !"
  • Note I am using an XFCE4 desktop. If you are using Gnome, use gnome-session in the eval-line. – user680858 May 31 '17 at 5:21
  • I have a few questions about your code . I don't really understand what do this two lines do eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME xfce4-session)/environ)"; echo "Setting vars...". And I am using Unity Ubuntu 16.04 – Dennis.Z May 31 '17 at 9:30
  • The "why" for using the 'eval' line isnt clear to me yet. I found it: 'askubuntu.com/questions/298608/…' and it worked. So i didnt sesrch any further. The echo-lines are just there for reference in the output file in /tmp in case the notify didnt work. I suggest you remove the echo's and use the rest to your convenience. – user680858 May 31 '17 at 10:06

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.