-   Linux - Software (
-   -   udev to run script not behaves like when run in terminal (

matiasar 04-13-2012 08:05 AM

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:


# 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/"
ACTION=="remove", KERNEL=="sdb*",
RUN+="/bin/umount /media/removible1"
ACTION=="add",KERNEL=="sdc*", RUN+="/bin/mount -o sync,ro /dev/%k
/media/removible1", RUN+="/usr/local/share/myscripts/"
ACTION=="remove", KERNEL=="sdc*",
RUN+="/bin/umount /media/removible1"

And this is the script udev runs:


#!/usr/bin/env bash

#Actual mpd user

# 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 ]
                exit 1

if [ $1 ]

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

for item in $(find $DEFUSB -regex ".*\(mp3\|ogg\|flac\|wma\|aac\)$" )
                DIRNAME=$(dirname $item)
                # Creates Artist/Album/ dir. structure
                if [ ! -d $RAMFS/$DIRNAME ]; then
                        mkdir -p $RAMFS$DIRNAME
                        ln -s $item $RAMFS/$DIRNAME/$(basename $item)
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


MensaWater 04-13-2012 08:23 AM

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:

(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.)

matiasar 04-16-2012 06:56 AM

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.


catkin 04-16-2012 09:51 AM

Or you could set/export a normal PATH in the script.

MensaWater 04-16-2012 10:08 AM


Originally Posted by catkin (Post 4654154)
Or you could set/export a normal PATH in the script.

I put that as an option in my response.

catkin 04-16-2012 08:27 PM


Originally Posted by MensaWater (Post 4654169)
I put that as an option in my response.

Apologies for the repetition.

matiasar 04-17-2012 07:56 AM

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.


All times are GMT -5. The time now is 05:47 PM.