3

I'm trying to set a simple script to run whenever a USB storage device is being plugged to the computer. For that task I have a 100-usb.rules file in /etc/udev/rules.d which has a very generic rule:

ACTION=="add", RUN+="/usr/local/auto-update.sh"

I expect that whenever I plug anything to the computer that script will trigger. The reason the rule is so generic is because doesn't matter what kind of rule I tried the script just NEVER being run.

Now the /user/local/auto-update.sh script has a very simple command:

#!/bin/bash

mkdir /tmp/testfolder

I've made sure to give execute rights to the script with

sudo chmod +x /usr/local/auto-update.sh

after running the command ls -l /usr/local/auto-update.sh I get the permissions

-rwxr-xr-x

The weird thing is that if I alter my rule to look something like

ACTION=="add", RUN+="/bin/mkdir /tmp/folder1"

Then it is actually going ahead and creating that folder.

I really am lost here... I have NO clue why it's not working!

16
  • Please edit your question and post the output of ls -l /usr/local/auto-update.sh. You have only given execute rights to the owner of the file, not everyone.
    – terdon
    Sep 13, 2015 at 12:37
  • @terdon I've edited my post. It seems like the execute right is being granted to everyone
    – kfirba
    Sep 13, 2015 at 12:41
  • 2
    Rather a workaround, but until you find out why it's not working /bin/bash /usr/local/auto-update.sh will probably work
    – kos
    Sep 13, 2015 at 12:42
  • @kos well, thanks it seems to work now. But it really annoys me as I don't understand what the issue is :/
    – kfirba
    Sep 13, 2015 at 12:44
  • 1
    Try changing the RUN rule to RUN+="/usr/local/auto-update.sh 2>/tmp/error". I'm not sure if that will work but it might give us some debugging information.
    – terdon
    Sep 13, 2015 at 12:55

1 Answer 1

0

The problem is that udev does not provide a PATH in its exported environment when running scripts.

Add a PATH=/bin:/usr/bin:/usr/local/bin or whatever to the start of your bash script.

For example, I dumped the env contents when a script of mine was called as a usb key was plugged in and got:

ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb1/1-3/1-3:1.0/host4
DEVTYPE=scsi_host
ID_BUS=usb
ID_MODEL=Transcend
ID_MODEL_ENC=Transcend
ID_MODEL_ID=4000
ID_REVISION=0035
ID_SERIAL=TS-RDF5_Transcend_000000000036
ID_SERIAL_SHORT=000000000036
ID_TYPE=scsi
ID_USB_DRIVER=usb-storage
ID_USB_INTERFACES=:080650:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=TS-RDF5
ID_VENDOR_ENC=TS-RDF5\x20
ID_VENDOR_ID=8564
SEQNUM=2023
SUBSYSTEM=scsi
USEC_INITIALIZED=65380632

i.e. no PATH.

5
  • how does that settle with our findings in the question's comment section? that's interesting.
    – kfirba
    Sep 13, 2015 at 13:04
  • just caught up with the comments: dont think you can do redirection and shell stuff in the RUN command.
    – meuh
    Sep 13, 2015 at 13:09
  • Ha, that would explain it. @kfirba Try ACTION=="add", RUN+="/usr/local/auto-update.sh" and /bin/mkdir /tmp/testfolder (or add PATH=/bin:/usr/bin:/usr/local/bin at the top of the script as meuh suggested). Please also let me know if it works so I can upvote this answer
    – kos
    Sep 13, 2015 at 13:09
  • 1
    @kos I'm just not sure if it solved the issue or not since when I reverted all the changes to what I had originally it suddenly started to work :/
    – kfirba
    Sep 13, 2015 at 13:26
  • @kfirba I doubt this is the problem. udev only knows about programs in /lib/udev but bash is called with its full path in the shebang line and that means it has its own environment, including a path. Try adding an echo $PATH > /tmp/foo to your script and you'll see what it has as PATH. In any case, if this is indeed the issue, just change the mkdir in your script to /bin/mkdir.
    – terdon
    Sep 13, 2015 at 13:35

Not the answer you're looking for? Browse other questions tagged .