# Cdrom control and detection

**URL:** <https://www.ruby-forum.com/t/cdrom-control-and-detection/168522>\
**Category:** Ruby\
**Created:** [May 21, 2009, 9:13pm UTC](https://www.ruby-forum.com/t/cdrom-control-and-detection/168522 "2009-05-21T21:13:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [May 21, 2009, 9:13pm UTC](https://www.ruby-forum.com/t/cdrom-control-and-detection/168522/1 "2009-05-21T21:13:59Z")

</div>

OK,

I know how to eject/open the CD drive on a linux box from within Ruby:

system “eject”

How do I detect that the user has inserted the next cd so that my  
program can continue cataloging cd contents?

I know it’s more of a Linux programming question, but maybe there is  
some rubylicious way to do this? Or just the good old fashioned best  
practice way will do.

Is there a good tutorial on detecting system and media changes?

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [May 21, 2009, 10:20pm UTC](https://www.ruby-forum.com/t/cdrom-control-and-detection/168522/2 "2009-05-21T22:20:41Z")

</div>

It just occurred to me that most linux users today use automounting! So  
using the sane method you should omit the last line BUT:

That is a slight problem because mounting (automatic or not) can take  
several seconds. If you insert a disk and hit enter immediately, and  
the program is not delayed by the system “mount” call, execution will  
proceed altho the mount may not be completed.

So, either you put in a gap  
sleep(5)  
which should be long enough, or you do some kind of check for data in  
the mount point directory.

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [May 21, 2009, 10:08pm UTC](https://www.ruby-forum.com/t/cdrom-control-and-detection/168522/3 "2009-05-21T22:08:53Z")

</div>

David Thurston wrote:

> How do I detect that the user has inserted the next cd so that my  
> program can continue cataloging cd contents?

You could do a loop with mount, system will return true if it succeeds  
(and false if it fails!). However, that is not a very nice idea unless  
you really, really want to completely minimize user interaction to  
nothing. I’d recommend something like:

puts (“Insert DVD and hit enter.”)  
gets  
system “mount /dev/cdrom”

A little more sane.

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [May 21, 2009, 11:16pm UTC](https://www.ruby-forum.com/t/cdrom-control-and-detection/168522/4 "2009-05-21T23:16:31Z")

</div>

Hi,

Mk 27 schrieb:

> David Thurston wrote:
> 
> > How do I detect that the user has inserted the next cd so that my  
> > program can continue cataloging cd contents?
> 
> You could do a loop with mount, system will return true if it succeeds  
> (and false if it fails!). However, that is not a very nice idea unless  
> you really, really want to completely minimize user interaction to  
> nothing. I’d recommend something like:

better than polling the mount directory would be to observe it with  
inotify [http://raa.ruby-lang.org/project/ruby-inotify/](http://raa.ruby-lang.org/project/ruby-inotify/)

Because, this library includes a smal implementation error, you should  
take a look at  
[http://www.mindbucket.com/2009/02/24/ruby-daemons-verifying-good-behavior/](http://www.mindbucket.com/2009/02/24/ruby-daemons-verifying-good-behavior/)  
and make the suggested change to the source before compile.

Cheers, detlef

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [May 21, 2009, 11:59pm UTC](https://www.ruby-forum.com/t/cdrom-control-and-detection/168522/5 "2009-05-21T23:59:29Z")

</div>

> better than polling the mount directory would be to observe it with  
> inotify [http://raa.ruby-lang.org/project/ruby-inotify/](http://raa.ruby-lang.org/project/ruby-inotify/)

> Cheers, detlef

Thanks Detlef!

Getting geekier 🙂

I wonder if the Linux kernel sends up a flare or a flag or signal or  
changes a constant or something in relation to removable media events?

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [May 21, 2009, 11:17pm UTC](https://www.ruby-forum.com/t/cdrom-control-and-detection/168522/6 "2009-05-21T23:17:46Z")

</div>

Thanks Mk 27,

That will work.

I wonder if there is anything in udev or hal or /proc that I can listen  
to?

(Always looking for a really ubergeeky answer)
