LinuxQuestions.org
Review your favorite Linux distribution.
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
 
LinkBack Search this Thread
Old 04-13-2012, 08:05 AM   #1
matiasar
Member
 
Registered: Nov 2006
Location: Argentina
Distribution: Debian
Posts: 312
Blog Entries: 1

Rep: Reputation: 31
udev to run script not behaves like when run in terminal


I'm trying to use udev to automount USB storage device and run a script which looks for music files, and then "dynamicaly" adds them to MPD playlist and playthem.
If I run the script from a CLI terminal works ok, but when udev runs the behaviour seems a little different, as the playlist appears trunkated or incomplete.

These are the rules I add to udev in order to automount devices:

Code:
# Mount USB storage in ro mode
ACTION=="add",KERNEL=="sdb*", RUN+="/bin/mount -o sync,ro /dev/%k
/media/removible1", RUN+="/usr/local/share/myscripts/autoplayUSB.sh"
ACTION=="remove", KERNEL=="sdb*",
RUN+="/usr/local/share/myscripts/autoplayUSB-stop.sh",
RUN+="/bin/umount /media/removible1"
ACTION=="add",KERNEL=="sdc*", RUN+="/bin/mount -o sync,ro /dev/%k
/media/removible1", RUN+="/usr/local/share/myscripts/autoplayUSB.sh"
ACTION=="remove", KERNEL=="sdc*",
RUN+="/usr/local/share/myscripts/autoplayUSB-stop.sh",
RUN+="/bin/umount /media/removible1"
And this is the script udev runs:

Code:
#!/usr/bin/env bash

#Actual mpd user
MPDUSER="mpd"
RAMFS='/mnt/ramfs'
DEFUSB='/media/removible1'

# Just root will do:
mount -t ramfs -o size=2M,mode=0755 ramfs $RAMFS

# Verifies virtual FS is mounted
cat /proc/mounts | grep $RAMFS > /dev/null
if [ $? -ne 0 ]
        then
                exit 1
fi
# EOD FIN VERIFICACION FS VIRTUAL

if [ $1 ]
        then
          DEFUSB=$1
fi

# IFS is Bash's internal field separator, and here we set it to consist of a newline character only.
IFS=$'\n'

for item in $(find $DEFUSB -regex ".*\(mp3\|ogg\|flac\|wma\|aac\)$" )
        do
                DIRNAME=$(dirname $item)
                # Creates Artist/Album/ dir. structure
                if [ ! -d $RAMFS/$DIRNAME ]; then
                        mkdir -p $RAMFS$DIRNAME
                else
                        ln -s $item $RAMFS/$DIRNAME/$(basename $item)
                fi
        done
su $MPDUSER -c "mpc update"
su $MPDUSER -c "mpc ls ramfs | mpc add"
su $MPDUSER -c "mpc play"
I think I'm making any mistake related to how udev runs the script.
Any suggestion will be very appreciated! Thanks

Matías
 
Old 04-13-2012, 08:23 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,765
Blog Entries: 4

Rep: Reputation: 697Reputation: 697Reputation: 697Reputation: 697Reputation: 697Reputation: 697
I don't understand the purpose of your first line in the script. Since you're not passing any options env is doing nothing other than attempting to invoke bash which could be done directly by making the line:
Code:
#!/bin/bash
(or whatever the path to bash is on your system)

In looking through your udev entry it has full paths defined for everything so the problem is the script. You use multiple things in the script (bash, find, mount) that you don't have full path of the command listed for. When you login you are invoking various profiles for the shell that set environmental variables including PATH. When you run a command in the background (e.g. via init script, udev, cron) it inherits a very minimal environment. Key variables are either not set or not as complete as they are after login. The main one usually being PATH. You can type "echo $PATH" to see what this is set to. You can type "env" to see all the variables set. You could then add these commands to your script to output to files and see the difference the script outputs when run as udev as opposed to from command line.

The simplest thing is to put full paths of commands in. Alternatively when you have a lot of them you can simply set the PATH variable at the beginning of the script so it can find all the commands you're using later. (You do still need to do the full path to bash at the #! line because that tells the script to run in bash shell.)
 
Old 04-16-2012, 06:56 AM   #3
matiasar
Member
 
Registered: Nov 2006
Location: Argentina
Distribution: Debian
Posts: 312
Blog Entries: 1

Original Poster
Rep: Reputation: 31
Thanks a lot for your reponse.
I'll add full path to every command invocation within the script and I'll let you know if everything goes fine.

Regards
 
Old 04-16-2012, 09:51 AM   #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,512
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
Or you could set/export a normal PATH in the script.
 
Old 04-16-2012, 10:08 AM   #5
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,765
Blog Entries: 4

Rep: Reputation: 697Reputation: 697Reputation: 697Reputation: 697Reputation: 697Reputation: 697
Quote:
Originally Posted by catkin View Post
Or you could set/export a normal PATH in the script.
I put that as an option in my response.
 
Old 04-16-2012, 08:27 PM   #6
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,512
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
Quote:
Originally Posted by MensaWater View Post
I put that as an option in my response.
Apologies for the repetition.
 
Old 04-17-2012, 07:56 AM   #7
matiasar
Member
 
Registered: Nov 2006
Location: Argentina
Distribution: Debian
Posts: 312
Blog Entries: 1

Original Poster
Rep: Reputation: 31
Thanks for your suggestinos folks. It's working much better now with full path to commands.
Still I have some issue with script when it adds music files fount to build the playlist, I have to take a look.

Thanks
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UDEV: Unable to run a command line interface script after USB automount using udev mohamr2 Linux - Embedded & Single-board computer 8 08-16-2011 10:39 AM
[SOLVED] Using UDEV to automatically run a script on optical disc mount? atavus Linux - Software 8 03-04-2010 09:48 PM
run a terminal from script abd_bela DamnSmallLinux 1 06-24-2008 06:18 AM
Script run on xterm terminal, how? hraposo Linux - General 3 08-14-2007 08:12 PM
Can't run openoffice from bash-script with udev krisealv Programming 1 04-16-2007 11:17 AM


All times are GMT -5. The time now is 05:02 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