3

What I plan to achieve:

I am trying to automatically run a script when a certain SD card is inserted.

FACTS:

a. Script I plan to run is in /home/vivek/Desktop/Message.sh and has the following script in it. Its permissions are set as being an executable file:

#!/bin/sh
echo "Card is now inserted"

b. Under /etc/udev/rules.d my rules file is called 10-local.rules and has the following rule in it:

ENV{ID_FS_UUID}=="C097-C12A", ACTION == "add", RUN+="/bin/sh /home/vivek/Desktop/Message.sh"

c. The ENV{ID_FS_UUID} of my SD card is detected by udevadm is as follows:

enter image description here

d. My rules file in /etc/udev/rules.d/ is being processed as found by 'udevadm test' pictured below:

enter image description here

ISSUE: When I insert my SD card it mounts fine. But my SCRIPT NEVER GETS EXECUTED! Any help is appreciated.

Cheers

Vivek
  • 91
  • 1
  • 2
  • 4
  • have you seen this: http://askubuntu.com/questions/642511/how-to-autorun-files-and-scripts-in-ubuntu-when-inserting-a-usb-stick-like-autor – Elder Geek Oct 07 '16 at 21:18
  • Thanks. I just looked it up. It does not seem to be directly contextual to my issue. In my problem, I seem to be doing all the right things. But, obviously, am missing something. Not able to figure out. – Vivek Oct 07 '16 at 21:21
  • It would seem to me that you are attempting to autorun a specific script upon insertion of a specific media mounted by udev. Did I misunderstand your question? – Elder Geek Oct 07 '16 at 21:23
  • You are right. I would like to automatically execute a certain script when a certain SD card is inserted. – Vivek Oct 07 '16 at 21:30
  • Thats why I believe the link I provided to you is directly applicable to your issue. Did you try the solution there? Did it succeed or fail? – Elder Geek Oct 07 '16 at 21:32
  • My script is not on the SD card as is in the example you provide. Instead, my script resides on the machine in a certain folder and must be called by udev processes. So the example you provided does not quite apply, unless I am missing something. – Vivek Oct 07 '16 at 21:35
  • I've reviewed your script as posted and see no reason why it can't be placed on the card unless I'm missing something. feasible as the images are hard to read – Elder Geek Oct 07 '16 at 21:39
  • Well I can place it on the card for this particular card. But what i intend to do is run different scripts for different devices that are connected to this computer. EG: A USB Stick, A Camera, a Hard Drive, A printer or many other such things. So placing a script on the card may not be very useful to me, unfortunately. Thanks for thinking of a solution, but, that may not be the one I am looking for. – Vivek Oct 07 '16 at 22:15
  • If I'm understanding your intent, you might wish to consider that the Device IDs are specific to that device and will not be the same from one device to another even if they share a type. Related: http://superuser.com/questions/521402/use-uuid-in-udev-rules-and-mount-usb-drive-on-media-uuid – Elder Geek Oct 07 '16 at 22:42
  • Yes. I plan to use a combination of the KERNEL, SUBSYSTEMS, UUID etc... so I can uniquely identify what has been connected and accordingly execute an external script – Vivek Oct 07 '16 at 22:55
  • If you change `echo "Card is now inserted"` to `echo "Card is now inserted" > /tmp/foo`, does that file get created? – Mark Plotnick Oct 07 '16 at 23:45
  • I had tried "Card is now inserted" > /home/vivek/Desktop/Test. It did NOT create the file. – Vivek Oct 07 '16 at 23:48
  • In that case you might wish to read the Related link I left you most carefully. – Elder Geek Oct 08 '16 at 01:39

1 Answers1

1

I think the problem may be that your rule file 10-local.rules is far too early in the execution list. If you rename it 95-local.rules and reload the rules with sudo udevadm control --reload (if necessary on your system), then you may have more success. Note, there is never any output from udev, so an echo in your script needs to be to a file.

The values shown by udevadm info that begin E: are ENV{} properties that may be set by earlier rules, so you should put your rules as late as possible.

meuh
  • 49,672
  • 2
  • 52
  • 114
  • Thanks @meuh. That made sense at first. But then remember that 10-local.rules is the only file in /etc/udev/rules.d/ so it may not matter what I rename it to be. However, I am going to try to rename it to something like a 99zzz.rules an dplace it in /lib/udev/rules.d/ in the odd event that ENV{} is being worked upon there and will come back with finding. – Vivek Oct 08 '16 at 19:09
  • It worked when I put my new file in /lib/udev/rules.d as the file that is executed last in sequence! Thanks @meuh for the idea. – Vivek Oct 08 '16 at 19:20
  • The files in `/etc/udev/rules.d/` and `/usr/lib/udev/rules.d/` are merged by order of filename, so normally local changes are made in `/etc/udev/rules.d/`. – meuh Oct 08 '16 at 19:41
  • I am trying to detect USB insert regardless of the device it is. Eg: A phone, a battery pack, a biometric system etc.. which may or may not be recognized by Linux (no drivers or nothing to "Drive/run") Linux will, detect the USB insert, and then proceed to register and act on Recognized devices. For instance it will list USB device details in /dev/bus/usb/ etc... I would like to trap/process everything that is inserted into USB. This means, no dependence on stuff like udevadm. Also means I will need to trap the insert event early on the curve. How do I do this? – Vivek Oct 09 '16 at 02:53
  • The one above is a new question, which I was wondering if I could get some on :) Pardon the super terse language, had a character count limitation. lol! – Vivek Oct 09 '16 at 02:59
  • You should probably post a new question with more details on what you actually want to achieve, e.g. you want Linux to ignore all usb devices except your sd card. – meuh Oct 09 '16 at 07:09
  • I did .... here it is ..http://unix.stackexchange.com/questions/315216/ubuntu-16-04-how-can-i-detect-a-device-independent-usb-insert-event – Vivek Oct 09 '16 at 07:15