Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
cuttlefish ............... – Qasim Apr 22 '13 at 15:29
@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
possible duplicate:… – 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
" 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. – blujay May 5 '13 at 5:25
up vote 9 down vote accepted

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>:


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 &


sleep 1


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


sleep 1


  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.
share|improve this answer
This looks wrong from my novice perspective. Don't those clauses have to be on the same line? – nafg Nov 28 '13 at 4:57
This works: ACTION=="add", ATTRS{idVendor}=="...", ATTRS{idProduct}=="...", ENV{XAUTHORITY}="/home/<user>/.Xauthority", ENV{DISPLAY}=":0", RUN+="<script>" – nafg Nov 28 '13 at 5: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 1 '13 at 17:07
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 9 '13 at 1: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 9 '13 at 13:53

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

share|improve this answer

Your Answer


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.