I want to monitor USB-Keys on my system. I know they are always mounted in /media so I use inotify to monitor /media. Some USB Keys create a folder (e.g. sda) when plugged which
The subfolder problem with inotify is well known and easily reproduced:
Start inotifywait watching an empty tmp directory:
inotifywait -e create -m -r --format '%:e %f' ./tmp
In another shell enter:
mkdir tmp/0 tmp/0/0 tmp/0/0/0 tmp/0/0/0/0
You will most likely only get a notification for the first subdirectory.
CREATE:ISDIR 0
The distinct possibility of losing events (particularly subdirectory creation events) between the time a directory is created, your app gets notified, and a new inotify watch is added, makes recursive monitoring too unreliable. The only safe option is to scan contents of newly created directories.
From the inotify doc under Limitations and caveats:
If monitoring an entire directory subtree, and a new subdirectory is created in that tree, be aware that by the time you create a watch for the new subdirectory, new files may already have been created in the subdirectory. Therefore, you may want to scan the contents of the subdirectory immediately after adding the watch.
You can use inotifywait
command (from the inotify-tools
package) to monitor the /media directory, in order to check whether the inotify
events which interest you do occur.
reference:
http://www.noah.org/wiki/Inotify,_FAM,_Gamin#Examples_with_inotify-tools
If inotify
does miss events, the reason might be:
Inotify
does report some but not all events in sysfs
and procfs
.
(Well, I can not say for sure. Just my guess.)
Reference:
http://en.wikipedia.org/wiki/Inotify#Limitations
http://en.wikipedia.org/wiki/Sysfs
http://en.wikipedia.org/wiki/Procfs
In the meanwhile I found that this is a known issue of inotify. If two events appear virtually at the same time, inotify only catches one of them. My solution: I don't use inotify anymore, but took libudev instead to monitor the devices plugged to the machine...