14

I am trying to run a script whenever a pendrive is connected to my system. I followed this and this question and it is working, but there are still some problems.

This is my udev rules file:

ACTION=="add", ATTRS{idVendor}=="8564", ATTRS{idProduct}=="1000", RUN+="/usr/local/my_script.sh"

And this is my script:

#!/bin/bash

env > /tmp/env.out cp -r /media/device-name/* ~/test/

I have two questions:

  1. Whenever I plug my USB device in, the script is executed because a file env.out is generated in /tmp, but the data from the USB device are not copied to the test directory.

    If I run the script manually, it works fine! Why?

  2. How can I make my rules file more general so that it will work for any USB device which is connected without knowing its ID?

My idea behind this is to copy all the data from any connected USB device to my computer automatically when it is connected.

Null pointer
  • 2,547
  • 5
  • 26
  • 38
  • aren't you just trying to do this? http://askubuntu.com/questions/474/how-can-i-keep-a-folder-synchronized-to-an-external-usb-hard-drive – virtualxtc Jan 07 '14 at 08:28
  • @virtualxtc Thanks for this suggestion! But I want to know What is wrong with this approach? – Null pointer Jan 07 '14 at 08:33
  • can you please tell me what is wrong with my script? Why is it not working? – Null pointer Jan 07 '14 at 08:36
  • Also, just a heads up - I've learned the hard way that specific questions like this usually are better suited for superuser than askubuntu. – virtualxtc Jan 07 '14 at 08:37
  • If this would have been the problem of permissions then it would have showed up when I tried to run it manually, isn't it? – Null pointer Jan 07 '14 at 08:40
  • A guess, but '~' expansion might not work in this case (I'm not sure which user the script runs under but it might be one that doesn't have a home directory). Try it with an absolute path. – chronitis Jan 07 '14 at 08:44
  • I was thinking that the udev user/group doesn't have the correct permissions, but I really have no idea. – virtualxtc Jan 07 '14 at 08:44
  • @chronitis I tried giving absolute path but still, No luck! :( – Null pointer Jan 07 '14 at 08:47
  • Also: the udev rule occurs when the block device (/dev/sdb1 or whatever) is created. I don't think it will have been mounted at this point (ie, /media/*device name* is probably either empty or doesn't exist). You probably need to explicitly mount it first, or somehow wait until udisks or similar has mounted it for you. I'm afraid I'm not certain how to do this correctly. – chronitis Jan 07 '14 at 08:54

2 Answers2

10

To answer my own question:

1. The script was not running because it needed sudo rights to run.

To solve this, make this script sudo executable:

  • Edit the sudoers file using:

    sudo visudo
    
  • After line 25 (i.e %sudo ALL=(ALL)) add this:

    username  ALL=(ALL) NOPASSWD: /home/username/my_script.sh
    

Now we can use sudo in this script without being asked for the sudo password. However, this may cause some security problems, so please refer to this question: How do I sudo a command in a script without being asked for a password?

2. Answer to my second question:

To make this script work for any USB device which is connected, change the udev rules file's content to this:

ACTION=="add", ATTRS{idVendor}=="`****`", ATTRS{idProduct}=="`****`", RUN+="/usr/local/my_script.sh"

The asterik (*) tells that this should be done for every USB device connected.

That's it! Make sure that script is executable and plug your USB in!

ENjoy!

Null pointer
  • 2,547
  • 5
  • 26
  • 38
  • Thanks for your answer! With this, I created a script that copies all images automatically into a picture frame when it gets attached: https://github.com/AmmanVMS/picture-frame – User Aug 20 '22 at 05:58
0

Try replacing the tilde with the full path to your home directory. I don't know if this will work, but you say that the script works when you run it directly but when it's run automatically it doesn't: This makes me think that the problem might be to do with it being run by a different user when it's run automatically, and the tilde resolves to different values for different users.

Toby 1 Kenobi
  • 133
  • 1
  • 8
  • Please read the comments before answering the question – Null pointer Jan 07 '14 at 11:37
  • I did read the comments, then I answered the question. Did I do something wrong? Which comment in particular are you referring to? – Toby 1 Kenobi Jan 07 '14 at 11:40
  • In comments these suggestions were already given and they didn't work for me..that is why I said read the comments – Null pointer Jan 07 '14 at 11:47
  • Oh yes, sorry, the last 5 comments were hidden from me and I didn't notice them. My mistake! – Toby 1 Kenobi Jan 07 '14 at 15:53
  • @Nullpointer: please edit your question and keep the complete information in the question so people don't need to read the comments... Comments are not part of the question... ;-) – Fabby Mar 11 '15 at 18:10