I would like to automatically run my own command any time a drive is inserted under Linux (including USB drives, for example). Obviously the simplest solution would be to regularly poll and check for changes to /proc/partitions or to /dev/disk, but I was hoping for something a little more elegant than that.
EDIT
I know this is possible, because GNOME does this very thing; any time a new drive is inserted, GNOME runs it's own "autoplay" mechanism to take some action. ("Would you like to view the pictures on this SD card?")
I can come up with two uses for this: the first is in an Amazon EC2 cluster I manage, to detect and initialize newly attached EBS storage volumes automatically.
The other usage is on a Linux-based home file-server device; when I insert an SD card into the device, it should mount the card, move the pictures from the card onto the internal hard drive, unmount the card, and then beep to let me know it's done.
Udev rules are what you are looking for. From the man page itself:
Emphasis mine.
The rules files are in /etc/udev/rules.d/ and their documentation is in udev(7). Oh, and they work for practically everything.
I found a pretty good link here. http://linlog.skepticats.com/content/udevautorun/
Basically you can place scripts in /etc/udev for all block devices or specific ones.
Autorun ( http://linux.die.net/man/1/autorun ) takes care of CDs, but I don't think something like that exists for other media. I'm really not of the opinion that it /should/, either.
You might be able to craft autorun into looking at dmesg on occasion, but chances are good that it will only work for your pet system.
What is your overall goal? Maybe there's a better way?