LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 03-01-2010, 04:11 PM   #1
atavus
LQ Newbie
 
Registered: Feb 2010
Posts: 6

Rep: Reputation: 0
Using UDEV to automatically run a script on optical disc mount?


I'm trying to get a script to run whenever an optical disc is mounted by the system but can't get it to work. I'm running Fedora 12 64bit and the drive is SATA device sr0.

Following online guides, I created a new rule file called '10-my.rules' in '/etc/udev/rules.d' which contains the following:
Code:
KERNEL=="sr0", RUN+="/usr/local/bin/myscript"
But nothing happens when a disc is mounted. The script itself runs fine manually and is clearly evident when launched so the problem must be the rule file. I've tried changing the name of the rule file in a few incremental steps from 10-my.rule to 99-my.rule to no effect.

I've gathered some information about my optical drive using the udevadm tool.
Here is the result of an info query:
Code:
P: /devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0/block/sr0
N: sr0
S: block/11:0
S: scd0
S: disk/by-path/pci-0000:00:1f.2-scsi-1:0:0:0
S: disk/by-label/METROPOLIS_USA_NTSC
S: cdrom
S: dvd
E: UDEV_LOG=3
E: DEVPATH=/devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0/block/sr0
E: MAJOR=11
E: MINOR=0
E: DEVNAME=/dev/sr0
E: DEVTYPE=disk
E: ID_CDROM=1
E: ID_CDROM_DVD=1
E: ID_CDROM_MEDIA=1
E: ID_CDROM_MEDIA_DVD=1
E: ID_CDROM_MEDIA_STATE=complete
E: ID_CDROM_MEDIA_SESSION_COUNT=1
E: ID_CDROM_MEDIA_TRACK_COUNT=1
E: ID_CDROM_MEDIA_TRACK_COUNT_DATA=1
E: ID_VENDOR=ATAPI
E: ID_VENDOR_ENC=ATAPI\x20\x20\x20
E: ID_MODEL=iHOS104
E: ID_MODEL_ENC=iHOS104\x20\x20\x20\x20\x20\x20\x20\x20\x20
E: ID_REVISION=WL0B
E: ID_TYPE=cd
E: ID_BUS=scsi
E: ID_PATH=pci-0000:00:1f.2-scsi-1:0:0:0
E: ID_FS_LABEL=METROPOLIS_USA_NTSC
E: ID_FS_LABEL_ENC=METROPOLIS_USA_NTSC
E: ID_FS_TYPE=udf
E: ID_FS_USAGE=filesystem
E: ACL_MANAGE=1
E: GENERATED=1
E: DKD_PRESENTATION_NOPOLICY=0
E: DKD_MEDIA_AVAILABLE=1
E: DEVLINKS=/dev/block/11:0 /dev/scd0 /dev/disk/by-path/pci-0000:00:1f.2-scsi-1:0:0:0 /dev/disk/by-label/METROPOLIS_USA_NTSC /dev/cdrom /dev/dvd
Here is the result of monitoring udev while a disc is inserted:
Code:
KERNEL[1266604947.007297] change   /devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0 (scsi)
KERNEL[1266604947.008405] change   /devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0/block/sr0 (block)
UDEV  [1266604947.008441] change   /devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0 (scsi)
UDEV  [1266604947.663998] change   /devices/pci0000:00/0000:00:1f.2/host1/target1:0:0/1:0:0:0/block/sr0 (block)
 
Old 03-01-2010, 05:38 PM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 248Reputation: 248Reputation: 248
Have you tried a simpler rule to see if it's the rule or the script that's a problem ?

i.e.
Code:
KERNEL=="sr0", NAME="my_optical_disk"
What does udevinfo say about the optical drive ?
 
Old 03-02-2010, 12:56 PM   #3
atavus
LQ Newbie
 
Registered: Feb 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Hi, thanks for taking time to help me out. Your suggestion of using a simpler rule, I'm afraid, isn't possible since the rule I tested...
Code:
KERNEL=="sr0", RUN+="/usr/local/bin/myscript"
...contains only a single criteria.
Code:
KERNEL=="sr0"
Also, udevinfo is a deprecated function in Fedora 12 and has been replaced by udevadm, which I did use and the results were included in my original post.
 
Old 03-02-2010, 01:23 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
How do you know your script isn't being run?

Changing to 99-my.rule should ensure that it is, lexically, the last rule processed but you could try changing RUN+= to RUN= to ensure it is not subsequently changed.

You can generate (too much!) udev logging (in /var/log files) by changing udev_log in etc/udev/udev.conf to "debug". Maybe "info" will give enough information without going to "debug".

Last edited by catkin; 03-02-2010 at 01:25 PM. Reason: Removed a stray "/"
 
Old 03-02-2010, 01:45 PM   #5
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 248Reputation: 248Reputation: 248
Quote:
Originally Posted by atavus View Post
Hi, thanks for taking time to help me out. Your suggestion of using a simpler rule, I'm afraid, isn't possible since the rule I tested...
Code:
KERNEL=="sr0", RUN+="/usr/local/bin/myscript"
...contains only a single criteria.
Code:
KERNEL=="sr0"
Also, udevinfo is a deprecated function in Fedora 12 and has been replaced by udevadm, which I did use and the results were included in my original post.
I think you missed the point of my post.

The criteria isn't the problem, the script not running is.

Unless you can prove that any rule you supply works, then you cannot say whether the script is even being called.

If you read the udev docs you will see that any script does not run in the context of a shell, and is not going to output anything to a terminal through stdout.

If you can prove that another simpler rule works, then you can concentrate on the script.
I even supplied an example where I didn't change the device, just the action.

or not.

Last edited by smoker; 03-02-2010 at 01:48 PM.
 
Old 03-03-2010, 07:47 PM   #6
atavus
LQ Newbie
 
Registered: Feb 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Again, thanks for the help. The script I'm trying to run is very obvious when executed - it's an automatic disc ripper that firstly pops up a dialogue box via zenity for 30 seconds giving an option to exit the script, after that it rips whatever is in the drive, ejects the disc and sets up an encoding cue.

I tried smoker's idea of testing a simple rule
Code:
KERNEL=="sr0", NAME="my_optical_disk"
which had it's intended effect so the rule is working.

I suspect this may be an issue with my script being launch by the system instead of my user account or root. I'll look into more.
 
Old 03-03-2010, 10:34 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by atavus View Post
... firstly pops up a dialogue box via zenity ...
That can't work from within the udev system; zenity needs to know which display to use and udev doesn't have one.
 
Old 03-04-2010, 03:37 PM   #8
atavus
LQ Newbie
 
Registered: Feb 2010
Posts: 6

Original Poster
Rep: Reputation: 0
You're right, Catkin, and that was the problem. Zenity was taking down the script from the get-go since it was the first line in there. However I found a solution by adding an 'export DISPLAY' command to the code.
Code:
export DISPLAY=:0.0; zenity --question --title="Auto Disc Ripper" --text="Allow automatic disc rip?" --timeout=15
Once that was solved the UDEV command was easy; it will launch my script when a disc is mounted by the optical drive. Also, the rule file had to run last with the name "99-my.rule" in order to function.
Code:
SUBSYSTEM=="block", KERNEL=="sr0", ENV{DKD_MEDIA_AVAIABLE}=="1", RUN+="/usr/local/bin/detectdisc"
Everything is now working.

Last edited by atavus; 03-05-2010 at 04:16 PM.
 
Old 03-04-2010, 09:48 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by atavus View Post
Code:
SUBSYSTEM=="block", KERNEL=="sr0", ENV{DKD_MEDIA_AVAIABLE}=="1", RUN+="/usr/local/bin/detectdisc
Glad you solved it

In case anyone wants to copy this technique, the closing double quote is missing from RUN+="/usr/local/bin/detectdisc.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to run a shell script automatically? ceantuco Linux - Newbie 4 12-16-2008 10:16 AM
Execute script automatically on mount Groucho Ubuntu 1 06-11-2007 08:54 PM
Can't run openoffice from bash-script with udev krisealv Programming 1 04-16-2007 11:17 AM
Automatically run script on file? paddyjoy Linux - Software 3 10-03-2006 04:29 PM
Run script automatically shipon_97 Linux - Newbie 2 08-20-2006 12:13 AM


All times are GMT -5. The time now is 03:57 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration