Ubuntu: copying a file from a USB storage if it exist with udev rules



Question:

I have a simple udev rule to match USB storage devices which runs a simple script that looks for a file in the USB. I'm using the usbmount package in order to automatically mount the USB. As the documentation says, the USB devices will be mounted to /media/usb{0..7}.

When the USB storage device has been detected I run this script

#!/bin/bash    options=("usb0" "usb1" "usb2" "usb3" "usb4" "usb5" "usb6" "usb7")    for dir in "${options[@]}"  do      lookup="/media/$dir/luah_v2_version.zip"        if [ -e "$lookup" ]; then          cp $lookup /home/luah          exit 0      else          echo "could not find $lookup" >> /home/luah/log      fi  done  

The thing is that when the script is ran automatically after the USB was connected, it results in a log file where it says that the file could not be found in any of the directories above (while it does exist). When I then manually run the script it works and copies the zip file to where I wanted.

I've tried to set a wait time before the script executes of 10 seconds to see maybe the USB was not mounted by the time the script ran but I get the same end result.


Solution:1

Probably usb has not been mounted at the time your rule is processed. Try adding a hook to the usbmount i.e. create a file named /etc/usbmount/mount.d/10_autocopy.sh

#!/bin/sh  [ -f $UM_MOUNTPOINT/luah_v2_version.zip ] &&  cp $UM_MOUNTPOINT/luah_v2_version.zip /home/luah  

then make it executable:

chmod +x /etc/usbmount/mount.d/10_autocopy.sh  

variables set for mount scripts (/etc/usb/mount.d):

  • UM_DEVICE: mounted device
  • UM_MOUNTPOINT: mount point of the device
  • UM_FILESYSTEM: filesystem type
  • UM_MOUNTOPTIONS: mount options
  • UM_VENDOR: vendor string of the device
  • UM_MODEL: model or product string

variables set for unmount scripts (/etc/usb/umount.d):

  • UM_DEVICE: mounted device
  • UM_MOUNTPOINT: mount point of the device
  • UM_FILESYSTEM: filesystem type

Note:

  • all scripts are by default executed as user root.
  • usbmount uses the syslog facility if VERBOSE=yes - /etc/usbmount/usbmount.conf

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »