How to start running a script once a CD is inserted
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to start running a script once a CD is inserted
I am trying to write a script that will start when a CD or DVD is inserted into the computer. What is the best way to handle this? I will probably just be inserting DVDs, and do not want a DVD player to start (they are video DVDs). I am slightly new at writing scripts in Linux. Thanks for the help!
I suggest write a script and put on the background, which would parse tail of /var/log/messages for a message which tells that CD/DVD is mounted, and when this is detected calls a function you intended to perform.
I suggest write a script and put on the background, which would parse tail of /var/log/messages for a message which tells that CD/DVD is mounted, and when this is detected calls a function you intended to perform.
Is this in fact the common way of doing it? I somehow had it in my head that "signals" or "events" were generated for such actions. (I have no actual experience.)
I don't know if it's a usual approach, but I've done something similar with USB stick, e.g. when stick was plugged in and automounted the script was checking for a specific 'authorization' file, and if the file was not there was unmounting the stick. I am sure there are some other, maybe better, approaches, but that one worked for me.
That sounds like it will work. I am new to linux. What would i be parsing for? I will also open up the log and have a look.
Type 'tail -f /var/log/messages', and plug your CD to see exact message produced.
BTW: another way is probably checking in /proc/partitions for a new partition associated with CD. I think it is even better, because you don't need to be a root to go there.
My end goal is to create a script that can detect when a DVD is inserted into the computer and then rip the main title, then use transcode (or DVD::rip) to create a MPEG1 file that will fit on a CD.
I have manually been doing this with DVD::rip, but I would like to automate it.
I would like the whole process to start when the DVD is inserted, then request a file/folder name for the file, rip & transcode the main chapter, and finally eject the DVD when complete.
Any tips on how to do this, or does anyone have existing scripts similar to this that I could modify?
I suggest write a script and put on the background, which would parse tail of /var/log/messages for a message which tells that CD/DVD is mounted, and when this is detected calls a function you intended to perform.
I tried that on Debian and I saw nothing regarding mounting a CD. Would it be in a different log file for Debian?
My end goal is to create a script that can detect when a DVD is inserted into the computer and then rip the main title, then use transcode (or DVD::rip) to create a MPEG1 file that will fit on a CD.
The preferred way of doing this sort of thing is using HAL. It is much cleaner and less hackish than “tail -f /proc/kmsg” or something. HAL will even determine the type of CD (e.g., volume.disc.has_audio, volume.disc.has_data, volume.disc.is_blank, volume.disc.is_videodvd, volume.disc.is_vcd, volume.disc.is_svcd, etc.). You can use something like ivman to match a value and execute a script on a match.
As for writing the script itself, I might use tcl/tk for a quick-and-dirty solution. An even quicker (and no doubt dirtier) solution is to use bash with Xdialog (or its analogs).
I never worked or used HAL and my google search didn't produce any usable information except papers describing Hardware Abstraction Layer. Is there a usable link describing how to use HAL for the real life situation, like in the original post?
Is there a usable link describing how to use HAL for the real life situation, like in the original post?
Well, HAL by itself is really just an API (it’s actually a daemon—hald—and a D-Bus API for talking to the daemon). It is a little like udev (but more capable). Udev will process UEVENTS (e.g., plugging in a usb drive), but HAL can process those and other things (e.g., putting in a CD). I guess that’s the difference between removable media (CDs) and hotpluggable media (USB drives). Also, HAL is also supposed (eventually) to be cross-platform (it’s a project of freedesktop.org), whereas udev is specific strictly to linux (although udev is used partly as a backend in the the reference implementation for HAL on linux).
Most desktop environments (e.g., KDE, GNOME, XFCE) provide daemons (e.g., kioslave, gnome-volume-manager, and thunar-volman) which use HAL to let you know what is going on with removable media (and hotpluggable media). That’s what’s responsible for the annoying little prompts such as: “You have inserted a Blank CD. Would you like to run your CD burning software or ignore this message?” Anyway, something like ivman is a daemon which gives you finer control over how events should be handled. It is also more script-friendly and is disconnected from any particular window manager/desktop environment.
Anyway, here is an article about using ivman. If you want to know specifics about HAL itself, see here.
If I needed something working quickly I would be tempted to use a command like "volname" on the device node, and check for something other than the "No medium found" message. I think it would be a lot less intrusive than tailing /var/log/messages (which incidentally I do anyway with root-tail .
Inserting a CD doesn't trigger any kernel events like inserting USB mass storage devices does -as far as I know. To detect insertion of a CD you need to use some sort of daemon which periodically polls the drives. The other alternative is to use a kernel patched with supermount so that the events are created. Maybe autofs is able to do something with CD's by now -haven't checked recently. If you want to create some sort of independent daemon you might look for a program called cdstatus which you can run periodically to check if there is a medium present. Of course, ivman or HAL will do the same thing and are likely to already be present on most current systems.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.