What can I do to run automatically a script after I mount/plugin or unmount/unplug a USB device?
-
2@XxxXxx : is not an option as long as Cuttlefish doesn't have a release version for Ubuntu 12.10– Radu RădeanuApr 22, 2013 at 16:42
-
2possible duplicate: askubuntu.com/questions/25071/…– GlutanimateApr 22, 2013 at 16:44
-
@Glutanimate: that question is about a specific USB flash-drive, but I asked about a USB device, in generally– Radu RădeanuApr 22, 2013 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ădeanuApr 22, 2013 at 17:35
-
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.– user41313May 5, 2013 at 5:25
2 Answers
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:
You can capture the values
<yourvendorid>
and<yourproductid>
by entering the commandlsusb
in Terminal -- when your USB device is plugged in -- which will list all your USB devices currently available, likeBus 003 Device 002: ID 8087:07da Intel Corp.
, where 8087 is the VendorID and 07da is the ProductID.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.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.Some display managers store the
.Xauthority
outside the user home directory. You will need to update theENV{XAUTHORITY}
accordingly. As an example GNOME Display Manager looks as follows:$ printenv XAUTHORITY
/run/user/1000/gdm/Xauthority
-
This looks wrong from my novice perspective. Don't those clauses have to be on the same line?– nafgNov 28, 2013 at 4:57
-
1This works: ACTION=="add", ATTRS{idVendor}=="...", ATTRS{idProduct}=="...", ENV{XAUTHORITY}="/home/<user>/.Xauthority", ENV{DISPLAY}=":0", RUN+="<script>"– nafgNov 28, 2013 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.– SadiDec 1, 2013 at 17:07
-
1yes 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.– nafgDec 9, 2013 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.– SadiDec 9, 2013 at 13:53
The file manager SpaceFM allows that. See Auto Run settings, for example Auto Run | On Mount or Auto Run | On Unmount.