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!

terdon
  • 100,812
kfirba
  • 299
  • 2
  • 7
  • 18
  • 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 '15 at 12:37
  • @terdon I've edited my post. It seems like the execute right is being granted to everyone – kfirba Sep 13 '15 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 '15 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 '15 at 12:44
  • I've never used udev rules, but from what I'm reading around it should work with a script (also if it's a shell interpreting the command it should be able to read the shebang at the top and run it regardless), upvoted – kos Sep 13 '15 at 12:47
  • That's very strange. I just tested and it works fine here. Does running the script manually work? – terdon Sep 13 '15 at 12:50
  • @terdon Yes. When I run it manually it works – kfirba Sep 13 '15 at 12:53
  • 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 '15 at 12:55
  • According to this answer at some point specifying bash path became mandatory, but it doesn't reference any source. I guess the command is not interpreted by a shell, but if it is doing what terdon suggested definetly could give an insight – kos Sep 13 '15 at 12:58
  • @terdon Okay, that's disturbing. I've edited my RUN section and guess what? everything works just fine and there are no errors so the /tmp/error file is never being created. – kfirba Sep 13 '15 at 13:00
  • Was /tmp/testfolder created? – terdon Sep 13 '15 at 13:03
  • @terdon I changed the script to create a random file in some other location, but yea, it worked – kfirba Sep 13 '15 at 13:03
  • I wonder if you had some kind of non-printing character in there that was screwing things up. Did you ever edit the script in Windows or something? If you change it back to creating a dir in /tmp and it still works, could you delete this question? If it's not reproducible, it's unlikely to help anyone else. – terdon Sep 13 '15 at 13:06
  • @terdon It seems like the issue is randomly happening now... until now kos's workaround worked everytime – kfirba Sep 13 '15 at 13:26
  • @kfirba , try change its name from 100-usb.rules to 99-usb.rules – user.dz Sep 19 '15 at 10:25
  • @kfirba , Could you flag this for closing: off-topic > can't reproduced. I just read your comment on meuh's answer that the initial setup now working correctly. – user.dz Sep 27 '15 at 12:11

1 Answers1

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.

meuh
  • 3,211
  • how does that settle with our findings in the question's comment section? that's interesting. – kfirba Sep 13 '15 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 '15 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 '15 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 '15 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 '15 at 13:35