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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-01-2010, 04:11 PM
|
#1
|
|
LQ Newbie
Registered: Feb 2010
Posts: 6
Rep:
|
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)
|
|
|
|
03-01-2010, 05:38 PM
|
#2
|
|
Senior Member
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279
|
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 ?
|
|
|
|
03-02-2010, 12:56 PM
|
#3
|
|
LQ Newbie
Registered: Feb 2010
Posts: 6
Original Poster
Rep:
|
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. 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.
|
|
|
|
03-02-2010, 01:23 PM
|
#4
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
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 "/"
|
|
|
|
03-02-2010, 01:45 PM
|
#5
|
|
Senior Member
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279
|
Quote:
Originally Posted by atavus
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. 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.
|
|
|
|
03-03-2010, 07:47 PM
|
#6
|
|
LQ Newbie
Registered: Feb 2010
Posts: 6
Original Poster
Rep:
|
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.
|
|
|
|
03-03-2010, 10:34 PM
|
#7
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Quote:
Originally Posted by atavus
... 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.
|
|
|
|
03-04-2010, 03:37 PM
|
#8
|
|
LQ Newbie
Registered: Feb 2010
Posts: 6
Original Poster
Rep:
|
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.
|
|
|
|
03-04-2010, 09:48 PM
|
#9
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Quote:
Originally Posted by atavus
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:01 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|