Is there a way to run a script when a particular USB device is mounted?
I keep my videos on a separate USB and would like to run a script that would mount the video folder on the USB device to the one in the home folder.
|
|
|
Start by finding your device in Create a new udev rules file in
Note how I used the ID from Then you just need to write the script to do the work. A simple mount command should work. You might need a Addition from Allan: Long running scripts might block "all further events for this or a dependent device". My Mint man page further states "Long running tasks need to be immediately detached from the event process itself." No tip is given on where to gain the skill to do this. Reply from Oli: Wrap it like so: https://askubuntu.com/a/106359/449 |
|||||||||||||||||||||
|
|
There's much nicer solution with systemd now. You create a service which depends and is wanted by you media e.g.:
Then you have to start/enable the service: After mount systemd fires your trigger script. The advantage over udev rule is that the script really fires after mount, not after adding system device. Use case: I have a crypted partition which I want to backup automatically. After adding the device I have to type-in password. If I hooked the backup script to udev, the script attempts to run at the time when I'm typing password, which will fail. Resource: Scripting with udev Note: You can find your device unit with: |
|||||||||||||||||||||
|
|
Another way to get the values for ATTRS{idVendor} and ATTRS{idProduct} (tested in Ubuntu 12.04) is:
|
|||
|
|
|
In Nautilus under Edit>Preferences>Media you can choose "other action" and than "costum command". for different kind of media to be executed. By that time the usb drive is already mounted, but I suppose you could still link it (with a costum command) to the folder you want the drive to appear in. I couldn't tell whether this is easier or better than using udev. |
|||||
|
|
If you don't want to interfere with your filemanager (nautilus, konquerer, gnome, etc) 's control over mounting and unmounting your device, I suggest not going the udev route. Instead, use udisks-glue if your system uses udisks (almost all do). After installing, just create a config file My following example updates GPS-Assist data on my camera everytime I plug in the SD-card.
Afterward just make sure udisks-glue starts when you boot or log-in. I.e. via gnome's startup applications |
|||||
|
|
|
|||
|
|
|
@sumid's answer has a problem. You should not start the service. You just need to enable it. file:
Then enable the service:
That's all. Note: You can find your medial label with: Note2: If the service is failed somehow (for example, the script is not executable), your mount point will change to |
||||
|
|