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, 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.
You need to set XDG_RUNTIME_DIR
as well. Change your crontab to this:
DISPLAY=":0.0"
XAUTHORITY="/home/nazar/.Xauthority"
XDG_RUNTIME_DIR="/run/user/1001"
00 13 * * * /home/nazar/Documents/scripts/lunch_break_job.sh # JOB_ID_2
50 * * * * /home/nazar/Documents/scripts/pc_break.sh # JOB_ID_1
* * * * * /home/nazar/Documents/scripts/cron_job_test.sh # JOB_ID
Make sure you change nazar
to whatever your username is and 1001
to your actual UID. You can get your UID by running id -u
.
And all you need in your script is:
#!/bin/bash
/usr/bin/notify-send "hello"
I just tested this on Arch running Cinnamon and it worked fine.
The variables are being set in the crontab, no need to export anything from the script. There's also no point in doing so, the script is being called by cron, it wouldn't export the values you need anyway.
Best Answer
This is half-way at best, but perhaps you find some help in it.
There are two pieces to the puzzle notify-send and the notify-daemon or notification-server. Various implementations have various philosophies on what to include and how to use the different suggestions by the spec.
Implementations like NotifyOSD supply an option on the
--expire-time
with0
or default-1
. If value is0
and alert box is used instead of notification, any other value is ignored – and server default used.The
--hint
option can be used to send extra information on how to lay out custom alerts such as volume and brightness. For example:show a volume notification with level set to
55
.As for the urgency level one thing, at least, is that it has an impact if other applications are running in full-screen mode. Games, video-player, (screen-saver?) etc. If Urgency level is low notifications are not displayed, if critical they are.