26

What can I do to run automatically a script after I mount/plugin or unmount/unplug a USB device?

Sadi
  • 10,996
Radu Rădeanu
  • 169,590
  • 2
    @XxxXxx : is not an option as long as Cuttlefish doesn't have a release version for Ubuntu 12.10 – Radu Rădeanu Apr 22 '13 at 16:42
  • 2
    possible duplicate: http://askubuntu.com/questions/25071/how-to-run-a-script-when-a-distinct-flash-drive-is-mounted?rq=1 – Glutanimate Apr 22 '13 at 16:44
  • @Glutanimate: that question is about a specific USB flash-drive, but I asked about a USB device, in generally – Radu Rădeanu Apr 22 '13 at 17:22
  • @Glutanimate: let say that the answers from that question are helpful, anyway doesn't answer at how to run automatically a script after I unplugged an USB device. – Radu Rădeanu Apr 22 '13 at 17:35
  • @Radu ...well I m using it in 13.04, let us know If u r interested in cuttlefish – Qasim Apr 23 '13 at 00:32
  • @XxxXxx I think he is looking for a command line way ;) – Suhaib Apr 24 '13 at 22:37
  • @Suhaib Yes, for this I am looking – Radu Rădeanu May 04 '13 at 18:33
  • @Radu This might help you, https://help.ubuntu.com/community/UsbDriveDoSomethingHowto – Suhaib May 04 '13 at 23:12
  • 1
    " let say that the answers from that question are helpful, anyway doesn't answer at how to run automatically a script after I unplugged an USB device" I'm guessing that if you change "ACTION='add'" to "ACTION='remove'", that would be a good place to start. –  May 05 '13 at 05:25
  • I tried udev rule ACTION=="add", ATTRS{idVendor}=="xxxxx", ATTRS{idProduct}=="yyyyy", RUN+="/usr/bin/sudo -u user /home/user/.bin/zzzzz" but had no result - so looking forward to seeing some solution here. – Sadi Aug 26 '13 at 21:36

2 Answers2

20

Thanks to MinimusHeximus and the respective contributors to the thread he mentioned in his comment to my similar question, I think I can now offer you the following answer.

You'll need 5 (five) files for such a USB device as follows, simply filling in respective values <fortheseparts>:

/etc/udev/rules.d/00-usb-<yourdevice>.rules

ACTION=="add", ATTRS{idVendor}=="<yourvendorid>", ATTRS{idProduct}=="<yourproductid>", ENV{XAUTHORITY}="/home/<user>/.Xauthority", ENV{DISPLAY}=":0", OWNER="<user>", RUN+="/usr/local/bin/usb-<yourdevice>-in_udev"    
ACTION=="remove", ATTRS{idVendor}=="<yourvendorid>", ATTRS{idProduct}=="<yourproductid>", ENV{XAUTHORITY}="/home/<user>/.Xauthority", ENV{DISPLAY}=":0", OWNER="<user>", RUN+="/usr/local/bin/usb-<yourdevice>-out_udev"

/usr/local/bin/usb-<yourdevice>-in_udev

#!/bin/bash
/usr/local/bin/usb-<yourdevice>-in &

/usr/local/bin/usb-<yourdevice>-in

#!/bin/bash
sleep 1
<yourbashscriptcode>

/usr/local/bin/usb-<yourdevice>-out_udev

#!/bin/bash
/usr/local/bin/usb-<yourdevice>-out &

/usr/local/bin/usb-<yourdevice>-out

#!/bin/bash
sleep 1
<yourbashscriptcode>

Notes:

  1. You can capture the values <yourvendorid> and <yourproductid> by entering the command lsusb in Terminal -- when your USB device is plugged in -- which will list all your USB devices currently available, like Bus 003 Device 002: ID 8087:07da Intel Corp., where 8087 is the VendorID and 07da is the ProductID.

  2. And <yourdevice> can be any arbitrary name you may choose for your USB device, for example, I chose to use the generic name "keyboard" when creating such files for my USB keyboard which required applying a different keyboard layout whenever it's plugged in.

  3. In some scenarios, it may not be necessary to use the ACTION=="remove" line in the udev rules file, and hence the associated 2 (two) "out" files, when you don't need to do anything (e.g. reverse a change made when the device is plugged in) after the device is plugged out.

  4. Some display managers store the .Xauthority outside the user home directory. You will need to update the ENV{XAUTHORITY} accordingly. As an example GNOME Display Manager looks as follows:

    $ printenv XAUTHORITY
    
    /run/user/1000/gdm/Xauthority
    
abrac
  • 200
Sadi
  • 10,996
  • This looks wrong from my novice perspective. Don't those clauses have to be on the same line? – nafg Nov 28 '13 at 04:57
  • 1
    This works: ACTION=="add", ATTRS{idVendor}=="...", ATTRS{idProduct}=="...", ENV{XAUTHORITY}="/home//.Xauthority", ENV{DISPLAY}=":0", RUN+=" – nafg Nov 28 '13 at 05:56
  • @nafg: from my novice perspective, it shouldn't make any difference if you put all those in one line (using comma as separator) in udev rules file, but I see that you have added XAUTHORITY and DISPLAY parameters to udev rules, which might work for you or somebody else, in which case I wonder if they are no longer necessary in the two script files below. – Sadi Dec 01 '13 at 17:07
  • 1
    yes that's the point, you no longer need the intermediate script (except perhaps to put the actual script in the background) since udev lets you set environment. Also my understanding from the udev docs is that each line is an independent rule, lines in a file are not combined. – nafg Dec 09 '13 at 01:17
  • @nafg: Thanks for your valuable contribution. I moved these environments to udev rule and it worked for me too. Also, I moved the scripts to /usr/local/bin as I began encountering problems after I moved my /home folder to a separate partition on its own (probably because it wasn't mounted earlier). It seems using line breaks in udev rules doesn't pose any problems at least in my system (Ubuntu 13.04 64 bit) and I prefered this for readability. – Sadi Dec 09 '13 at 13:53
  • Interesting. Are you sure it's only running for the specified device and not other devices? I haven't found docs that support that. For ex. http://www.reactivated.net/writing_udev_rules.html says "Be aware that udev does not support any form of line continuation. Do not insert any line breaks in your rules, as this will cause udev to see your one rule as multiple rules and will not work as expected." – nafg Dec 11 '13 at 21:02
  • Interesting indeed. This is my first work with udev and it seems you're right although this udev rule works in spite of breaching a rule about udev rules ;-) Just in case, I should better edit that udev rule accordingly. – Sadi Dec 12 '13 at 08:05
  • How to make the script executed only once? – MUY Belgium Nov 14 '23 at 09:58
  • @MUYBelgium I think what you're asking contradicts the very logic of udev rules, which "RUN" a script every time udev detects an "ACTION" by "{idVendor}{idProduct}". So when a certain USB keyboard is plugged in or unplugged the keyboard layout is changed accordingly. – Sadi Nov 15 '23 at 11:56
4

The file manager SpaceFM allows that. See Auto Run settings, for example Auto Run | On Mount or Auto Run | On Unmount.

jep
  • 448