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.

9
  • aren't you just trying to do this? askubuntu.com/questions/474/…
    – virtualxtc
    Jan 7, 2014 at 8:28
  • @virtualxtc Thanks for this suggestion! But I want to know What is wrong with this approach? Jan 7, 2014 at 8:33
  • can you please tell me what is wrong with my script? Why is it not working? Jan 7, 2014 at 8: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 7, 2014 at 8: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? Jan 7, 2014 at 8:40

2 Answers 2

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!

1
  • Thanks for your answer! With this, I created a script that copies all images automatically into a picture frame when it gets attached: github.com/AmmanVMS/picture-frame
    – User
    Aug 20, 2022 at 5: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.

5
  • Please read the comments before answering the question Jan 7, 2014 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? Jan 7, 2014 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 Jan 7, 2014 at 11:47
  • Oh yes, sorry, the last 5 comments were hidden from me and I didn't notice them. My mistake! Jan 7, 2014 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, 2015 at 18:10

You must log in to answer this question.

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