Automatically Launch Bash Script On Optical Disc Mount?

User avatar
flickeringsight
Posts: 41
Joined: Fri Nov 06, 2009 3:46 pm
Location: Ventura, Ca

Automatically Launch Bash Script On Optical Disc Mount?

Postby flickeringsight » Wed Feb 24, 2010 1:29 pm

Hi everybody, I've been search the web trying to find a solution to this but haven't found one. I want a script to automatically run whenever an optical disc is mounted by the system. It seems like an easy thing to setup but I have yet to get it to work.

Does anyone here have a solution or any ideas?

Thanks!
HDA: 2.6GHz Dual Core, Gigabyte MB, 2GB RAM, Blu-Ray, 320GB+1.0TB+2.0TB+4.0TB, Amahi 8

jOOc
Posts: 19
Joined: Sun Jan 17, 2010 3:24 am
Location: Sweden

Re: Automatically Launch Bash Script On Optical Disc Mount?

Postby jOOc » Thu Feb 25, 2010 12:32 pm

Hi everybody, I've been search the web trying to find a solution to this but haven't found one. I want a script to automatically run whenever an optical disc is mounted by the system. It seems like an easy thing to setup but I have yet to get it to work.

Does anyone here have a solution or any ideas?

Thanks!
You should be able to do that by writing your own udev rule, see http://reactivated.net/writing_udev_rul ... ternal-run

User avatar
flickeringsight
Posts: 41
Joined: Fri Nov 06, 2009 3:46 pm
Location: Ventura, Ca

Re: Automatically Launch Bash Script On Optical Disc Mount?

Postby flickeringsight » Thu Feb 25, 2010 1:54 pm

Thanks for the help. Unfortunately I've already tried and failed at getting udev rules to work and have read that web page over and again. I must have made four dozen variations without success and am beginning to think udev is just broken.

Here's my last few attempts from the 10-my.rules file I created:

Code: Select all

KERNEL=="sr0", ACTION=="add", RUN+="/usr/local/bin/myscript" KERNEL=="sr0", RUN+="/usr/local/bin/myscript" SUBSYSTEM=="block", ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="pci-0000:00:1f.2-scsi-1:0:0:0", RUN+="/usr/local/bin/myscript" KERNEL=="sr0", RUN+="/usr/bin/myscript.sh"
The last line is based on the system-generated "70-persistent-cd.rules" so I figured that should work, but it didn't. You can see I also tried moving my script to different directories and changing the naming a bit. I also tried changing the preceding number on my udev config file from "10-my.rules" to "99-my.rules" in incremental steps and nothing worked. Also I should note that the script I'm trying to execute runs fine from the terminal.

Has anyone else running Fedora 12 gotten a custom udev rule to work?
HDA: 2.6GHz Dual Core, Gigabyte MB, 2GB RAM, Blu-Ray, 320GB+1.0TB+2.0TB+4.0TB, Amahi 8

jOOc
Posts: 19
Joined: Sun Jan 17, 2010 3:24 am
Location: Sweden

Re: Automatically Launch Bash Script On Optical Disc Mount?

Postby jOOc » Mon Mar 01, 2010 2:00 am

I did an easy test with my USB.

1. Monitored my /var/log/messages

Code: Select all

tail -f /var/log/messages
Found this line when putting my USB in

Code: Select all

Mar 1 09:52:55 fc12 kernel: usb 1-4.3.3: New USB device found, idVendor=2008, idProduct=2018
2. Created this little script: /tmp/test.sh

Code: Select all

#!/bin/bash echo "Testing!!" >> /tmp/test.out
3. chmod +x /tmp/test.sh

4. vi /etc/udev/rules.d/10-test.rules

Code: Select all

SUBSYSTEM=="usb", SYSFS{idProduct}=="2018", SYSFS{idVendor}=="2008", RUN+="/tmp/test.sh"
5. udevadm control --reload-rules

6. Put my USB in and the file /tmp/test.out was created and contained "Testing!!"

User avatar
flickeringsight
Posts: 41
Joined: Fri Nov 06, 2009 3:46 pm
Location: Ventura, Ca

Re: Automatically Launch Bash Script On Optical Disc Mount?

Postby flickeringsight » Wed Mar 03, 2010 7:40 pm

Hey jOOc, thanks for the great reply. You helped to remind me to take baby steps when things aren't working.

Short story, I got my script working with the UDEV rule - sweet. I mimicked your test script, which worked, and traced the problem back to an obscure issue with Zenity which I am using to generate a dialog box from my script. Since Zenity wasn't being called from a terminal window it had no point of reference for where to open the dialog box and crashed out the script, so it needed a display explicitly stated. In case anybody needs the Zenity solution, here it is:

Code: Select all

export DISPLAY=:0.0; zenity --question --title="Auto Disc Ripper" --text="Allow automatic disc rip?" --timeout=15
HOWEVER! I encountered a second, smaller problem. My script was being launched whenever a disc is mounted AND ejected. By using the udevadm tool command 'udevadm info --query=all --path=/sys/block/sr0' to get information about the drive while a disc was present and not present I was able to figure an addition to my rule that fixed the problem.

Code: Select all

SUBSYSTEM=="block", KERNEL=="sr0", ENV{DKD_MEDIA_AVAIABLE}=="1", RUN+="/usr/local/bin/detectdisc"
The only caveat is that the rule name had to be changed to from "10-my.rule" to "99-my.rule".
HDA: 2.6GHz Dual Core, Gigabyte MB, 2GB RAM, Blu-Ray, 320GB+1.0TB+2.0TB+4.0TB, Amahi 8

Who is online

Users browsing this forum: No registered users and 29 guests