How to show notification when a usb device is inserted?

7,599

Solution 1

It's really surprizing for me that in 2014 we still don't have this feature working by default. It seems there are very little amount of people who needs this feature, but I really need it.

First of all, I found udev-notify package, but unfortunately it almost doesn't work for me: it works for very little time (1-2 minutes), and then, if I connect/disconnect any device, it crashes:

Traceback (most recent call last):
  File "./udev-notify.py", line 319, in <module>
    notification.show()
glib.GError: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name :1.1061 was not provided by any .service files

I haven't found any solution, so I had to remove it. (filed bugreport also)

There're no similar utilities I've found so far. So I had to implement my own solution, it is probably not brilliant, but it works for me: it notifies me with sound and visual notification for any device that I plug/unplug. Title of the device is received via lsusb, so that if I plug some unknown device, at least I see its vendor_id:product_id. I like it.

It looks like this:

enter image description here

Installation details.

Tested on Ubuntu 12.04, Xubuntu 12.04, Xubuntu 13.04, Linux Mint 13. I believe it should work on other Ubuntu 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, see readme.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 unpacked my-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.

Solution 2

The libnotify-bin package includes a binary, notify-send which displays a notification. Here's an example of invoking it:

notify-send "New USB device connected" "A new device has been connected: blah blah blah"

I believe you could craft a udev rule for "add" and for "remove" actions to trigger a small script which would invoke a notification. I haven't looked into this but I have seen similar scripts invoking encryption/mouting comands for storage devices.

The Guadalinex project also had this type of functionality[1] which they demo'ed in UDS Seville in 2005 (!), perhaps it would be easier to track down if they submitted their work upstream or if it's available as a separate package somewhere. Do NOT install the packages referred to in the Debian bug I am citing, this is only for reference purposes if anyone would decide to research it.

[1] http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=458405

Solution 3

There are applets for the gnome-panel which will let you see plugged in devices and let you eject them. For e.g there is ejecter which is a applet lets you see mounted removable devices and lets you eject them. KDE has a ultra cool notification system letting you see what devices has recently been plugged in. You can try using the lsusb command to check whether your device has been detected or not. And if you are the adventurous type you can write your own udev rules.

Share:
7,599

Related videos on Youtube

Keyvan
Author by

Keyvan

Updated on September 17, 2022

Comments

  • Keyvan
    Keyvan over 1 year

    It would be good to know that when you connect a USB device like a web-cam, it notifies me that it works or not.

    • Admin
      Admin over 13 years
      Could you please convert your sentence to question :) ?