Call notify-send from an udev rule
Well, after many hours of googling and asking on forums, I got it working (it seems). Anyone who wants to get nice visual and/or audio notification when some USB device is plugged/unplugged can install my script, see installation details below.
First of all, answers on my own questions.
1. How to get actual title of the device attached, the same as I can see in lsusb
output?
There's no such titles in the kernel (in common case). There is a database file with titles for many pairs vendor_id:product_id
, it's usually /usr/share/hwdata/usb.ids
file. This database can be updated by /usr/sbin/update-usbids.sh
. Thanks to guys from linux.org.ru for that info.
I don't know if there is some special tool for getting device title by pair vendor_id:product_id
, so I had to hack a bit with lsusb
and grep
: for example, lsusb | grep '0458:003a'
2. Currently, too many notifications are activated. Say, when I attach my USB stick, I got about 15 notifications!
I must admit I haven't figured out how to write rule for this, but I found another way I could filter it.
udev
allows us to use some substitutions for RUN+="..."
: say, we can get bus number and device number by $attr{busnum}
and $attr{devnum}
respectively. Firstly, in my script I store list of attached devices in the special file, so that if script got new "plug" event, and this device's busnum and devnum are already stored in our file, then notification isn't generated. And secondly, these substitutions $attr{busnum}
and $attr{devnum}
are usually available only for one of the devices from the "series" of events. But anyway, explained algorithm should sort it out in any case.
Current project page: my-udev-notify.
It looks like this:
Installation details.
Tested on Linux Mint 13, I believe it should work on Ubuntu and other Ubuntu's derivatives, and I hope it will work on any *nix system with udev
.
- Go to project page, get sources from there and put them somewhere. There's just one main script in it:
my-udev-notify.sh
, but archive also contains sounds for plug/unplug notifications, plus some more info, seereadme.txt
for details. - Create file
/etc/udev/rules.d/my-udev-notify.rules
with the following contents: (don't forget to modify path to your real path where you unpackedmy-udev-notify.sh
!)
ACTION=="add", RUN+="/bin/bash /path/to/my-udev-notify.sh -a add -p '%p' -b '$attr{busnum}' -d '$attr{devnum}'"
ACTION=="remove", RUN+="/bin/bash /path/to/my-udev-notify.sh -a remove -p '%p' -b '$attr{busnum}' -d '$attr{devnum}'"
After this, it should work for newly attached devices. That is, if you unplug some device, you won't get notification. But when you plug it back, you will. (yes, for me it works without any udev restarting. If it doesn't for you, try rebooting)
To make it work for all devices, just reboot your system. NOTE that there might
be many notifications during first boot (see known issues in the readme.txt
). On second
boot, there will be no notifications (unless you plug in new device when
system is off)
You can customize it (turn on/off visual and sound notifications, or change sounds), check readme.txt in the archive for details.
sudo -u X_user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/X_userid/bus notify-send 'Hello world!'
Where X_user is the user you want to run as, and X_userid is group ID of the user
example bigbird and 1000