The easiest way to detect removal / insertion of CDROM in Linux

What is the easiest way to detect removal and insertion of a CDROM on Linux? I want to write simple code for this. For example, just bind an event to insert media and execute a handler.

Thanks!

+4
source share
3 answers

Try man udev or man hotplug .

+2
source

Desktop standards have options for this.

For example, I think Gnome uses dbus for this:

http://www.linuxcertification.co.za/linux-training-sysfs-udev-hald-dbus

[..] These applications are mainly used by the desktop environment to perform tasks when an event occurs, such as opening a file browser when inserting a USB drive or an image application when inserting a camera.

D-Bus is used, for example, to launch media players when an audio CD is inserted and, for example, to notify other applications of the currently playing song.

Configuration

You can simply force your working shell (e.g. Gnome / nautilus) to invoke your application when this happens:

http://library.gnome.org/users/user-guide/stable/gosnautilus-61.html.en

Non-desktop

For a non-desktop version, man udev really your friend. Information on writing udev rules is here:

http://reactivated.net/writing_udev_rules.html

+1
source

The easiest way out of user space is to grep the mount command if you configured the CDROM for automount (in / etc / fstab). But if you want to receive asynchronous notification, you may need to look at udev and uevents rules.

0
source

All Articles