Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Please don't mark this as duplicate as this is a different question!

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 still there are 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 2 questions:

1.Whenver I plug in my usb device script is executed because a file env.out is generated in /tmp but the data from usb device is not copied in to test directory, if I run the script manually then it works fine! Why??

2.How can 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 connected USB device to my computer automatically, when it is connected.

Thanks in advanced!

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

3 Answers 3

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.

share|improve this answer
    
Please read the comments before answering the question –  Null pointer Jan 7 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 7 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 7 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 7 at 15:53
up vote 0 down vote accepted

To answer my own question:

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

Solution to this:

Make this script sudo executable!

EDIT 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 sudo password

but it may cause some security problems so please refer this link

2.Answer to my second question:

To make this script work for any USB device which is connected change the rules file to this

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

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

Thats it! Make sure that script is executable and Plug in your USB!!

ENjoy!

share|improve this answer

Have a look at this, cuttlefish.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.