LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 05-11-2007, 08:50 AM   #1
PlancksCnst
Member
 
Registered: Sep 2006
Posts: 34

Rep: Reputation: 15
udev RUN+= doesn't seem to work


I am making a system to copy all my podcasts over to my memory card (my mp3 player uses a memory card) when it is plugged in. I made a script to do the copying and it works just fine - I can call the script and it does the expected behavior. I made a udev rule to automatically run this script when the card is inserted. I also had the rule make a syslink so that the script could reference the same syslink no matter what device (/dev/sdb1, /sda1, etc) the card ended up on. It matches on the model attribute of the device.

The problem: the rule is making the symlink just fine, but it does not seem to be running the script. I have set the script to chmod 777 for testing purposes - so it is definitely executable by whatever user the udev system is using. The path is correct; I can copy straight from the text file and paste it on the command line, and it works. What could be wrong?

The following is the rule:
Code:
ATTRS{model}=="Flash*" ACTION=="add", SYMLINK+="memcard", RUN+="/home/shawn/copypodcasts.sh"
I'm certain that the script is fine since I can run it, and it works just fine, but here it is anyway:
Code:
echo "preparing to copy podcast directory"
echo "creating mount directory"
mkdir /mnt/memcard
echo "mounting memcard"
mount /dev/memcard /mnt/memcard
echo "removing previous copy"
rm -r /mnt/memcard/media/
echo "making directory for copying into"
mkdir /mnt/memcard/media
echo "copying podcasts..."
cp -r /home/shawn/podcasts/cache /mnt/memcard/media/
echo "done copying... cleaning up"
echo "unmounting memcard"
umount /mnt/memcard
echo "removing mount directory"
rmdir /mnt/memcard
echo "finished"
Thanks for your help!
 
Old 05-11-2007, 05:43 PM   #2
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Ruhr Area, Germany
Distribution: Slackware64 14.0
Posts: 1,517

Rep: Reputation: 90
What fell into my mind when reading your question was if it is enough to chmod the script to 777 as long as it might be in a folder that not everyone has access to. So depending on who "owns" the udev process the /home.... folder might be not accessible. Maybe try to put it in /usr/bin for example and see if that helps.
 
Old 05-11-2007, 06:19 PM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
For one thing, get rid of the echo statements. Since udev doesn't have access to your console that could be blocking the program.
 
Old 05-11-2007, 07:09 PM   #4
PlancksCnst
Member
 
Registered: Sep 2006
Posts: 34

Original Poster
Rep: Reputation: 15
I tried both suggestions. I also ran chmod after I copied since I'm not sure if cp preserves permissions (is there a way to see permissions on a file?). I also did udevcontrol reload_rules (although I don't think I need to). It still doesn't work.
 
Old 05-11-2007, 11:03 PM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You can look at the permissions on a file with the "ls -l <filename>" command.

The PATH that exists when the command runs depends on the environment of the UDEV daemon. Look for other run+= entries and locate where the called script is. On my SuSE system, there is a script in /lib/udev/. Also make sure that your rule isn't misplaced preventing the device from being mounted. If you get it working, you may want to add another test, such as ID_FS_LABEL={podcast*}. That way you won't accidently delete everything in a flash drive because of the rule.

You could redirect any errors output to a logfile.
[code]
ATTRS{model}=="Flash*" ACTION=="add", SYMLINK+="memcard", RUN+="/lib/udev/copypodcasts.sh 2&>>/tmp/copypodcast.log"

Last edited by jschiwal; 05-11-2007 at 11:49 PM.
 
Old 05-12-2007, 12:58 AM   #6
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
I suspect that shell scripts may not be executed by udev in this manner. (Just a guess, though) Try running "bash SCRIPTNAME" as the RUN+= command. Also, take note of this excerpt from the manpage:
Quote:
RUN
Add a program to the list of programs to be executed for a specific
device. This can only be used for very short running tasks. Running
an event process for a long period of time may block all further
events for this or a dependent device. Long running tasks need to
be immediately detached from the event process itself.
 
Old 05-12-2007, 01:17 AM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Code:
grep mount.sh /etc/udev/rules.d/*
/etc/udev/rules.d/85-mount-fstab.rules:SUBSYSTEM=="block", ACTION=="add", KERNEL=="sd*[0-9]|hd*[0-9]", RUN+="mount.sh"
jschiwal@hpamd64:/etc/udev/rules.d> locate mount.sh
/lib/udev/mount.sh
jschiwal@hpamd64:/etc/udev/rules.d> cat /lib/udev/mount.sh
#! /bin/bash
#
...
I would use a script in ~/bin/ after the device was mounted to do something like what the OP wants to do. However if he can get it to work, great. Part of the problem may be that udev rules aren't the best place for it because udev is used to create devices. Also, running a script like that as root just doesn't feel right.

I should have reread the manpage. Copying media files would take a long time. Maybe he could send a signal to a daemon which would copy the files.

I admit I still have a lot to learn about udev and the hal system, plus how the kde daemon interfaces with hal to do the mounting.
 
Old 05-12-2007, 01:28 AM   #8
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Oops, I should've remembered the mount.sh bit myself. Quite silly, guess it's getting too late.
 
Old 05-12-2007, 08:59 AM   #9
PlancksCnst
Member
 
Registered: Sep 2006
Posts: 34

Original Poster
Rep: Reputation: 15
Yeah - it's copying 800Mb over a usb 1.0 - it's going to take a while. I think I can detach a process by using:
Code:
(somescript.sh &) &
So I moved the script to /lib/udev/ (where all the other udev scripts lie) and changed my rule to:
Code:
ATTRS{model}=="Flash*" ACTION=="add", SYMLINK+="memcard", RUN+="(/lib/udev/copypodcasts.sh &) &"
It still doesn't run the script.
 
Old 05-12-2007, 09:03 AM   #10
PlancksCnst
Member
 
Registered: Sep 2006
Posts: 34

Original Poster
Rep: Reputation: 15
Does anyone know another way to do what I'm trying to do (copy -or sync- files over to a thumb drive automatically when it is inserted)
 
Old 05-12-2007, 10:22 AM   #11
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Here is a Linux Journal article on using udev to backup a thumb drive:
http://www.linuxjournal.com/article/9311

It also uses RUN+= in a udev rule. You might want to read his script. After initializing some variables used in the script, this is the first command it runs:
# wait for device to settle
sleep 10
 
Old 05-13-2007, 03:13 PM   #12
PlancksCnst
Member
 
Registered: Sep 2006
Posts: 34

Original Poster
Rep: Reputation: 15
Yep - I tried the wait. No luck. I also put a logger message in at the beginning of the script to see if it was even getting that far; it's not.
 
Old 05-13-2007, 03:42 PM   #13
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Ruhr Area, Germany
Distribution: Slackware64 14.0
Posts: 1,517

Rep: Reputation: 90
I just tried that myself and what worked was to put in even the full path to bash, for example
Code:
BUS=="scsi", KERNEL=="sd*", SYSFS{model}=="HD400LD         ", SYSFS{vendor}=="SAMSUNG ", NAME="%k", SYMLINK="usbhd-trekstor%n", RUN+="/bin/sh /etc/udev/scripts/logmount.sh"
EDIT: That was for just switching on the harddisk and creating the symlink in /dev without mounting it at all. I didn't try the ACTION commands at this point.

Last edited by titopoquito; 05-13-2007 at 03:44 PM.
 
Old 05-13-2007, 09:34 PM   #14
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Have you tried udevtest with the device path to see what rules are specifically being applied?
 
Old 05-13-2007, 09:37 PM   #15
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Another thought: you may want to try removing the whitespace from the model and vendor strings. Per the udev manpage:
Quote:
Trailing whitespace in the
attribute values is ignored, if the specified match value does not
contain trailing whitespace itself.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't run openoffice from bash-script with udev krisealv Programming 1 04-16-2007 11:17 AM
udev does not run program on remove uselpa Slackware 2 10-22-2006 04:24 AM
usb won't work with udev dinolinux Linux - Software 4 06-14-2006 10:21 AM
udev rules all_partitions doesn't work arubin Slackware 1 06-10-2006 05:46 PM
Making UDEV Work dannyl Slackware 42 01-29-2006 12:20 AM


All times are GMT -5. The time now is 08:38 PM.

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