LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2007, 02:38 PM   #1
garagestudios
LQ Newbie
 
Registered: Dec 2007
Posts: 5

Rep: Reputation: 0
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!

Mike
 
Old 12-14-2007, 03:22 PM   #2
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
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.
 
Old 12-14-2007, 03:31 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by rsashok View Post
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.)
 
Old 12-14-2007, 03:33 PM   #4
garagestudios
LQ Newbie
 
Registered: Dec 2007
Posts: 5

Original Poster
Rep: Reputation: 0
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.

Thanks!
 
Old 12-14-2007, 04:07 PM   #5
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
Quote:
Originally Posted by pixellany View Post
Is this in fact the common way of doing it?
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.
 
Old 12-14-2007, 04:11 PM   #6
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
Quote:
Originally Posted by garagestudios View Post
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.
 
Old 12-14-2007, 04:20 PM   #7
garagestudios
LQ Newbie
 
Registered: Dec 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Let me try that.

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?

Thanks!!!
 
Old 12-14-2007, 04:23 PM   #8
garagestudios
LQ Newbie
 
Registered: Dec 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rsashok View Post
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?
 
Old 12-14-2007, 05:02 PM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by garagestudios View Post
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).
 
Old 12-14-2007, 05:37 PM   #10
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
osor,

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?
 
Old 12-14-2007, 08:14 PM   #11
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by rsashok View 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.
 
Old 12-16-2007, 12:23 AM   #12
hellork
Member
 
Registered: Nov 2007
Posts: 41

Rep: Reputation: 15
Just use your window manager's autoplay feature. In gnome, vis:

gconftool-2 -t string -s /desktop/gnome/volume_manager/autoplay_dvd_command "myscript.sh"
 
Old 12-16-2007, 07:31 AM   #13
ciotog
Member
 
Registered: Mar 2004
Location: Canada
Distribution: Slackware current
Posts: 728
Blog Entries: 2

Rep: Reputation: 43
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 .
 
Old 12-26-2007, 03:03 PM   #14
kscott121
Member
 
Registered: Jul 2003
Location: NC
Distribution: Fedora,Mepis,Debian
Posts: 84

Rep: Reputation: 15
I ran across this link which might help a bit.

http://wiki.sourcemage.org/Automounting_with_udev

ken
 
Old 12-26-2007, 03:22 PM   #15
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running script from KDE start menu bertheylen Linux - Software 3 02-14-2006 06:03 AM
Running a script to start WiFi on boot nanodano Linux - Hardware 3 11-06-2005 09:00 PM
Script to see if a process ir running, if not start it ncsuapex Programming 4 07-03-2005 01:27 PM
running start up script Kendo1979 Linux - General 2 11-08-2004 12:48 PM
Execute script when CD is inserted rokka Linux - Software 0 04-14-2004 01:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration